From 6616ddc2573488d72d987c112a88bd6f2486dd0f Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Mon, 14 Oct 2024 19:12:30 +0000 Subject: [PATCH 1/3] fix(coderd/notifications): use template.Name as a fallback if template.DisplayName is an empty string --- ...uild_failed_notification_template.down.sql | 9 ++++++++ ..._build_failed_notification_template.up.sql | 8 +++++++ coderd/notifications/notifications_test.go | 1 - ...lateWorkspaceManualBuildFailed.html.golden | 6 ++---- ...lateWorkspaceManualBuildFailed.json.golden | 5 ++--- .../provisionerdserver/provisionerdserver.go | 21 +++++++++++-------- 6 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql create mode 100644 coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql diff --git a/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql b/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql new file mode 100644 index 0000000000000..64ced8ab9ba26 --- /dev/null +++ b/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql @@ -0,0 +1,9 @@ +UPDATE notification_templates +SET + body_template = E'Hi {{.UserName}},\n\n' || + E'A manual build of the workspace **{{.Labels.name}}** using the template **{{.Labels.template_name}}** failed (version: **{{.Labels.template_version_name}}**).\n\n' || + -- Mention template display name: + E'The template''s display name was **{{.Labels.template_display_name}}**. ' || + E'The workspace build was initiated by **{{.Labels.initiator}}**.' +WHERE + id = '2faeee0f-26cb-4e96-821c-85ccb9f71513'; diff --git a/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql b/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql new file mode 100644 index 0000000000000..a9088976c7a66 --- /dev/null +++ b/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql @@ -0,0 +1,8 @@ +UPDATE notification_templates +SET + body_template = E'Hi {{.UserName}},\n\n' || + -- Revert to a single label for the template name.: + E'A manual build of the workspace **{{.Labels.name}}** using the template **{{.Labels.template_name}}** failed (version: **{{.Labels.template_version_name}}**).\n\n' || + E'The workspace build was initiated by **{{.Labels.initiator}}**.' +WHERE + id = '2faeee0f-26cb-4e96-821c-85ccb9f71513'; diff --git a/coderd/notifications/notifications_test.go b/coderd/notifications/notifications_test.go index d6eb480f75c9b..4d1af42654669 100644 --- a/coderd/notifications/notifications_test.go +++ b/coderd/notifications/notifications_test.go @@ -886,7 +886,6 @@ func TestNotificationTemplates_Golden(t *testing.T) { Labels: map[string]string{ "name": "bobby-workspace", "template_name": "bobby-template", - "template_display_name": "William's Template", "template_version_name": "bobby-template-version", "initiator": "joe", "workspace_owner_username": "mrbobby", diff --git a/coderd/notifications/testdata/rendered-templates/smtp/TemplateWorkspaceManualBuildFailed.html.golden b/coderd/notifications/testdata/rendered-templates/smtp/TemplateWorkspaceManualBuildFailed.html.golden index a1711b70eae85..1f456a72f4df4 100644 --- a/coderd/notifications/testdata/rendered-templates/smtp/TemplateWorkspaceManualBuildFailed.html.golden +++ b/coderd/notifications/testdata/rendered-templates/smtp/TemplateWorkspaceManualBuildFailed.html.golden @@ -15,8 +15,7 @@ Hi Bobby, A manual build of the workspace bobby-workspace using the template bobby-te= mplate failed (version: bobby-template-version). -The template's display name was William's Template. The workspace build was= - initiated by joe. +The workspace build was initiated by joe. View build: http://test.com/@mrbobby/bobby-workspace/builds/3 @@ -55,8 +54,7 @@ argin: 8px 0 32px; line-height: 1.5;"> he template bobby-template failed (version: bobby-= template-version).

-

The template’s display name was William’s Template. The workspace build was initiated by joe.

+

The workspace build was initiated by joe.

=20 diff --git a/coderd/notifications/testdata/rendered-templates/webhook/TemplateWorkspaceManualBuildFailed.json.golden b/coderd/notifications/testdata/rendered-templates/webhook/TemplateWorkspaceManualBuildFailed.json.golden index 8fe2b8b86fd03..6b406a1928a70 100644 --- a/coderd/notifications/testdata/rendered-templates/webhook/TemplateWorkspaceManualBuildFailed.json.golden +++ b/coderd/notifications/testdata/rendered-templates/webhook/TemplateWorkspaceManualBuildFailed.json.golden @@ -18,7 +18,6 @@ "labels": { "initiator": "joe", "name": "bobby-workspace", - "template_display_name": "William's Template", "template_name": "bobby-template", "template_version_name": "bobby-template-version", "workspace_build_number": "3", @@ -28,6 +27,6 @@ }, "title": "Workspace \"bobby-workspace\" manual build failed", "title_markdown": "Workspace \"bobby-workspace\" manual build failed", - "body": "Hi Bobby,\n\nA manual build of the workspace bobby-workspace using the template bobby-template failed (version: bobby-template-version).\n\nThe template's display name was William's Template. The workspace build was initiated by joe.", - "body_markdown": "Hi Bobby,\n\nA manual build of the workspace **bobby-workspace** using the template **bobby-template** failed (version: **bobby-template-version**).\n\nThe template's display name was **William's Template**. The workspace build was initiated by **joe**." + "body": "Hi Bobby,\n\nA manual build of the workspace bobby-workspace using the template bobby-template failed (version: bobby-template-version).\n\nThe workspace build was initiated by joe.", + "body_markdown": "Hi Bobby,\n\nA manual build of the workspace **bobby-workspace** using the template **bobby-template** failed (version: **bobby-template-version**).\n\nThe workspace build was initiated by **joe**." } \ No newline at end of file diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index 0fc89b239a8d4..6f364b1567a26 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -1124,16 +1124,19 @@ func (s *server) notifyWorkspaceManualBuildFailed(ctx context.Context, workspace } for _, templateAdmin := range templateAdmins { + labels := map[string]string{ + "name": workspace.Name, + "template_name": template.DisplayName, + "template_version_name": templateVersion.Name, + "initiator": build.InitiatorByUsername, + "workspace_owner_username": workspaceOwner.Username, + "workspace_build_number": strconv.Itoa(int(build.BuildNumber)), + } + if template.DisplayName == "" { + labels["template_name"] = template.Name + } if _, err := s.NotificationsEnqueuer.Enqueue(ctx, templateAdmin.ID, notifications.TemplateWorkspaceManualBuildFailed, - map[string]string{ - "name": workspace.Name, - "template_name": template.Name, - "template_display_name": template.DisplayName, - "template_version_name": templateVersion.Name, - "initiator": build.InitiatorByUsername, - "workspace_owner_username": workspaceOwner.Username, - "workspace_build_number": strconv.Itoa(int(build.BuildNumber)), - }, "provisionerdserver", + labels, "provisionerdserver", // Associate this notification with all the related entities. workspace.ID, workspace.OwnerID, workspace.TemplateID, workspace.OrganizationID, ); err != nil { From 02967d373c21fa2aee2ce1c14ab475de1d1faaf2 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 15 Oct 2024 05:40:50 +0000 Subject: [PATCH 2/3] chore(coderd/provisionerdserver): fix TestNotifications --- coderd/provisionerdserver/provisionerdserver_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/coderd/provisionerdserver/provisionerdserver_test.go b/coderd/provisionerdserver/provisionerdserver_test.go index 32ae2efa648a0..c6c5613f97a35 100644 --- a/coderd/provisionerdserver/provisionerdserver_test.go +++ b/coderd/provisionerdserver/provisionerdserver_test.go @@ -1859,8 +1859,7 @@ func TestNotifications(t *testing.T) { assert.Contains(t, notifEnq.Sent[0].Targets, workspace.OrganizationID) assert.Contains(t, notifEnq.Sent[0].Targets, user.ID) assert.Equal(t, workspace.Name, notifEnq.Sent[0].Labels["name"]) - assert.Equal(t, template.Name, notifEnq.Sent[0].Labels["template_name"]) - assert.Equal(t, template.DisplayName, notifEnq.Sent[0].Labels["template_display_name"]) + assert.Equal(t, template.DisplayName, notifEnq.Sent[0].Labels["template_name"]) assert.Equal(t, version.Name, notifEnq.Sent[0].Labels["template_version_name"]) assert.Equal(t, user.Username, notifEnq.Sent[0].Labels["initiator"]) assert.Equal(t, user.Username, notifEnq.Sent[0].Labels["workspace_owner_username"]) From 89bed47afdd05ffba7ab45dbebdcb64c5cfde551 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 15 Oct 2024 15:37:35 +0000 Subject: [PATCH 3/3] fix(coderd/notifications): improve TemplateDeleted notification wording --- ..._build_failed_notification_template.down.sql | 9 +++++++++ ...al_build_failed_notification_template.up.sql | 10 +++++++++- coderd/notifications/notifications_test.go | 5 ++--- .../smtp/TemplateTemplateDeleted.html.golden | 17 ++++++----------- .../webhook/TemplateTemplateDeleted.json.golden | 11 +++++------ coderd/provisionerdserver/provisionerdserver.go | 9 +++++---- coderd/templates.go | 10 +++++++--- coderd/templates_test.go | 3 +-- 8 files changed, 44 insertions(+), 30 deletions(-) diff --git a/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql b/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql index 64ced8ab9ba26..9a9d5b9c5c002 100644 --- a/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql +++ b/coderd/database/migrations/000264_manual_build_failed_notification_template.down.sql @@ -7,3 +7,12 @@ SET E'The workspace build was initiated by **{{.Labels.initiator}}**.' WHERE id = '2faeee0f-26cb-4e96-821c-85ccb9f71513'; + +UPDATE notification_templates +SET + body_template = E'Hi {{.UserName}},\n\n' || -- Add a comma + E'The template **{{.Labels.name}}** was deleted by **{{ .Labels.initiator }}**.\n\n' || + -- Mention template display name: + E'The template''s display name was **{{.Labels.display_name}}**.' +WHERE + id = '29a09665-2a4c-403f-9648-54301670e7be'; diff --git a/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql b/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql index a9088976c7a66..b5deebe30369f 100644 --- a/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql +++ b/coderd/database/migrations/000264_manual_build_failed_notification_template.up.sql @@ -1,8 +1,16 @@ UPDATE notification_templates SET body_template = E'Hi {{.UserName}},\n\n' || - -- Revert to a single label for the template name.: + -- Revert to a single label for the template name: E'A manual build of the workspace **{{.Labels.name}}** using the template **{{.Labels.template_name}}** failed (version: **{{.Labels.template_version_name}}**).\n\n' || E'The workspace build was initiated by **{{.Labels.initiator}}**.' WHERE id = '2faeee0f-26cb-4e96-821c-85ccb9f71513'; + +UPDATE notification_templates +SET + body_template = E'Hi {{.UserName}},\n\n' || + -- Revert to a single label for the template name: + E'The template **{{.Labels.name}}** was deleted by **{{ .Labels.initiator }}**.\n\n' +WHERE + id = '29a09665-2a4c-403f-9648-54301670e7be'; diff --git a/coderd/notifications/notifications_test.go b/coderd/notifications/notifications_test.go index 4d1af42654669..61862826acca1 100644 --- a/coderd/notifications/notifications_test.go +++ b/coderd/notifications/notifications_test.go @@ -870,9 +870,8 @@ func TestNotificationTemplates_Golden(t *testing.T) { UserEmail: "bobby@coder.com", UserUsername: "bobby", Labels: map[string]string{ - "name": "bobby-template", - "display_name": "Bobby's Template", - "initiator": "rob", + "name": "Bobby's Template", + "initiator": "rob", }, }, }, diff --git a/coderd/notifications/testdata/rendered-templates/smtp/TemplateTemplateDeleted.html.golden b/coderd/notifications/testdata/rendered-templates/smtp/TemplateTemplateDeleted.html.golden index ec7c3cfdce485..2ae9ac8e61db5 100644 --- a/coderd/notifications/testdata/rendered-templates/smtp/TemplateTemplateDeleted.html.golden +++ b/coderd/notifications/testdata/rendered-templates/smtp/TemplateTemplateDeleted.html.golden @@ -1,6 +1,6 @@ From: system@coder.com To: bobby@coder.com -Subject: Template "bobby-template" deleted +Subject: Template "Bobby's Template" deleted 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 @@ -12,9 +12,7 @@ Content-Type: text/plain; charset=UTF-8 Hi Bobby, -The template bobby-template was deleted by rob. - -The template's display name was Bobby's Template. +The template Bobby's Template was deleted by rob. View templates: http://test.com/templates @@ -29,7 +27,7 @@ Content-Type: text/html; charset=UTF-8 - Codestin Search App + Codestin Search App

- Template "bobby-template" deleted + Template "Bobby's Template" deleted

Hi Bobby,

-

The template bobby-template was deleted by rob<= -/strong>.

- -

The template’s display name was Bobby’s Template.

+

The template Bobby’s Template was deleted by rob.

=20 diff --git a/coderd/notifications/testdata/rendered-templates/webhook/TemplateTemplateDeleted.json.golden b/coderd/notifications/testdata/rendered-templates/webhook/TemplateTemplateDeleted.json.golden index 4a97b3a14f866..4390a3ddfb84b 100644 --- a/coderd/notifications/testdata/rendered-templates/webhook/TemplateTemplateDeleted.json.golden +++ b/coderd/notifications/testdata/rendered-templates/webhook/TemplateTemplateDeleted.json.golden @@ -16,14 +16,13 @@ } ], "labels": { - "display_name": "Bobby's Template", "initiator": "rob", - "name": "bobby-template" + "name": "Bobby's Template" }, "data": null }, - "title": "Template \"bobby-template\" deleted", - "title_markdown": "Template \"bobby-template\" deleted", - "body": "Hi Bobby,\n\nThe template bobby-template was deleted by rob.\n\nThe template's display name was Bobby's Template.", - "body_markdown": "Hi Bobby,\n\nThe template **bobby-template** was deleted by **rob**.\n\nThe template's display name was **Bobby's Template**." + "title": "Template \"Bobby's Template\" deleted", + "title_markdown": "Template \"Bobby's Template\" deleted", + "body": "Hi Bobby,\n\nThe template Bobby's Template was deleted by rob.", + "body_markdown": "Hi Bobby,\n\nThe template **Bobby's Template** was deleted by **rob**.\n\n" } \ No newline at end of file diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index 6f364b1567a26..e81aa02f0c264 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -1124,17 +1124,18 @@ func (s *server) notifyWorkspaceManualBuildFailed(ctx context.Context, workspace } for _, templateAdmin := range templateAdmins { + templateNameLabel := template.DisplayName + if templateNameLabel == "" { + templateNameLabel = template.Name + } labels := map[string]string{ "name": workspace.Name, - "template_name": template.DisplayName, + "template_name": templateNameLabel, "template_version_name": templateVersion.Name, "initiator": build.InitiatorByUsername, "workspace_owner_username": workspaceOwner.Username, "workspace_build_number": strconv.Itoa(int(build.BuildNumber)), } - if template.DisplayName == "" { - labels["template_name"] = template.Name - } if _, err := s.NotificationsEnqueuer.Enqueue(ctx, templateAdmin.ID, notifications.TemplateWorkspaceManualBuildFailed, labels, "provisionerdserver", // Associate this notification with all the related entities. diff --git a/coderd/templates.go b/coderd/templates.go index 01c2a056bb813..907a4d1265836 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -135,11 +135,15 @@ func (api *API) notifyTemplateDeleted(ctx context.Context, template database.Tem return } + templateNameLabel := template.DisplayName + if templateNameLabel == "" { + templateNameLabel = template.Name + } + if _, err := api.NotificationsEnqueuer.Enqueue(ctx, receiverID, notifications.TemplateTemplateDeleted, map[string]string{ - "name": template.Name, - "display_name": template.DisplayName, - "initiator": initiator.Username, + "name": templateNameLabel, + "initiator": initiator.Username, }, "api-templates-delete", // Associate this notification with all the related entities. template.ID, template.OrganizationID, diff --git a/coderd/templates_test.go b/coderd/templates_test.go index 4273169d4ca82..c1f1f8f1bbed2 100644 --- a/coderd/templates_test.go +++ b/coderd/templates_test.go @@ -1456,8 +1456,7 @@ func TestTemplateNotifications(t *testing.T) { require.Contains(t, notifiedUsers, n.UserID) require.Contains(t, n.Targets, template.ID) require.Contains(t, n.Targets, template.OrganizationID) - require.Equal(t, n.Labels["name"], template.Name) - require.Equal(t, n.Labels["display_name"], template.DisplayName) + require.Equal(t, n.Labels["name"], template.DisplayName) require.Equal(t, n.Labels["initiator"], coderdtest.FirstUserParams.Username) } })