Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d71c94

Browse files
committed
Apply Dannys review comments
1 parent 0226fdf commit 4d71c94

12 files changed

+45
-41
lines changed

coderd/autobuild/lifecycle_executor.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
299299
nextBuildReason = string(nextBuild.Reason)
300300
}
301301

302-
if _, err := e.notificationsEnqueuer.Enqueue(e.ctx, ws.OwnerID, notifications.WorkspaceAutoUpdated,
302+
if _, err := e.notificationsEnqueuer.Enqueue(e.ctx, ws.OwnerID, notifications.TemplateWorkspaceAutoUpdated,
303303
map[string]string{
304304
"name": ws.Name,
305305
"initiator": "autobuild",
@@ -334,8 +334,6 @@ func (e *Executor) runOnce(t time.Time) Stats {
334334
if err != nil {
335335
log.Warn(e.ctx, "failed to notify of workspace marked as dormant", slog.Error(err), slog.F("workspace_id", dormantNotification.Workspace.ID))
336336
}
337-
} else {
338-
log.Warn(e.ctx, "no dormant notification to send", slog.F("workspace_id", wsID))
339337
}
340338
return nil
341339
}()

coderd/autobuild/lifecycle_executor_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/coder/coder/v2/coderd/coderdtest"
1919
"github.com/coder/coder/v2/coderd/database"
2020
"github.com/coder/coder/v2/coderd/database/dbauthz"
21+
"github.com/coder/coder/v2/coderd/notifications"
2122
"github.com/coder/coder/v2/coderd/schedule"
2223
"github.com/coder/coder/v2/coderd/schedule/cron"
2324
"github.com/coder/coder/v2/coderd/util/ptr"
@@ -1107,15 +1108,16 @@ func TestNotifications(t *testing.T) {
11071108

11081109
// Wait for workspace to become dormant
11091110
ticker <- workspace.LastUsedAt.Add(timeTilDormant * 3)
1110-
<-statCh
1111+
_ = testutil.RequireRecvCtx(testutil.Context(t, testutil.WaitShort), t, statCh)
11111112

11121113
// Check that the workspace is dormant
11131114
workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
1114-
require.NotNil(t, workspace.DormantAt)
1115+
require.Equal(t, workspace.DormantAt, workspace.LastUsedAt.Add(timeTilDormant))
11151116

11161117
// Check that a notification was enqueued
11171118
require.Len(t, notifyEnq.Sent, 1)
11181119
require.Equal(t, notifyEnq.Sent[0].UserID, workspace.OwnerID)
1120+
require.Equal(t, notifyEnq.Sent[0].TemplateID, notifications.TemplateWorkspaceDormant)
11191121
require.Contains(t, notifyEnq.Sent[0].Targets, template.ID)
11201122
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.ID)
11211123
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.OrganizationID)
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
DELETE FROM notification_templates WHERE id = '0ea69165-ec14-4314-91f1-69566ac3c5a0';
1+
DELETE FROM notification_templates
2+
WHERE
3+
id = '0ea69165-ec14-4314-91f1-69566ac3c5a0';
4+
5+
DELETE FROM notification_templates
6+
WHERE
7+
id = '51ce2fdf-c9ca-4be1-8d70-628674f9bc42';

coderd/database/migrations/000229_dormancy_notification_template.up.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,18 @@ VALUES (
1818
"label": "View workspace",
1919
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
2020
}
21+
]'::jsonb,
22+
),
23+
(
24+
'51ce2fdf-c9ca-4be1-8d70-628674f9bc42',
25+
'Workspace Marked for Deletion',
26+
E'Workspace "{{.Labels.name}}" marked for deletion',
27+
E'Hi {{.UserName}}\n\n' || E'Your workspace **{{.Labels.name}}** has been marked for **deletion** after {{.Labels.dormancyHours}} hours of dormancy.\n' || E'The specified reason was "**{{.Labels.reason}}{{end}}**\n\n' || E'Dormancy refers to a workspace being unused for a defined length of time, and after it exceeds {{.Labels.dormancyHours}} hours of dormancy it will be deleted.\n' || E'To prevent your workspace from being deleted, simply use it as normal.',
28+
'Workspace Events',
29+
'[
30+
{
31+
"label": "View workspace",
32+
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
33+
}
2134
]'::jsonb
2235
);

