- Notifications
You must be signed in to change notification settings - Fork1k
feat: notify users on template deprecation#15195
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
2b236e8
feat: notify users on template deprecation
DanielleMaywood640138a
test: test behaviour
DanielleMaywood22ec59d
fix: ci
DanielleMaywoodcd97285
feat: add CTAs
DanielleMaywood5732b26
test: add rendered-template test
DanielleMaywooda971712
test: ensure notification goes to correct users
DanielleMaywood800adad
refactor: only notify users of template
DanielleMaywoodc63dbff
test: create another template+workspace
DanielleMaywood468ba87
fix: remove unused test
DanielleMaywoodc1e0fb2
chore: rename 'See workspaces' to 'See affected workspaces'
DanielleMaywoodb210680
fix: update golden files
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionscoderd/database/migrations/000270_template_deprecation_notification.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DELETE FROM notification_templates WHERE id = 'f40fae84-55a2-42cd-99fa-b41c1ca64894'; |
22 changes: 22 additions & 0 deletionscoderd/database/migrations/000270_template_deprecation_notification.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
INSERT INTO notification_templates | ||
(id, name, title_template, body_template, "group", actions) | ||
VALUES ( | ||
'f40fae84-55a2-42cd-99fa-b41c1ca64894', | ||
'Template Deprecated', | ||
E'Template ''{{.Labels.template}}'' has been deprecated', | ||
E'Hello {{.UserName}},\n\n'|| | ||
E'The template **{{.Labels.template}}** has been deprecated with the following message:\n\n' || | ||
E'**{{.Labels.message}}**\n\n' || | ||
E'New workspaces may not be created from this template. Existing workspaces will continue to function normally.', | ||
'Template Events', | ||
'[ | ||
{ | ||
"label": "See affected workspaces", | ||
"url": "{{base_url}}/workspaces?filter=owner%3Ame+template%3A{{.Labels.template}}" | ||
}, | ||
{ | ||
"label": "View template", | ||
"url": "{{base_url}}/templates/{{.Labels.organization}}/{{.Labels.template}}" | ||
} | ||
]'::jsonb | ||
); |
3 changes: 2 additions & 1 deletioncoderd/notifications/events.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletionscoderd/notifications/notifications_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletionscoderd/notifications/testdata/rendered-templates/smtp/TemplateTemplateDeprecated.html.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
From: system@coder.com | ||
To: bobby@coder.com | ||
Subject: Template 'alpha' has been deprecated | ||
Message-Id: 02ee4935-73be-4fa1-a290-ff9999026b13@blush-whale-48 | ||
Date: Fri, 11 Oct 2024 09:03:06 +0000 | ||
Content-Type: multipart/alternative; boundary=bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
MIME-Version: 1.0 | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
Content-Transfer-Encoding: quoted-printable | ||
Content-Type: text/plain; charset=UTF-8 | ||
Hello Bobby, | ||
The template alpha has been deprecated with the following message: | ||
This template has been replaced by beta | ||
New workspaces may not be created from this template. Existing workspaces w= | ||
ill continue to function normally. | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
See affected workspaces: http://test.com/workspaces?filter=3Downer%3Ame+tem= | ||
plate%3Aalpha | ||
View template: http://test.com/templates/coder/alpha | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
Content-Transfer-Encoding: quoted-printable | ||
Content-Type: text/html; charset=UTF-8 | ||
<!doctype html> | ||
<html lang=3D"en"> | ||
<head> | ||
<meta charset=3D"UTF-8" /> | ||
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale= | ||
=3D1.0" /> | ||
<title>Template 'alpha' has been deprecated</title> | ||
</head> | ||
<body style=3D"margin: 0; padding: 0; font-family: -apple-system, system-= | ||
ui, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarel= | ||
l', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; color: #020617= | ||
; background: #f8fafc;"> | ||
<div style=3D"max-width: 600px; margin: 20px auto; padding: 60px; borde= | ||
r: 1px solid #e2e8f0; border-radius: 8px; background-color: #fff; text-alig= | ||
n: left; font-size: 14px; line-height: 1.5;"> | ||
<div style=3D"text-align: center;"> | ||
<img src=3D"https://coder.com/coder-logo-horizontal.png" alt=3D"Cod= | ||
er Logo" style=3D"height: 40px;" /> | ||
</div> | ||
<h1 style=3D"text-align: center; font-size: 24px; font-weight: 400; m= | ||
argin: 8px 0 32px; line-height: 1.5;"> | ||
Template 'alpha' has been deprecated | ||
</h1> | ||
<div style=3D"line-height: 1.5;"> | ||
<p>Hello Bobby,</p> | ||
<p>The template <strong>alpha</strong> has been deprecated with the followi= | ||
ng message:</p> | ||
<p><strong>This template has been replaced by beta</strong></p> | ||
<p>New workspaces may not be created from this template. Existing workspace= | ||
s will continue to function normally.</p> | ||
</div> | ||
<div style=3D"text-align: center; margin-top: 32px;"> | ||
=20 | ||
<a href=3D"http://test.com/workspaces?filter=3Downer%3Ame+template%= | ||
3Aalpha" style=3D"display: inline-block; padding: 13px 24px; background-col= | ||
or: #020617; color: #f8fafc; text-decoration: none; border-radius: 8px; mar= | ||
gin: 0 4px;"> | ||
See affected workspaces | ||
</a> | ||
=20 | ||
<a href=3D"http://test.com/templates/coder/alpha" style=3D"display:= | ||
inline-block; padding: 13px 24px; background-color: #020617; color: #f8faf= | ||
c; text-decoration: none; border-radius: 8px; margin: 0 4px;"> | ||
View template | ||
</a> | ||
=20 | ||
</div> | ||
<div style=3D"border-top: 1px solid #e2e8f0; color: #475569; font-siz= | ||
e: 12px; margin-top: 64px; padding-top: 24px; line-height: 1.6;"> | ||
<p>© 2024 Coder. All rights reserved - <a = | ||
href=3D"http://test.com" style=3D"color: #2563eb; text-decoration: none;">h= | ||
ttp://test.com</a></p> | ||
<p><a href=3D"http://test.com/settings/notifications" style=3D"colo= | ||
r: #2563eb; text-decoration: none;">Click here to manage your notification = | ||
settings</a></p> | ||
<p><a href=3D"http://test.com/settings/notifications?disabled=3Df40= | ||
fae84-55a2-42cd-99fa-b41c1ca64894" style=3D"color: #2563eb; text-decoration= | ||
: none;">Stop receiving emails like this</a></p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4-- |
33 changes: 33 additions & 0 deletions.../notifications/testdata/rendered-templates/webhook/TemplateTemplateDeprecated.json.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"_version": "1.1", | ||
"msg_id": "00000000-0000-0000-0000-000000000000", | ||
"payload": { | ||
"_version": "1.1", | ||
"notification_name": "Template Deprecated", | ||
"notification_template_id": "00000000-0000-0000-0000-000000000000", | ||
"user_id": "00000000-0000-0000-0000-000000000000", | ||
"user_email": "bobby@coder.com", | ||
"user_name": "Bobby", | ||
"user_username": "bobby", | ||
"actions": [ | ||
{ | ||
"label": "See affected workspaces", | ||
"url": "http://test.com/workspaces?filter=owner%3Ame+template%3Aalpha" | ||
}, | ||
{ | ||
"label": "View template", | ||
"url": "http://test.com/templates/coder/alpha" | ||
} | ||
], | ||
"labels": { | ||
"message": "This template has been replaced by beta", | ||
"organization": "coder", | ||
"template": "alpha" | ||
}, | ||
"data": null | ||
}, | ||
"title": "Template 'alpha' has been deprecated", | ||
"title_markdown": "Template 'alpha' has been deprecated", | ||
"body": "Hello Bobby,\n\nThe template alpha has been deprecated with the following message:\n\nThis template has been replaced by beta\n\nNew workspaces may not be created from this template. Existing workspaces will continue to function normally.", | ||
"body_markdown": "Hello Bobby,\n\nThe template **alpha** has been deprecated with the following message:\n\n**This template has been replaced by beta**\n\nNew workspaces may not be created from this template. Existing workspaces will continue to function normally." | ||
} |
42 changes: 42 additions & 0 deletionscoderd/templates.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -845,6 +845,12 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
if template.Deprecated != updated.Deprecated && updated.Deprecated != "" { | ||
if err := api.notifyUsersOfTemplateDeprecation(ctx, updated); err != nil { | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
api.Logger.Error(ctx, "failed to notify users of template deprecation", slog.Error(err)) | ||
} | ||
} | ||
if updated.UpdatedAt.IsZero() { | ||
aReq.New = template | ||
rw.WriteHeader(http.StatusNotModified) | ||
@@ -855,6 +861,42 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(updated)) | ||
} | ||
func (api *API) notifyUsersOfTemplateDeprecation(ctx context.Context, template database.Template) error { | ||
workspaces, err := api.Database.GetWorkspaces(ctx, database.GetWorkspacesParams{ | ||
TemplateIDs: []uuid.UUID{template.ID}, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("get workspaces by template id: %w", err) | ||
} | ||
users := make(map[uuid.UUID]struct{}) | ||
for _, workspace := range workspaces { | ||
users[workspace.OwnerID] = struct{}{} | ||
} | ||
errs := []error{} | ||
for userID := range users { | ||
_, err = api.NotificationsEnqueuer.Enqueue( | ||
//nolint:gocritic // We need the system auth context to be able to send the deprecation notification. | ||
dbauthz.AsSystemRestricted(ctx), | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
userID, | ||
notifications.TemplateTemplateDeprecated, | ||
map[string]string{ | ||
"template": template.Name, | ||
"message": template.Deprecated, | ||
"organization": template.OrganizationName, | ||
}, | ||
"notify-users-of-template-deprecation", | ||
) | ||
if err != nil { | ||
errs = append(errs, xerrors.Errorf("enqueue notification: %w", err)) | ||
} | ||
} | ||
return errors.Join(errs...) | ||
} | ||
// @Summary Get template DAUs by ID | ||
// @ID get-template-daus-by-id | ||
// @Security CoderSessionToken | ||
44 changes: 43 additions & 1 deletionenterprise/coderd/templates_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.