Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4c5cb3d

Browse files
committed
fix(notifications): improve tests and some nit fixes
1 parent a6d4a0c commit 4c5cb3d

File tree

12 files changed

+44
-32
lines changed

12 files changed

+44
-32
lines changed

coderd/notifications/dispatch/smtp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, log slog.Logger) *SMT
6161
return &SMTPHandler{cfg: cfg, log: log}
6262
}
6363

64-
func (s *SMTPHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
64+
func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string, helpers template.FuncMap) (DeliveryFunc, error) {
6565
// First render the subject & body into their own discrete strings.
6666
subject, err := markdown.PlaintextFromMarkdown(titleTmpl)
6767
if err != nil {

coderd/notifications/dispatch/smtp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func TestSMTP(t *testing.T) {
480480
Labels: make(map[string]string),
481481
}
482482

483-
dispatchFn, err := handler.Dispatcher(helpers, payload, subject, body)
483+
dispatchFn, err := handler.Dispatcher(payload, subject, body, helpers())
484484
require.NoError(t, err)
485485

486486
msgID := uuid.New()
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package dispatch_test
22

3-
var helpers = map[string]any{
4-
"base_url": func() string { return "http://test.com" },
5-
"current_year": func() string { return "2024" },
6-
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
7-
"app_name": func() string { return "Coder" },
3+
func helpers() map[string]any {
4+
return map[string]any{
5+
"base_url": func() string { return "http://test.com" },
6+
"current_year": func() string { return "2024" },
7+
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
8+
"app_name": func() string { return "Coder" },
9+
}
810
}

coderd/notifications/dispatch/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewWebhookHandler(cfg codersdk.NotificationsWebhookConfig, log slog.Logger)
4242
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
4343
}
4444

45-
func (w *WebhookHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
45+
func (w *WebhookHandler) Dispatcher(payload types.MessagePayload, titleMarkdown, bodyMarkdown string, _ template.FuncMap) (DeliveryFunc, error) {
4646
if w.cfg.Endpoint.String() == "" {
4747
return nil, xerrors.New("webhook endpoint not defined")
4848
}

coderd/notifications/dispatch/webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestWebhook(t *testing.T) {
141141
Endpoint: *serpent.URLOf(endpoint),
142142
}
143143
handler := dispatch.NewWebhookHandler(cfg, logger.With(slog.F("test", tc.name)))
144-
deliveryFn, err := handler.Dispatcher(helpers, msgPayload, titleMarkdown, bodyMarkdown)
144+
deliveryFn, err := handler.Dispatcher(msgPayload, titleMarkdown, bodyMarkdown, helpers())
145145
require.NoError(t, err)
146146

147147
retryable, err := deliveryFn(ctx, msgID)

coderd/notifications/fetcher.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,39 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"text/template"
78

89
"golang.org/x/xerrors"
910
)
1011

12+
func (n *notifier) fetchHelpers(ctx context.Context) (map[string]any, error) {
13+
appName, err := n.fetchAppName(ctx)
14+
if err != nil {
15+
return nil, xerrors.Errorf("fetch app name: %w", err)
16+
}
17+
logoURL, err := n.fetchLogoURL(ctx)
18+
if err != nil {
19+
return nil, xerrors.Errorf("fetch logo URL: %w", err)
20+
}
21+
22+
helpers := make(template.FuncMap)
23+
for k, v := range n.helpers {
24+
helpers[k] = v
25+
}
26+
27+
helpers["app_name"] = func() string { return appName }
28+
helpers["logo_url"] = func() string { return logoURL }
29+
30+
return helpers, nil
31+
}
32+
1133
func (n *notifier) fetchAppName(ctx context.Context) (string, error) {
1234
appName, err := n.store.GetApplicationName(ctx)
1335
if err != nil {
1436
if errors.Is(err, sql.ErrNoRows) {
1537
return notificationsDefaultAppName, nil
1638
}
17-
return "", xerrors.Errorf("get organization: %w", err)
39+
return "", xerrors.Errorf("get application name: %w", err)
1840
}
1941
return appName, nil
2042
}

coderd/notifications/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ type santaHandler struct {
206206
nice atomic.Int32
207207
}
208208

209-
func (s *santaHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
209+
func (s *santaHandler) Dispatcher(payload types.MessagePayload, _, _ string, _ template.FuncMap) (dispatch.DeliveryFunc, error) {
210210
return func(_ context.Context, _ uuid.UUID) (retryable bool, err error) {
211211
if payload.Labels["nice"] != "true" {
212212
s.naughty.Add(1)

coderd/notifications/metrics_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ func newBarrierHandler(total int, handler notifications.Handler) *barrierHandler
516516
}
517517
}
518518

519-
func (bh *barrierHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
520-
deliverFn, err := bh.h.Dispatcher(helpers, payload, title, body)
519+
func (bh *barrierHandler) Dispatcher(payload types.MessagePayload, title, body string, helpers template.FuncMap) (dispatch.DeliveryFunc, error) {
520+
deliverFn, err := bh.h.Dispatcher(payload, title, body, helpers)
521521
if err != nil {
522522
return nil, err
523523
}

coderd/notifications/notifications_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ type fakeHandler struct {
15941594
succeeded, failed []string
15951595
}
15961596

1597-
func (f *fakeHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
1597+
func (f *fakeHandler) Dispatcher(payload types.MessagePayload, _, _ string, _ template.FuncMap) (dispatch.DeliveryFunc, error) {
15981598
return func(_ context.Context, msgID uuid.UUID) (retryable bool, err error) {
15991599
f.mu.Lock()
16001600
defer f.mu.Unlock()

coderd/notifications/notifier.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,10 @@ func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotification
228228
return nil, xerrors.Errorf("failed to resolve handler %q", msg.Method)
229229
}
230230

231-
helpers := make(template.FuncMap)
232-
for k, v := range n.helpers {
233-
helpers[k] = v
234-
}
235-
236-
appName, err := n.fetchAppName(ctx)
231+
helpers, err := n.fetchHelpers(ctx)
237232
if err != nil {
238-
return nil, xerrors.Errorf("fetch app name: %w", err)
233+
return nil, xerrors.Errorf("fetch helpers: %w", err)
239234
}
240-
logoURL, err := n.fetchLogoURL(ctx)
241-
if err != nil {
242-
return nil, xerrors.Errorf("fetch logo URL: %w", err)
243-
}
244-
245-
helpers["app_name"] = func() string { return appName }
246-
helpers["logo_url"] = func() string { return logoURL }
247235

248236
var title, body string
249237
if title, err = render.GoTemplate(msg.TitleTemplate, payload, helpers); err != nil {
@@ -253,7 +241,7 @@ func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotification
253241
return nil, xerrors.Errorf("render body: %w", err)
254242
}
255243

256-
return handler.Dispatcher(helpers, payload, title, body)
244+
return handler.Dispatcher(payload, title, body, helpers)
257245
}
258246

259247
// deliver sends a given notification message via its defined method.

0 commit comments

Comments
 (0)