8000 fix: notifications: use username in workspace URLs by mtojek · Pull Request #14011 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: notifications: use username in workspace URLs #14011

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,7 @@ func (q *FakeQuerier) FetchNewMessageMetadata(_ context.Context, arg database.Fe
return database.FetchNewMessageMetadataRow{
UserEmail: user.Email,
UserName: userName,
UserUsername: user.Username,
NotificationName: "Some notification",
Actions: actions,
UserID: arg.UserID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE notification_templates
SET
actions = REPLACE(actions::text, '@{{.UserUsername}}', '@{{.UserName}}')::jsonb;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE notification_templates
SET
actions = REPLACE(actions::text, '@{{.UserName}}', '@{{.UserUsername}}')::jsonb;
5 changes: 4 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coderd/database/queries/notifications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ SELECT nt.name AS notificatio
nt.actions AS actions,
u.id AS user_id,
u.email AS user_email,
COALESCE(NULLIF(u.name, ''), NULLIF(u.username, ''))::text AS user_name
COALESCE(NULLIF(u.name, ''), NULLIF(u.username, ''))::text AS user_name,
COALESCE(u.username, '') AS user_username
FROM notification_templates nt,
users u
WHERE nt.id = @notification_template_id
Expand Down
7 changes: 4 additions & 3 deletions coderd/notifications/enqueuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID, templateID uui

NotificationName: metadata.NotificationName,

UserID: metadata.UserID.String(),
UserEmail: metadata.UserEmail,
UserName: metadata.UserName,
UserID: metadata.UserID.String(),
UserEmail: metadata.UserEmail,
UserName: metadata.UserName,
UserUsername: metadata.UserUsername,

Labels: labels,
// No actions yet
Expand Down
8 changes: 5 additions & 3 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ func TestWebhookDispatch(t *testing.T) {
require.NoError(t, err)

const (
email = "bob@coder.com"
name = "Robert McBobbington"
email = "bob@coder.com"
name = "Robert McBobbington"
username = "bob"
)
user := dbgen.User(t, db, database.User{
Email: email,
Username: "bob",
Username: username,
Name: name,
})

Expand All @@ -229,6 +230,7 @@ func TestWebhookDispatch(t *testing.T) {
// UserName is coalesced from `name` and `username`; in this case `name` wins.
// This is not strictly necessary for this test, but it's testing some side logic which is too small for its own test.
require.Equal(t, payload.Payload.UserName, name)
require.Equal(t, payload.Payload.UserUsername, username)
// Right now we don't have a way to query notification templates by ID in dbmem, and it's not necessary to add this
// just to satisfy this test. We can safely assume that as long as this value is not empty that the given value was delivered.
require.NotEmpty(t, payload.Payload.NotificationName)
Expand Down
5 changes: 3 additions & 2 deletions coderd/notifications/render/gotmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func TestGoTemplate(t *testing.T) {
name: "render workspace URL",
in: `[{
"label": "View workspace",
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
"url": "{{ base_url }}/@{{.UserUsername}}/{{.Labels.name}}"
}]`,
payload: types.MessagePayload{
UserName: "johndoe",
UserName: "John Doe",
UserUsername: "johndoe",
Labels: map[string]string{
"name": "my-workspace",
},
Expand Down
7 changes: 4 additions & 3 deletions coderd/notifications/types/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ type MessagePayload struct {

NotificationName string `json:"notification_name"`

UserID string `json:"user_id"`
UserEmail string `json:"user_email"`
UserName string `json:"user_name"`
UserID string `json:"user_id"`
UserEmail string `json:"user_email"`
UserName string `json:"user_name"`
UserUsername string `json:"user_username"`

Actions []TemplateAction `json:"actions"`
Labels map[string]string `json:"labels"`
Expand Down
Loading
0