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

Skip to content

Commit 68ae258

Browse files
committed
render template
1 parent 660c2d6 commit 68ae258

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

coderd/database/migrations/000226_notifications_autobuild_failed.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ VALUES ('381df2a9-c0c0-4749-420f-80a9280c66f9', 'Workspace Autobuild Failed', E'
33
E'Hi {{.UserName}}\n\Automatic build of your workspace **{{.Labels.name}}** failed.\nThe specified reason was "**{{.Labels.reason}}**".',
44
'Workspace Events', '[
55
{
6-
"label": "View workspaces",
7-
"url": "{{ base_url }}/workspaces"
6+
"label": "View workspace",
7+
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
88
}
99
]'::jsonb);

coderd/notifications/enqueuer.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (s *StoreEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUI
8080
// buildPayload creates the payload that the notification will for variable substitution and/or routing.
8181
// The payload contains information about the recipient, the event that triggered the notification, and any subsequent
8282
// actions which can be taken by the recipient.
83-
func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, templateID uuid.UUID, labels map[string]string) (*types.MessagePayload, error) {
83+
func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string) (*types.MessagePayload, error) {
8484
metadata, err := s.store.FetchNewMessageMetadata(ctx, database.FetchNewMessageMetadataParams{
8585
UserID: userID,
8686
NotificationTemplateID: templateID,
@@ -89,8 +89,21 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, temp
8989
return nil, xerrors.Errorf("new message metadata: %w", err)
9090
}
9191

92+
payload := types.MessagePayload{
93+
Version: "1.0",
94+
95+
NotificationName: metadata.NotificationName,
96+
97+
UserID: metadata.UserID.String(),
98+
UserEmail: metadata.UserEmail,
99+
UserName: metadata.UserName,
100+
101+
Labels: labels,
102+
// No actions yet
103+
}
104+
92105
// Execute any templates in actions.
93-
out, err := render.GoTemplate(string(metadata.Actions), types.MessagePayload{}, s.helpers)
106+
out, err := render.GoTemplate(string(metadata.Actions), payload, s.helpers)
94107
if err != nil {
95108
return nil, xerrors.Errorf("render actions: %w", err)
96109
}
@@ -100,19 +113,8 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, temp
100113
if err = json.Unmarshal(metadata.Actions, &actions); err != nil {
101114
return nil, xerrors.Errorf("new message metadata: parse template actions: %w", err)
102115
}
103-
104-
return &types.MessagePayload{
105-
Version: "1.0",
106-
107-
NotificationName: metadata.NotificationName,
108-
109-
UserID: metadata.UserID.String(),
110-
UserEmail: metadata.UserEmail,
111-
UserName: metadata.UserName,
112-
113-
Actions: actions,
114-
Labels: labels,
115-
}, nil
116+
payload.Actions = actions
117+
return &payload, nil
116118
}
117119

118120
// NoopEnqueuer implements the Enqueuer interface but performs a noop.

coderd/notifications/render/gotmpl_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ func TestGoTemplate(t *testing.T) {
3838
expectedOutput: userEmail,
3939
expectedErr: nil,
4040
},
41+
{
42+
name: "render workspace URL",
43+
in: `[{
44+
"label": "View workspace",
45+
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
46+
}]`,
47+
payload: types.MessagePayload{
48+
UserName: "johndoe",
49+
Labels: map[string]string{
50+
"name": "my-workspace",
51+
},
52+
},
53+
expectedOutput: `[{
54+
"label": "View workspace",
55+
"url": "https://mocked-server-address/@johndoe/my-workspace"
56+
}]`,
57+
},
4158
}
4259

4360
for _, tc := range tests {
@@ -46,7 +63,9 @@ func TestGoTemplate(t *testing.T) {
4663
t.Run(tc.name, func(t *testing.T) {
4764
t.Parallel()
4865

49-
out, err := render.GoTemplate(tc.in, tc.payload, nil)
66+
out, err := render.GoTemplate(tc.in, tc.payload, map[string]any{
67+
"base_url": func() string { return "https://mocked-server-address" },
68+
})
5069
if tc.expectedErr == nil {
5170
require.NoError(t, err)
5271
} else {

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ func TestNotifications(t *testing.T) {
16871687
require.Contains(t, notifEnq.sent[0].targets, workspace.OrganizationID)
16881688
require.Contains(t, notifEnq.sent[0].targets, user.ID)
16891689
if tc.deletionReason == database.BuildReasonInitiator {
1690-
require.Equal(t, notifEnq.sent[0].labels["initiator"], initiator.Username)
1690+
require.Equal(t, initiator.Username, notifEnq.sent[0].labels["initiator"])
16911691
}
16921692
} else {
16931693
require.Len(t, notifEnq.sent, 0)
@@ -1798,8 +1798,8 @@ func TestNotifications(t *testing.T) {
17981798
require.Contains(t, notifEnq.sent[0].targets, workspace.ID)
17991799
require.Contains(t, notifEnq.sent[0].targets, workspace.OrganizationID)
18001800
require.Contains(t, notifEnq.sent[0].targets, user.ID)
1801-
require.Equal(t, notifEnq.sent[0].labels["initiator"], "autobuild")
1802-
require.Equal(t, notifEnq.sent[0].labels["reason"], string(tc.buildReason))
1801+
require.Equal(t, "autobuild", notifEnq.sent[0].labels["initiator"])
1802+
require.Equal(t, string(tc.buildReason), notifEnq.sent[0].labels["reason"])
18031803
} else {
18041804
require.Len(t, notifEnq.sent, 0)
18051805
}

0 commit comments

Comments
 (0)