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
fix(notifications): improve tests and some nit fixes
  • Loading branch information
defelmnq committed Oct 18, 2024
commit 4c5cb3d94d6e68eb42d7515e4e1c1c38fad4653f
2 changes: 1 addition & 1 deletion coderd/notifications/dispatch/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, log slog.Logger) *SMT
return &SMTPHandler{cfg: cfg, log: log}
}

func (s *SMTPHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string, helpers template.FuncMap) (DeliveryFunc, error) {
// First render the subject & body into their own discrete strings.
subject, err := markdown.PlaintextFromMarkdown(titleTmpl)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/dispatch/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func TestSMTP(t *testing.T) {
Labels: make(map[string]string),
}

dispatchFn, err := handler.Dispatcher(helpers, payload, subject, body)
dispatchFn, err := handler.Dispatcher(payload, subject, body, helpers())
require.NoError(t, err)

msgID := uuid.New()
Expand Down
12 changes: 7 additions & 5 deletions coderd/notifications/dispatch/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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" },
func helpers() map[string]any {
return 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(_ template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
func (w *WebhookHandler) Dispatcher(payload types.MessagePayload, titleMarkdown, bodyMarkdown string, _ template.FuncMap) (DeliveryFunc, error) {
if w.cfg.Endpoint.String() == "" {
return nil, xerrors.New("webhook endpoint not defined")
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/dispatch/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestWebhook(t *testing.T) {
Endpoint: *serpent.URLOf(endpoint),
}
handler := dispatch.NewWebhookHandler(cfg, logger.With(slog.F("test", tc.name)))
deliveryFn, err := handler.Dispatcher(helpers, msgPayload, titleMarkdown, bodyMarkdown)
deliveryFn, err := handler.Dispatcher(msgPayload, titleMarkdown, bodyMarkdown, helpers())
require.NoError(t, err)

retryable, err := deliveryFn(ctx, msgID)
Expand Down
24 changes: 23 additions & 1 deletion coderd/notifications/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,39 @@ import (
"context"
"database/sql"
"errors"
"text/template"

"golang.org/x/xerrors"
)

func (n *notifier) fetchHelpers(ctx context.Context) (map[string]any, error) {
appName, err := n.fetchAppName(ctx)
if err != nil {
return nil, xerrors.Errorf("fetch app name: %w", err)
}
logoURL, err := n.fetchLogoURL(ctx)
if err != nil {
return nil, xerrors.Errorf("fetch logo URL: %w", err)
}

helpers := make(template.FuncMap)
for k, v := range n.helpers {
helpers[k] = v
}

helpers["app_name"] = func() string { return appName }
helpers["logo_url"] = func() string { return logoURL }

return helpers, nil
}

func (n *notifier) fetchAppName(ctx context.Context) (string, error) {
appName, err := n.store.GetApplicationName(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return notificationsDefaultAppName, nil
}
return "", xerrors.Errorf("get organization: %w", err)
return "", xerrors.Errorf("get application name: %w", err)
}
return appName, nil
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ type santaHandler struct {
nice atomic.Int32
}

func (s *santaHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
func (s *santaHandler) Dispatcher(payload types.MessagePayload, _, _ string, _ template.FuncMap) (dispatch.DeliveryFunc, error) {
return func(_ context.Context, _ uuid.UUID) (retryable bool, err error) {
if payload.Labels["nice"] != "true" {
s.naughty.Add(1)
Expand Down
4 changes: 2 additions & 2 deletions coderd/notifications/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func newBarrierHandler(total int, handler notifications.Handler) *barrierHandler
}
}

func (bh *barrierHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
deliverFn, err := bh.h.Dispatcher(helpers, payload, title, body)
func (bh *barrierHandler) Dispatcher(payload types.MessagePayload, title, body string, helpers template.FuncMap) (dispatch.DeliveryFunc, error) {
deliverFn, err := bh.h.Dispatcher(payload, title, body, helpers)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ type fakeHandler struct {
succeeded, failed []string
}

func (f *fakeHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
func (f *fakeHandler) Dispatcher(payload types.MessagePayload, _, _ string, _ template.FuncMap) (dispatch.DeliveryFunc, error) {
return func(_ context.Context, msgID uuid.UUID) (retryable bool, err error) {
f.mu.Lock()
defer f.mu.Unlock()
Expand Down
18 changes: 3 additions & 15 deletions coderd/notifications/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,10 @@ func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotification
return nil, xerrors.Errorf("failed to resolve handler %q", msg.Method)
}

helpers := make(template.FuncMap)
for k, v := range n.helpers {
helpers[k] = v
}

appName, err := n.fetchAppName(ctx)
helpers, err := n.fetchHelpers(ctx)
if err != nil {
return nil, xerrors.Errorf("fetch app name: %w", err)
return nil, xerrors.Errorf("fetch helpers: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you validated that failures here cause a "temporary failure" so that subsequent requests are retried?

}
logoURL, err := n.fetchLogoURL(ctx)
if err != nil {
return nil, xerrors.Errorf("fetch logo URL: %w", err)
}

helpers["app_name"] = func() string { return appName }
helpers["logo_url"] = func() string { return logoURL }

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

return handler.Dispatcher(helpers, payload, title, body)
return handler.Dispatcher(payload, title, body, helpers)
}

// deliver sends a given notification message via its defined method.
Expand Down
2 changes: 1 addition & 1 deletion coderd/notifications/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Store interface {
// Handler is responsible for preparing and delivering a notification by a given method.
type Handler interface {
// Dispatcher constructs a DeliveryFunc to be used for delivering a notification via the chosen method.
Dispatcher(helpers template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error)
Dispatcher(payload types.MessagePayload, title, body string, helpers template.FuncMap) (dispatch.DeliveryFunc, error)
}

// Enqueuer enqueues a new notification message in the store and returns its ID, should it enqueue without failure.
Expand Down
4 changes: 2 additions & 2 deletions coderd/notifications/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func newDispatchInterceptor(h notifications.Handler) *dispatchInterceptor {
return &dispatchInterceptor{handler: h}
}

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