Skip to content

Commit

Permalink
set user-agent header for success notification webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdzombak committed Jan 7, 2024
1 parent e5d7eab commit 65c5e7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
28 changes: 25 additions & 3 deletions deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ type discordDeliveryConfig struct {
logFileName string
}

const ntfyTimeout = 10 * time.Second
const discordTimeout = 10 * time.Second
const mailTimeout = 10 * time.Second
const (
successNotifyTimeout = 10 * time.Second
ntfyTimeout = 10 * time.Second
discordTimeout = 10 * time.Second
mailTimeout = 10 * time.Second
)

func executeDeliveries(config *deliveryConfig, runOutput *runOutput) []error {
var deliveryErrors []error
Expand Down Expand Up @@ -168,6 +171,25 @@ func executeDiscordDelivery(cfg *discordDeliveryConfig, runOutput *runOutput) er
return nil
}

func deliverSuccessNotification(url string) error {
client := http.DefaultClient
client.Timeout = successNotifyTimeout
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("failed to build GET request for '%s': %w", url, err)
}
req.Header.Set("User-Agent", productIdentifier())
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to GET '%s': %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode > 200 || resp.StatusCode < 299 {
return fmt.Errorf("failed to GET '%s' (%s)", url, resp.Status)
}
return nil
}

func extendErrSlice(errs []error, err error) []error {
if err != nil {
errs = append(errs, err)
Expand Down
15 changes: 2 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/user"
Expand Down Expand Up @@ -64,8 +63,6 @@ const (
CensorEnvVarsEnvVar = "RUNNER_CENSOR_ENV"
)

const successNotifyTimeout = 10 * time.Second

func usage() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] -- /path/to/program --program-args\n", filepath.Base(os.Args[0]))
_, _ = fmt.Fprintf(os.Stderr, "Run the given program, only printing its output if the program exits with an error, "+
Expand Down Expand Up @@ -438,16 +435,8 @@ func main() {
}

if runOut.succeeded && *successNotifyURL != "" {
client := http.DefaultClient
client.Timeout = successNotifyTimeout
resp, err := client.Get(*successNotifyURL)
if err != nil {
deliveryErrs = append(deliveryErrs, fmt.Errorf("failed to GET success notification URL '%s': %w", *successNotifyURL, err))
} else {
defer resp.Body.Close()
if resp.StatusCode > 200 || resp.StatusCode < 299 {
deliveryErrs = append(deliveryErrs, fmt.Errorf("failed to GET success notification URL '%s' (%s)", *successNotifyURL, resp.Status))
}
if err := deliverSuccessNotification(*successNotifyURL); err != nil {
deliveryErrs = append(deliveryErrs, fmt.Errorf("failed to call success notification URL: %w", err))
}
}

Expand Down

0 comments on commit 65c5e7e

Please sign in to comment.