coderd/database/migrations/000230_marked_for_deletion_notification_template.down.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

coderd/database/migrations/000230_marked_for_deletion_notification_template.up sql

Lines changed: 0 additions & 22 deletions
This file was deleted.

coderd/dormancy/notifications.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ func NotifyWorkspaceMarkedForDeletion(
5757
notification WorkspaceMarkedForDeletionNotification,
5858
) (id *uuid.UUID, err error) {
5959
labels := map[string]string{
60-
"name": notification.Workspace.Name,
61-
"initiator": "autobuild",
62-
"reason": notification.Reason,
60+
"name": notification.Workspace.Name,
61+
"reason": notification.Reason,
6362
}
6463
return enqueuer.Enqueue(
6564
ctx,
6665
notification.Workspace.OwnerID,
67-
notifications.TemplateWorkspaceDormant,
66+
notifications.TemplateWorkspaceMarkedForDeletion,
6867
labels,
6968
notification.CreatedBy,
7069
// Associate this notification with all the related entities.

coderd/notifications/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import "github.com/google/uuid"
88
// Workspace-related events.
99
var (
1010
TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
11-
WorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
11+
TemplateWorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
1212
TemplateWorkspaceDormant = uuid.MustParse("0ea69165-ec14-4314-91f1-69566ac3c5a0")
13-
WorkspaceAutoUpdated = uuid.MustParse("c34a0c09-0704-4cac-bd1c-0c0146811c2b")
13+
TemplateWorkspaceAutoUpdated = uuid.MustParse("c34a0c09-0704-4cac-bd1c-0c0146811c2b")
1414
TemplateWorkspaceMarkedForDeletion = uuid.MustParse("51ce2fdf-c9ca-4be1-8d70-628674f9bc42")
1515
)

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ func (s *server) notifyWorkspaceBuildFailed(ctx context.Context, workspace datab
11031103
reason = string(build.Reason)
11041104
initiator := "autobuild"
11051105

1106-
if _, err := s.NotificationsEnqueuer.Enqueue(ctx, workspace.OwnerID, notifications.WorkspaceAutobuildFailed,
1106+
if _, err := s.NotificationsEnqueuer.Enqueue(ctx, workspace.OwnerID, notifications.TemplateWorkspaceAutobuildFailed,
11071107
map[string]string{
11081108
"name": workspace.Name,
11091109
"initiator": initiator,

coderd/workspaces_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/coder/coder/v2/coderd/database/dbgen"
3131
"github.com/coder/coder/v2/coderd/database/dbtestutil"
3232
"github.com/coder/coder/v2/coderd/database/dbtime"
33+
"github.com/coder/coder/v2/coderd/notifications"
3334
"github.com/coder/coder/v2/coderd/rbac"
3435
"github.com/coder/coder/v2/coderd/rbac/policy"
3536
"github.com/coder/coder/v2/coderd/render"
@@ -3541,6 +3542,7 @@ func TestNotifications(t *testing.T) {
35413542
// Then
35423543
require.NoError(t, err, "mark workspace as dormant")
35433544
require.Len(t, notifyEnq.Sent, 1)
3545+
require.Equal(t, notifyEnq.Sent[0].TemplateID, notifications.TemplateWorkspaceDormant)
35443546
require.Equal(t, notifyEnq.Sent[0].UserID, workspace.OwnerID)
35453547
require.Contains(t, notifyEnq.Sent[0].Targets, template.ID)
35463548
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.ID)

0 commit comments

Comments
 (0)