Skip to content

feat(coderd): add company logo when available for email notifications #14935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a42f108
feat(notifications): add company logo url when available for email no…
defelmnq Oct 2, 2024
1fa1271
feat(notifications): adapt tests with new labels
defelmnq Oct 3, 2024
2def52e
fix: revert changes on generated code version
defelmnq Oct 3, 2024
1b1a4c4
feat(notification): move logo_url and app_name logic to helpers funct…
defelmnq Oct 3, 2024
779260e
chore: remove unused GetLogoURL method from notifications store inter…
defelmnq Oct 3, 2024
1e6899f
fix: add better context and timeout for db related queries
defelmnq Oct 4, 2024
b552267
chore: lint
defelmnq Oct 4, 2024
73e07e9
feat(notifications): improve logo_url and app_name fetching moving it…
defelmnq Oct 16, 2024
70e23ae
feat(notifications): improve logo_url and app_name fetching moving it…
defelmnq Oct 16, 2024
2f620c9
feat(notifications): improve logo_url and app_name fetching moving it…
defelmnq Oct 16, 2024
fdcdf7b
feat(notifications): improve logo_url and app_name fetching moving it…
defelmnq Oct 16, 2024
9c6c105
feat(coderd): regenerate golden files
Oct 16, 2024
0c131a5
feat(notifications): working on tests for logo and app_name
defelmnq Oct 17, 2024
34d6611
feat(notifications): change helpers logic to be defined in the Dispat…
Oct 17, 2024
0a9a66a
feat(notifications): add golden file for custom appearance
Oct 17, 2024
bf558cb
feat(notifications): add golden file for custom appearance
Oct 17, 2024
a420f37
feat(notifications): work on tests
defelmnq Oct 18, 2024
51d8d33
feat(notifications): add golden file for custom appearance
defelmnq Oct 18, 2024
d492d09
feat(notifications): add golden file for custom appearance
defelmnq Oct 18, 2024
0c9c485
feat(notifications): add comment ent in tests for enterprise feature
defelmnq Oct 18, 2024
a6d4a0c
feat(coderd): remove unused call
defelmnq Oct 18, 2024
4c5cb3d
fix(notifications): improve tests and some nit fixes
defelmnq Oct 18, 2024
e419431
chore(retry): improve retry policy on fetcher
defelmnq Oct 18, 2024
a7fec66
Merge remote-tracking branch 'origin/main' into feat/notif-custom-logo
defelmnq Oct 21, 2024
8b766f6
improve errors handling
defelmnq Oct 22, 2024
157e086
improve errors handling
defelmnq Oct 22, 2024
790ff33
improve errors handling
defelmnq Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(notifications): add golden file for custom appearance
  • Loading branch information
defelmnq committed Oct 17, 2024
commit bf558cbeb492b97fc467df924bc9007188d9afc4
14 changes: 6 additions & 8 deletions coderd/notifications/dispatch/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package dispatch_test

var (
helpers = map[string]any{
"base_url": func() string { return "http://test.com" },
"current_year": func() string { return "2024" },
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
"app_name": func() string { return "Coder" },
}
)
var helpers = map[string]any{
"base_url": func() string { return "http://test.com" },
"current_year": func() string { return "2024" },
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
"app_name": func() string { return "Coder" },
}
2 changes: 1 addition & 1 deletion coderd/notifications/dispatch/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewWebhookHandler(cfg codersdk.NotificationsWebhookConfig, log slog.Logger)
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
}

