Skip to content

Commit 34d6611

Browse files
author
defelmnq
committedOct 17, 2024
feat(notifications): change helpers logic to be defined in the Dispatcher function
1 parent 0c131a5 commit 34d6611

28 files changed

+295
-125
lines changed
 

‎coderd/notifications/dispatch/smtp.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ type SMTPHandler struct {
5555

5656
noAuthWarnOnce sync.Once
5757
loginWarnOnce sync.Once
58-
59-
helpers template.FuncMap
6058
}
6159

62-
func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, helpers template.FuncMap, log slog.Logger) *SMTPHandler {
63-
return &SMTPHandler{cfg: cfg, helpers: helpers, log: log}
60+
func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, log slog.Logger) *SMTPHandler {
61+
return &SMTPHandler{cfg: cfg, log: log}
6462
}
6563

66-
func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
64+
func (s *SMTPHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
6765
// First render the subject & body into their own discrete strings.
6866
subject, err := markdown.PlaintextFromMarkdown(titleTmpl)
6967
if err != nil {
@@ -79,12 +77,12 @@ func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTm
7977
// Then, reuse these strings in the HTML & plain body templates.
8078
payload.Labels["_subject"] = subject
8179
payload.Labels["_body"] = htmlBody
82-
htmlBody, err = render.GoTemplate(htmlTemplate, payload, s.helpers)
80+
htmlBody, err = render.GoTemplate(htmlTemplate, payload, helpers)
8381
if err != nil {
8482
return nil, xerrors.Errorf("render full html template: %w", err)
8583
}
8684
payload.Labels["_body"] = plainBody
87-
plainBody, err = render.GoTemplate(plainTemplate, payload, s.helpers)
85+
plainBody, err = render.GoTemplate(plainTemplate, payload, helpers)
8886
if err != nil {
8987
return nil, xerrors.Errorf("render full plaintext template: %w", err)
9088
}

‎coderd/notifications/dispatch/smtp_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,7 @@ func TestSMTP(t *testing.T) {
442442
require.NoError(t, hp.Set(listen.Addr().String()))
443443
tc.cfg.Smarthost = hp
444444

445-
helpers := map[string]any{
446-
"base_url": func() string { return "http://test.com" },
447-
"current_year": func() string { return "2024" },
448-
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
449-
"app_name": func() string { return "Coder" },
450-
}
451-
handler := dispatch.NewSMTPHandler(tc.cfg, helpers, logger.Named("smtp"))
445+
handler := dispatch.NewSMTPHandler(tc.cfg, logger.Named("smtp"))
452446

453447
// Start mock SMTP server in the background.
454448
var wg sync.WaitGroup
@@ -486,7 +480,7 @@ func TestSMTP(t *testing.T) {
486480
Labels: make(map[string]string),
487481
}
488482

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

492486
msgID := uuid.New()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dispatch_test
2+
3+
var (
4+
helpers = 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+
}
10+
)

‎coderd/notifications/dispatch/webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"io"
99
"net/http"
10+
"text/template"
1011

1112
"github.com/google/uuid"
1213
"golang.org/x/xerrors"
@@ -41,7 +42,7 @@ func NewWebhookHandler(cfg codersdk.NotificationsWebhookConfig, log slog.Logger)
4142
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
4243
}
4344

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

‎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(msgPayload, titleMarkdown, bodyMarkdown)
144+
deliveryFn, err := handler.Dispatcher(helpers, msgPayload, titleMarkdown, bodyMarkdown)
145145
require.NoError(t, err)
146146

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

‎coderd/notifications/fetcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"golang.org/x/xerrors"
99
)
1010

11-
func (n *notifier) FetchAppName(ctx context.Context) (string, error) {
11+
func (n *notifier) fetchAppName(ctx context.Context) (string, error) {
1212
appName, err := n.store.GetApplicationName(ctx)
1313
if err != nil {
1414
if errors.Is(err, sql.ErrNoRows) {
@@ -19,7 +19,7 @@ func (n *notifier) FetchAppName(ctx context.Context) (string, error) {
1919
return appName, nil
2020
}
2121

22-
func (n *notifier) FetchLogoURL(ctx context.Context) (string, error) {
22+
func (n *notifier) fetchLogoURL(ctx context.Context) (string, error) {
2323
logoURL, err := n.store.GetLogoURL(ctx)
2424
if err != nil {
2525
if errors.Is(err, sql.ErrNoRows) {

‎coderd/notifications/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func NewManager(cfg codersdk.NotificationsConfig, store Store, helpers template.
109109
stop: make(chan any),
110110
done: make(chan any),
111111

112-
handlers: defaultHandlers(cfg, helpers, log),
112+
handlers: defaultHandlers(cfg, log),
113113
helpers: helpers,
114114

115115
clock: quartz.NewReal(),
@@ -121,9 +121,9 @@ func NewManager(cfg codersdk.NotificationsConfig, store Store, helpers template.
121121
}
122122

123123
// defaultHandlers builds a set of known handlers; panics if any error occurs as these handlers should be valid at compile time.
124-
func defaultHandlers(cfg codersdk.NotificationsConfig, helpers template.FuncMap, log slog.Logger) map[database.NotificationMethod]Handler {
124+
func defaultHandlers(cfg codersdk.NotificationsConfig, log slog.Logger) map[database.NotificationMethod]Handler {
125125
return map[database.NotificationMethod]Handler{
126-
database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, helpers, log.Named("dispatcher.smtp")),
126+
database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, log.Named("dispatcher.smtp")),
127127
database.NotificationMethodWebhook: dispatch.NewWebhookHandler(cfg.Webhook, log.Named("dispatcher.webhook")),
128128
}
129129
}

‎coderd/notifications/manager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"sync/atomic"
77
"testing"
8+
"text/template"
89
"time"
910

1011
"github.com/google/uuid"
@@ -205,7 +206,7 @@ type santaHandler struct {
205206
nice atomic.Int32
206207
}
207208

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

‎coderd/notifications/metrics_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"sync"
77
"testing"
8+
"text/template"
89
"time"
910

1011
"github.com/google/uuid"
@@ -515,8 +516,8 @@ func newBarrierHandler(total int, handler notifications.Handler) *barrierHandler
515516
}
516517
}
517518

518-
func (bh *barrierHandler) Dispatcher(payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
519-
deliverFn, err := bh.h.Dispatcher(payload, title, body)
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)
520521
if err != nil {
521522
return nil, err
522523
}

0 commit comments

Comments
 (0)