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): change helpers logic to be defined in the Dispat…
…cher function
  • Loading branch information
defelmnq committed Oct 17, 2024
commit 34d661187959b1cc13aef5d92cda88206b047da4
12 changes: 5 additions & 7 deletions coderd/notifications/dispatch/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ type SMTPHandler struct {

noAuthWarnOnce sync.Once
loginWarnOnce sync.Once

helpers template.FuncMap
}

func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, helpers template.FuncMap, log slog.Logger) *SMTPHandler {
return &SMTPHandler{cfg: cfg, helpers: helpers, log: log}
func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, log slog.Logger) *SMTPHandler {
return &SMTPHandler{cfg: cfg, log: log}
}

func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
func (s *SMTPHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: when passing arguments, I try think of them in order of significance.
The helpers are not the most significant argument here; they're likely the least. I'd move helpers to the end.

// First render the subject & body into their own discrete strings.
subject, err := markdown.PlaintextFromMarkdown(titleTmpl)
if err != nil {
Expand All @@ -79,12 +77,12 @@ func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTm
// Then, reuse these strings in the HTML & plain body templates.
payload.Labels["_subject"] = subject
payload.Labels["_body"] = htmlBody
htmlBody, err = render.GoTemplate(htmlTemplate, payload, s.helpers)
htmlBody, err = render.GoTemplate(htmlTemplate, payload, helpers)
if err != nil {
return nil, xerrors.Errorf("render full html template: %w", err)
}
payload.Labels["_body"] = plainBody
plainBody, err = render.GoTemplate(plainTemplate, payload, s.helpers)
plainBody, err = render.GoTemplate(plainTemplate, payload, helpers)
if err != nil {
return nil, xerrors.Errorf("render full plaintext template: %w", err)
}
Expand Down
10 changes: 2 additions & 8 deletions coderd/notifications/dispatch/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,7 @@ func TestSMTP(t *testing.T) {
require.NoError(t, hp.Set(listen.Addr().String()))
tc.cfg.Smarthost = hp

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" },
}
handler := dispatch.NewSMTPHandler(tc.cfg, helpers, logger.Named("smtp"))
handler := dispatch.NewSMTPHandler(tc.cfg, logger.Named("smtp"))

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

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

msgID := uuid.New()
Expand Down
10 changes: 10 additions & 0 deletions coderd/notifications/dispatch/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +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" },
}
)
3 changes: 2 additions & 1 deletion coderd/notifications/dispatch/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"errors"
"io"
"net/http"
"text/template"

"github.com/google/uuid"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -41,7 +42,7 @@
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
}

func (w *WebhookHandler) Dispatcher(payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
func (w *WebhookHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {

Check failure on line 45 in coderd/notifications/dispatch/webhook.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'helpers' seems to be unused, consider removing or renaming it as _ (revive)
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(msgPayload, titleMarkdown, bodyMarkdown)
deliveryFn, err := handler.Dispatcher(helpers, msgPayload, titleMarkdown, bodyMarkdown)
require.NoError(t, err)

retryable, err := deliveryFn(ctx, msgID)
Expand Down
4 changes: 2 additions & 2 deletions coderd/notifications/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"golang.org/x/xerrors"
)

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

func (n *notifier) FetchLogoURL(ctx context.Context) (string, error) {
func (n *notifier) fetchLogoURL(ctx context.Context) (string, error) {
logoURL, err := n.store.GetLogoURL(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down
6 changes: 3 additions & 3 deletions coderd/notifications/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewManager(cfg codersdk.NotificationsConfig, store Store, helpers template.
stop: make(chan any),
done: make(chan any),

handlers: defaultHandlers(cfg, helpers, log),
handlers: defaultHandlers(cfg, log),
helpers: helpers,

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

// defaultHandlers builds a set of known handlers; panics if any error occurs as these handlers should be valid at compile time.
func defaultHandlers(cfg codersdk.NotificationsConfig, helpers template.FuncMap, log slog.Logger) map[database.NotificationMethod]Handler {
func defaultHandlers(cfg codersdk.NotificationsConfig, log slog.Logger) map[database.NotificationMethod]Handler {
return map[database.NotificationMethod]Handler{
database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, helpers, log.Named("dispatcher.smtp")),
database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, log.Named("dispatcher.smtp")),
database.NotificationMethodWebhook: dispatch.NewWebhookHandler(cfg.Webhook, log.Named("dispatcher.webhook")),
}
}
Expand Down
3 changes: 2 additions & 1 deletion coderd/notifications/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"encoding/json"
"sync/atomic"
"testing"
"text/template"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -205,7 +206,7 @@
nice atomic.Int32
}

func (s *santaHandler) Dispatcher(payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
func (s *santaHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {

Check failure on line 209 in coderd/notifications/manager_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'helpers' seems to be unused, consider removing or renaming it as _ (revive)
return func(ctx context.Context, msgID uuid.UUID) (retryable bool, err error) {
if payload.Labels["nice"] != "true" {
s.naughty.Add(1)
Expand Down
5 changes: 3 additions & 2 deletions coderd/notifications/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"strconv"
"sync"
"testing"
"text/template"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -43,7 +44,7 @@

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

Check failure on line 47 in coderd/notifications/metrics_test.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'template' shadows an import name (revive)

const (
method = database.NotificationMethodSmtp
Expand Down Expand Up @@ -216,7 +217,7 @@

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

Check failure on line 220 in coderd/notifications/metrics_test.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'template' shadows an import name (revive)

const method = database.NotificationMethodSmtp

Expand Down Expand Up @@ -300,7 +301,7 @@

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

Check failure on line 304 in coderd/notifications/metrics_test.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'template' shadows an import name (revive)

const method = database.NotificationMethodSmtp

Expand Down Expand Up @@ -379,7 +380,7 @@
var (
reg = prometheus.NewRegistry()
metrics = notifications.NewMetrics(reg)
template = notifications.TemplateWorkspaceDeleted

Check failure on line 383 in coderd/notifications/metrics_test.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'template' shadows an import name (revive)
anotherTemplate = notifications.TemplateWorkspaceDormant
)

Expand Down Expand Up @@ -515,8 +516,8 @@
}
}

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