Skip to content

Commit d488853

Browse files
authored
fix: notifications: use username in workspace URLs (coder#14011)
1 parent 88bc491 commit d488853

9 files changed

+29
-13
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ func (q *FakeQuerier) FetchNewMessageMetadata(_ context.Context, arg database.Fe
19291929
return database.FetchNewMessageMetadataRow{
19301930
UserEmail: user.Email,
19311931
UserName: userName,
1932+
UserUsername: user.Username,
19321933
NotificationName: "Some notification",
19331934
Actions: actions,
19341935
UserID: arg.UserID,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
UPDATE notification_templates
2+
SET
3+
actions = REPLACE(actions::text, '@{{.UserUsername}}', '@{{.UserName}}')::jsonb;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
UPDATE notification_templates
2+
SET
3+
actions = REPLACE(actions::text, '@{{.UserName}}', '@{{.UserUsername}}')::jsonb;

coderd/database/queries.sql.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/notifications.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ SELECT nt.name AS notificatio
44
nt.actions AS actions,
55
u.id AS user_id,
66
u.email AS user_email,
7-
COALESCE(NULLIF(u.name, ''), NULLIF(u.username, ''))::text AS user_name
7+
COALESCE(NULLIF(u.name, ''), NULLIF(u.username, ''))::text AS user_name,
8+
COALESCE(u.username, '') AS user_username
89
FROM notification_templates nt,
910
users u
1011
WHERE nt.id = @notification_template_id

coderd/notifications/enqueuer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID, templateID uui
9494

9595
NotificationName: metadata.NotificationName,
9696

97-
UserID: metadata.UserID.String(),
98-
UserEmail: metadata.UserEmail,
99-
UserName: metadata.UserName,
97+
UserID: metadata.UserID.String(),
98+
UserEmail: metadata.UserEmail,
99+
UserName: metadata.UserName,
100+
UserUsername: metadata.UserUsername,
100101

101102
Labels: labels,
102103
// No actions yet

coderd/notifications/notifications_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,13 @@ func TestWebhookDispatch(t *testing.T) {
201201
require.NoError(t, err)
202202

203203
const (
204-
email = "bob@coder.com"
205-
name = "Robert McBobbington"
204+
email = "bob@coder.com"
205+
name = "Robert McBobbington"
206+
username = "bob"
206207
)
207208
user := dbgen.User(t, db, database.User{
208209
Email: email,
209-
Username: "bob",
210+
Username: username,
210211
Name: name,
211212
})
212213

@@ -229,6 +230,7 @@ func TestWebhookDispatch(t *testing.T) {
229230
// UserName is coalesced from `name` and `username`; in this case `name` wins.
230231
// This is not strictly necessary for this test, but it's testing some side logic which is too small for its own test.
231232
require.Equal(t, payload.Payload.UserName, name)
233+
require.Equal(t, payload.Payload.UserUsername, username)
232234
// Right now we don't have a way to query notification templates by ID in dbmem, and it's not necessary to add this
233235
// just to satisfy this test. We can safely assume that as long as this value is not empty that the given value was delivered.
234236
require.NotEmpty(t, payload.Payload.NotificationName)

coderd/notifications/render/gotmpl_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ func TestGoTemplate(t *testing.T) {
4242
name: "render workspace URL",
4343
in: `[{
4444
"label": "View workspace",
45-
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
45+
"url": "{{ base_url }}/@{{.UserUsername}}/{{.Labels.name}}"
4646
}]`,
4747
payload: types.MessagePayload{
48-
UserName: "johndoe",
48+
UserName: "John Doe",
49+
UserUsername: "johndoe",
4950
Labels: map[string]string{
5051
"name": "my-workspace",
5152
},

coderd/notifications/types/payload.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ type MessagePayload struct {
99

1010
NotificationName string `json:"notification_name"`
1111

12-
UserID string `json:"user_id"`
13-
UserEmail string `json:"user_email"`
14-
UserName string `json:"user_name"`
12+
UserID string `json:"user_id"`
13+
UserEmail string `json:"user_email"`
14+
UserName string `json:"user_name"`
15+
UserUsername string `json:"user_username"`
1516

1617
Actions []TemplateAction `json:"actions"`
1718
Labels map[string]string `json:"labels"`

0 commit comments

Comments
 (0)