Skip to content

Commit

Permalink
feat(announce): implement expected_status_codes for webhooks (#5317)
Browse files Browse the repository at this point in the history
fixes #5316
  • Loading branch information
mattrobenolt authored Nov 29, 2024
1 parent 898c62c commit 77bbddf
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
24 changes: 17 additions & 7 deletions internal/pipe/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/url"
"slices"
"strings"

"github.com/caarlos0/env/v11"
Expand All @@ -24,6 +25,10 @@ const (
DefaultContentType = "application/json; charset=utf-8"
)

var defaultExepctedStatusCodes = []int{
http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent,
}

type Pipe struct{}

func (Pipe) String() string { return "webhook" }
Expand Down Expand Up @@ -71,6 +76,11 @@ func (p Pipe) Announce(ctx *context.Context) error {
return fmt.Errorf("webhook: %w", err)
}

expectedStatusCodes := ctx.Config.Announce.Webhook.ExpectedStatusCodes
if len(expectedStatusCodes) == 0 {
expectedStatusCodes = defaultExepctedStatusCodes
}

log.Infof("posting: '%s'", msg)
customTransport := http.DefaultTransport.(*http.Transport).Clone()

Expand Down Expand Up @@ -107,13 +117,13 @@ func (p Pipe) Announce(ctx *context.Context) error {
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent:
log.Infof("Post OK: '%v'", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
log.Infof("Response : %v\n", string(body))
return nil
default:
if !slices.Contains(expectedStatusCodes, resp.StatusCode) {
io.Copy(io.Discard, resp.Body)
return fmt.Errorf("request failed with status %v", resp.Status)
}

body, _ := io.ReadAll(resp.Body)
log.Infof("Post OK: '%v'", resp.StatusCode)
log.Infof("Response : %v\n", string(body))
return nil
}
33 changes: 33 additions & 0 deletions internal/pipe/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,39 @@ func TestAnnounceAdditionalHeadersWebhook(t *testing.T) {
require.NoError(t, Pipe{}.Announce(ctx))
}

func TestAnnounceExepectedStatusCodesWebhook(t *testing.T) {
responseServer := WebHookServerMockMessage{
Response: "Thanks for the announcement!",
UUID: uuid.New(),
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

body, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, "webhook-test", string(body))

w.WriteHeader(418)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(responseServer)
assert.NoError(t, err)
}))
defer srv.Close()

ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
EndpointURL: srv.URL,
MessageTemplate: "{{ .ProjectName }}",
ExpectedStatusCodes: []int{418},
},
},
})
require.NoError(t, Pipe{}.Announce(ctx))
}

func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(testctx.New()))
Expand Down
13 changes: 7 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,13 @@ type Announce struct {
}

type Webhook struct {
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty" json:"skip_tls_verify,omitempty"`
MessageTemplate string `yaml:"message_template,omitempty" json:"message_template,omitempty"`
EndpointURL string `yaml:"endpoint_url,omitempty" json:"endpoint_url,omitempty"`
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`
ContentType string `yaml:"content_type,omitempty" json:"content_type,omitempty"`
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty" json:"skip_tls_verify,omitempty"`
MessageTemplate string `yaml:"message_template,omitempty" json:"message_template,omitempty"`
EndpointURL string `yaml:"endpoint_url,omitempty" json:"endpoint_url,omitempty"`
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`
ContentType string `yaml:"content_type,omitempty" json:"content_type,omitempty"`
ExpectedStatusCodes []int `yaml:"exected_status_codes,omitempty" json:"expected_status_codes,omitempty"`
}

type Twitter struct {
Expand Down
5 changes: 5 additions & 0 deletions www/docs/customization/announce/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ announce:
# Authorization: "Bearer <token>"
headers:
User-Agent: "goreleaser"

# HTTP status codes to be considered as a successful response.
#
# Default: 200, 201, 202, 204
expected_status_codes: [418, 200, 201]
```
<!-- md:templates -->
6 changes: 6 additions & 0 deletions www/docs/static/schema-pro.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions www/docs/static/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77bbddf

Please sign in to comment.