From 898014c8a650390b403e75e7fea64a0c5b689705 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Wed, 7 Aug 2024 16:09:14 +0000 Subject: [PATCH 1/8] Update template email --- cli/server.go | 3 +- coderd/notifications/dispatch/smtp.go | 11 +- .../notifications/dispatch/smtp/html.gotmpl | 107 ++++++++++++++---- coderd/notifications/manager.go | 8 +- 4 files changed, 100 insertions(+), 29 deletions(-) diff --git a/cli/server.go b/cli/server.go index f76872a78c342..74cd0c5aed472 100644 --- a/cli/server.go +++ b/cli/server.go @@ -992,6 +992,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. ) if experiments.Enabled(codersdk.ExperimentNotifications) { cfg := options.DeploymentValues.Notifications + accessUrl := options.DeploymentValues.AccessURL.Value().String() metrics := notifications.NewMetrics(options.PrometheusRegistry) // The enqueuer is responsible for enqueueing notifications to the given store. @@ -1004,7 +1005,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. // The notification manager is responsible for: // - creating notifiers and managing their lifecycles (notifiers are responsible for dequeueing/sending notifications) // - keeping the store updated with status updates - notificationsManager, err = notifications.NewManager(cfg, options.Database, metrics, logger.Named("notifications.manager")) + notificationsManager, err = notifications.NewManager(cfg, options.Database, accessUrl, metrics, logger.Named("notifications.manager")) if err != nil { return xerrors.Errorf("failed to instantiate notification manager: %w", err) } diff --git a/coderd/notifications/dispatch/smtp.go b/coderd/notifications/dispatch/smtp.go index 218668e65d02e..f7048ac6f0861 100644 --- a/coderd/notifications/dispatch/smtp.go +++ b/coderd/notifications/dispatch/smtp.go @@ -49,14 +49,15 @@ var ( // NOTE: auth and TLS is currently *not* enabled in this initial thin slice. // TODO: implement DKIM/SPF/DMARC? https://github.com/emersion/go-msgauth type SMTPHandler struct { - cfg codersdk.NotificationsEmailConfig - log slog.Logger + cfg codersdk.NotificationsEmailConfig + log slog.Logger + accessURL string loginWarnOnce sync.Once } -func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, log slog.Logger) *SMTPHandler { - return &SMTPHandler{cfg: cfg, log: log} +func NewSMTPHandler(cfg codersdk.NotificationsEmailConfig, accessURL string, log slog.Logger) *SMTPHandler { + return &SMTPHandler{cfg: cfg, accessURL: accessURL, log: log} } func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string) (DeliveryFunc, error) { @@ -75,6 +76,8 @@ 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 + payload.Labels["_year"] = time.Now().Format("2006") + payload.Labels["_accessURL"] = s.accessURL htmlBody, err = render.GoTemplate(htmlTemplate, payload, nil) if err != nil { return nil, xerrors.Errorf("render full html template: %w", err) diff --git a/coderd/notifications/dispatch/smtp/html.gotmpl b/coderd/notifications/dispatch/smtp/html.gotmpl index 00005179316bf..9b1eac52233cb 100644 --- a/coderd/notifications/dispatch/smtp/html.gotmpl +++ b/coderd/notifications/dispatch/smtp/html.gotmpl @@ -1,27 +1,94 @@ - + - - - + + + {{ .Labels._subject }} - - -
-
- -
-
-

{{ .Labels._subject }}

- {{ .Labels._body }} + + + +
+ +

{{ .Labels._subject }}

+ {{ .Labels._body }} + +
{{ range $action := .Actions }} - {{ $action.Label }}
+ {{ $action.Label }} {{ end }} -
-
+
+ +
-
- - \ No newline at end of file + + diff --git a/coderd/notifications/manager.go b/coderd/notifications/manager.go index 91580d5fc4fb7..659d19b016049 100644 --- a/coderd/notifications/manager.go +++ b/coderd/notifications/manager.go @@ -59,7 +59,7 @@ type Manager struct { // // helpers is a map of template helpers which are used to customize notification messages to use global settings like // access URL etc. -func NewManager(cfg codersdk.NotificationsConfig, store Store, metrics *Metrics, log slog.Logger) (*Manager, error) { +func NewManager(cfg codersdk.NotificationsConfig, store Store, host string, metrics *Metrics, log slog.Logger) (*Manager, error) { // TODO(dannyk): add the ability to use multiple notification methods. var method database.NotificationMethod if err := method.Scan(cfg.Method.String()); err != nil { @@ -93,14 +93,14 @@ func NewManager(cfg codersdk.NotificationsConfig, store Store, metrics *Metrics, stop: make(chan any), done: make(chan any), - handlers: defaultHandlers(cfg, log), + handlers: defaultHandlers(cfg, host, log), }, nil } // 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, log slog.Logger) map[database.NotificationMethod]Handler { +func defaultHandlers(cfg codersdk.NotificationsConfig, accessURL string, log slog.Logger) map[database.NotificationMethod]Handler { return map[database.NotificationMethod]Handler{ - database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, log.Named("dispatcher.smtp")), + database.NotificationMethodSmtp: dispatch.NewSMTPHandler(cfg.SMTP, accessURL, log.Named("dispatcher.smtp")), database.NotificationMethodWebhook: dispatch.NewWebhookHandler(cfg.Webhook, log.Named("dispatcher.webhook")), } } From a17381f76c4ab95574498d9c659cafacfa7601c1 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Wed, 7 Aug 2024 17:09:05 +0000 Subject: [PATCH 2/8] Pass extra data to template --- .../notifications/dispatch/smtp/html.gotmpl | 13 +++++-- coderd/notifications/dispatch/smtp_test.go | 3 +- coderd/notifications/manager_test.go | 12 +++++-- coderd/notifications/metrics_test.go | 12 ++++--- coderd/notifications/notifications_test.go | 34 ++++++++++++------- 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/coderd/notifications/dispatch/smtp/html.gotmpl b/coderd/notifications/dispatch/smtp/html.gotmpl index 9b1eac52233cb..57ee3bc608894 100644 --- a/coderd/notifications/dispatch/smtp/html.gotmpl +++ b/coderd/notifications/dispatch/smtp/html.gotmpl @@ -26,6 +26,8 @@ .logo { width: 60px; + display: block; + margin: auto; } .title { @@ -82,9 +84,16 @@