fix: notifications: use username in workspace URLs (#14011)

This commit is contained in:
Marcin Tojek
2024-07-25 12:02:24 +02:00
committed by GitHub
parent 88bc491778
commit d488853393
9 changed files with 29 additions and 13 deletions

View File

@ -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,

View File

@ -0,0 +1,3 @@
UPDATE notification_templates
SET
actions = REPLACE(actions::text, '@{{.UserUsername}}', '@{{.UserName}}')::jsonb;

View File

@ -0,0 +1,3 @@
UPDATE notification_templates
SET
actions = REPLACE(actions::text, '@{{.UserName}}', '@{{.UserUsername}}')::jsonb;

View File

@ -3546,7 +3546,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 = $1
@ -3564,6 +3565,7 @@ type FetchNewMessageMetadataRow struct {
UserID uuid.UUID `db:"user_id" json:"user_id"`
UserEmail string `db:"user_email" json:"user_email"`
UserName string `db:"user_name" json:"user_name"`
UserUsername string `db:"user_username" json:"user_username"`
}
// This is used to build up the notification_message's JSON payload.
@ -3576,6 +3578,7 @@ func (q *sqlQuerier) FetchNewMessageMetadata(ctx context.Context, arg FetchNewMe
&i.UserID,
&i.UserEmail,
&i.UserName,
&i.UserUsername,
)
return i, err
}

View File

@ -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

View File

@ -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

View File

@ -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,
})
@ -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)

View File

@ -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",
},

View File

@ -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"`