func (w *WebhookHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
func (w *WebhookHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
if w.cfg.Endpoint.String() == "" {
return nil, xerrors.New("webhook endpoint not defined")
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/notifications/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ type santaHandler struct {
nice atomic.Int32
}

func (s *santaHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
return func(ctx context.Context, msgID uuid.UUID) (retryable bool, err error) {
func (s *santaHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
return func(_ context.Context, _ uuid.UUID) (retryable bool, err error) {
if payload.Labels["nice"] != "true" {
s.naughty.Add(1)
return false, xerrors.New("be nice")
Expand Down
30 changes: 15 additions & 15 deletions coderd/notifications/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestMetrics(t *testing.T) {

reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
template := notifications.TemplateWorkspaceDeleted
tmplate := notifications.TemplateWorkspaceDeleted

const (
method = database.NotificationMethodSmtp
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestMetrics(t *testing.T) {
user := createSampleUser(t, api.Database)

// Build fingerprints for the two different series we expect.
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, template.String())
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.String())
methodFP := fingerprintLabels(notifications.LabelMethod, string(method))

expected := map[string]func(metric *dto.Metric, series string) bool{
Expand All @@ -90,7 +90,7 @@ func TestMetrics(t *testing.T) {

var match string
for result, val := range results {
seriesFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, template.String(), notifications.LabelResult, result)
seriesFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.String(), notifications.LabelResult, result)
if !hasMatchingFingerprint(metric, seriesFP) {
continue
}
Expand Down Expand Up @@ -165,9 +165,9 @@ func TestMetrics(t *testing.T) {
}

// WHEN: 2 notifications are enqueued, 1 of which will fail until its retries are exhausted, and another which will succeed
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test") // this will succeed
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
require.NoError(t, err)
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
require.NoError(t, err)

mgr.Run(ctx)
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestPendingUpdatesMetric(t *testing.T) {

reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
template := notifications.TemplateWorkspaceDeleted
tmplate := notifications.TemplateWorkspaceDeleted

const method = database.NotificationMethodSmtp

Expand Down Expand Up @@ -248,9 +248,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
user := createSampleUser(t, api.Database)

// WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test") // this will succeed
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
require.NoError(t, err)
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
require.NoError(t, err)

mgr.Run(ctx)
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestInflightDispatchesMetric(t *testing.T) {

reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
template := notifications.TemplateWorkspaceDeleted
tmplate := notifications.TemplateWorkspaceDeleted

const method = database.NotificationMethodSmtp

Expand Down Expand Up @@ -334,7 +334,7 @@ func TestInflightDispatchesMetric(t *testing.T) {

// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
for i := 0; i < msgCount; i++ {
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
require.NoError(t, err)
}

Expand All @@ -343,7 +343,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
// THEN:
// Ensure we see the dispatches of the messages inflight.
require.Eventually(t, func() bool {
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), template.String())) == msgCount
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), tmplate.String())) == msgCount
}, testutil.WaitShort, testutil.IntervalFast)

for i := 0; i < msgCount; i++ {
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
var (
reg = prometheus.NewRegistry()
metrics = notifications.NewMetrics(reg)
template = notifications.TemplateWorkspaceDeleted
tmplate = notifications.TemplateWorkspaceDeleted
anotherTemplate = notifications.TemplateWorkspaceDormant
)

Expand All @@ -391,7 +391,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {

// GIVEN: a template whose notification method differs from the default.
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
ID: template,
ID: tmplate,
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
})
require.NoError(t, err)
Expand All @@ -417,7 +417,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {

user := createSampleUser(t, api.Database)

_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test")
require.NoError(t, err)
_, err = enq.Enqueue(ctx, user.ID, anotherTemplate, map[string]string{"type": "success"}, "test")
require.NoError(t, err)
Expand All @@ -438,7 +438,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
// THEN: we should have metric series for both the default and custom notification methods.
require.Eventually(t, func() bool {
return promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(defaultMethod), anotherTemplate.String(), notifications.ResultSuccess)) > 0 &&
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), template.String(), notifications.ResultSuccess)) > 0
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), tmplate.String(), notifications.ResultSuccess)) > 0
}, testutil.WaitShort, testutil.IntervalFast)
}

Expand Down
24 changes: 11 additions & 13 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1414,12 +1414,12 @@ func TestCustomNotificationMethod(t *testing.T) {

// GIVEN: a notification template which has a method explicitly set
var (
template = notifications.TemplateWorkspaceDormant
tmplate = notifications.TemplateWorkspaceDormant
defaultMethod = database.NotificationMethodSmtp
customMethod = database.NotificationMethodWebhook
)
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
ID: template,
ID: tmplate,
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
})
require.NoError(t, err)
Expand Down Expand Up @@ -1447,7 +1447,7 @@ func TestCustomNotificationMethod(t *testing.T) {

// WHEN: a notification of that template is enqueued, it should be delivered with the configured method - not the default.
user := createSampleUser(t, api.Database)
msgID, err := enq.Enqueue(ctx, user.ID, template, map[string]string{}, "test")
msgID, err := enq.Enqueue(ctx, user.ID, tmplate, map[string]string{}, "test")
require.NoError(t, err)

// THEN: the notification should be received by the custom dispatch method
Expand Down Expand Up @@ -1562,7 +1562,7 @@ type fakeHandler struct {
succeeded, failed []string
}

func (f *fakeHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
func (f *fakeHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
return func(_ context.Context, msgID uuid.UUID) (retryable bool, err error) {
f.mu.Lock()
defer f.mu.Unlock()
Expand Down Expand Up @@ -1629,15 +1629,13 @@ func TestNotificationTemplates_GoldenWithCustomAppearance(t *testing.T) {
hint = "run \"DB=ci make update-golden-files\" and commit the changes"
)

var (
payload = types.MessagePayload{
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "autodeleted due to dormancy",
"initiator": "autobuild",
},
}
)
payload := types.MessagePayload{
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "autodeleted due to dormancy",
"initiator": "autobuild",
},
}

// Spin up the DB
db, logger, user := func() (database.Store, *slog.Logger, *codersdk.User) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func newDispatchInterceptor(h notifications.Handler) *dispatchInterceptor {
return &dispatchInterceptor{handler: h}
}

func (i *dispatchInterceptor) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
func (i *dispatchInterceptor) Dispatcher(_ template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
return func(ctx context.Context, msgID uuid.UUID) (retryable bool, err error) {
deliveryFn, err := i.handler.Dispatcher(defaultHelpers(), payload, title, body)
if err != nil {
Expand Down
Loading