Skip to content

Commit

Permalink
fix(announce): move default status codes to Default, more tests
Browse files Browse the repository at this point in the history
building on #5317
  • Loading branch information
caarlos0 committed Nov 29, 2024
1 parent 77bbddf commit c7f4e90
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
10 changes: 4 additions & 6 deletions internal/pipe/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (p Pipe) Default(ctx *context.Context) error {
if ctx.Config.Announce.Webhook.ContentType == "" {
ctx.Config.Announce.Webhook.ContentType = DefaultContentType
}
if len(ctx.Config.Announce.Webhook.ExpectedStatusCodes) == 0 {
ctx.Config.Announce.Webhook.ExpectedStatusCodes = defaultExepctedStatusCodes
}
return nil
}

Expand Down Expand Up @@ -76,11 +79,6 @@ 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 @@ -117,7 +115,7 @@ func (p Pipe) Announce(ctx *context.Context) error {
}
defer resp.Body.Close()

if !slices.Contains(expectedStatusCodes, resp.StatusCode) {
if !slices.Contains(ctx.Config.Announce.Webhook.ExpectedStatusCodes, resp.StatusCode) {
io.Copy(io.Discard, resp.Body)
return fmt.Errorf("request failed with status %v", resp.Status)
}
Expand Down
38 changes: 38 additions & 0 deletions internal/pipe/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestNoEndpoint(t *testing.T) {
Webhook: config.Webhook{},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `webhook: no endpoint url`)
}

Expand All @@ -50,6 +51,7 @@ func TestAnnounceInvalidMessageTemplate(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -87,6 +89,7 @@ func TestAnnounceWebhook(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -118,6 +121,7 @@ func TestAnnounceTLSWebhook(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -145,6 +149,7 @@ func TestAnnounceTLSCheckCertWebhook(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Error(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -182,6 +187,7 @@ func TestAnnounceBasicAuthWebhook(t *testing.T) {
},
})
t.Setenv("BASIC_AUTH_HEADER_VALUE", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte("user:pass"))))
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -220,6 +226,7 @@ func TestAnnounceAdditionalHeadersWebhook(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Announce(ctx))
}

Expand Down Expand Up @@ -253,6 +260,7 @@ func TestAnnounceExepectedStatusCodesWebhook(t *testing.T) {
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Announce(ctx))
}

Expand All @@ -272,3 +280,33 @@ func TestSkip(t *testing.T) {
require.False(t, Pipe{}.Skip(ctx))
})
}

func TestDefault(t *testing.T) {
t.Run("empty", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: config.Webhook{},
},
})
require.NoError(t, Pipe{}.Default(ctx))
actual := ctx.Config.Announce.Webhook
require.NotEmpty(t, actual.MessageTemplate)
require.NotEmpty(t, actual.ContentType)
require.NotEmpty(t, actual.ExpectedStatusCodes)
})
t.Run("not empty", func(t *testing.T) {
expected := config.Webhook{
MessageTemplate: "foo",
ContentType: "text",
ExpectedStatusCodes: []int{200},
}
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: expected,
},
})
require.NoError(t, Pipe{}.Default(ctx))
actual := ctx.Config.Announce.Webhook
require.Equal(t, expected, actual)
})
}

0 comments on commit c7f4e90

Please sign in to comment.