- Notifications
You must be signed in to change notification settings - Fork920
feat: add tool to send a test notification#16611
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
53d6975
879cdd1
13567ff
27e9765
fc49ec4
4a2ec9b
a74d3f7
1a2d1ea
f75f822
2316e96
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
coder v0.0.0-devel | ||
USAGE: | ||
coder notifications test | ||
Send a test notification | ||
——— | ||
Run `coder --help` for a list of global options. |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DELETE FROM notification_templates WHERE id = 'c425f63e-716a-4bf4-ae24-78348f706c3f'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
INSERT INTO notification_templates | ||
(id, name, title_template, body_template, "group", actions) | ||
VALUES ( | ||
'c425f63e-716a-4bf4-ae24-78348f706c3f', | ||
'Test Notification', | ||
E'A test notification', | ||
E'Hi {{.UserName}},\n\n'|| | ||
E'This is a test notification.', | ||
'Notification Events', | ||
'[ | ||
{ | ||
"label": "View notification settings", | ||
"url": "{{base_url}}/deployment/notifications?tab=settings" | ||
} | ||
]'::jsonb | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -11,9 +11,12 @@ import ( | ||
"github.com/coder/coder/v2/coderd/audit" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/coderd/notifications" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
@@ -163,6 +166,53 @@ func (api *API) notificationDispatchMethods(rw http.ResponseWriter, r *http.Requ | ||
}) | ||
} | ||
// @Summary Send a test notification | ||
// @ID send-a-test-notification | ||
// @Security CoderSessionToken | ||
// @Tags Notifications | ||
// @Success 200 | ||
// @Router /notifications/test [post] | ||
func (api *API) postTestNotification(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
ctx = r.Context() | ||
key = httpmw.APIKey(r) | ||
) | ||
if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { | ||
httpapi.Forbidden(rw) | ||
return | ||
} | ||
if _, err := api.NotificationsEnqueuer.EnqueueWithData( | ||
//nolint:gocritic // We need to be notifier to send the notification. | ||
dbauthz.AsNotifier(ctx), | ||
key.UserID, | ||
notifications.TemplateTestNotification, | ||
map[string]string{}, | ||
map[string]any{ | ||
// NOTE(DanielleMaywood): | ||
// When notifications are enqueued, they are checked to be | ||
// unique within a single day. This means that if we attempt | ||
// to send two test notifications to the same user on | ||
// the same day, the enqueuer will prevent us from sending | ||
// a second one. We are injecting a timestamp to make the | ||
// notifications appear different enough to circumvent this | ||
// deduplication logic. | ||
"timestamp": api.Clock.Now(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 🥇 | ||
}, | ||
"send-test-notification", | ||
); err != nil { | ||
api.Logger.Error(ctx, "send notification", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to send test notification", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
httpapi.Write(ctx, rw, http.StatusOK, nil) | ||
} | ||
// @Summary Get user notification preferences | ||
// @ID get-user-notification-preferences | ||
// @Security CoderSessionToken | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
From: system@coder.com | ||
To: bobby@coder.com | ||
Subject: A test notification | ||
Message-Id: 02ee4935-73be-4fa1-a290-ff9999026b13@blush-whale-48 | ||
Date: Fri, 11 Oct 2024 09:03:06 +0000 | ||
Content-Type: multipart/alternative; boundary=bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
MIME-Version: 1.0 | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
Content-Transfer-Encoding: quoted-printable | ||
Content-Type: text/plain; charset=UTF-8 | ||
Hi Bobby, | ||
This is a test notification. | ||
View notification settings: http://test.com/deployment/notifications?tab=3D= | ||
settings | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4 | ||
Content-Transfer-Encoding: quoted-printable | ||
Content-Type: text/html; charset=UTF-8 | ||
<!doctype html> | ||
<html lang=3D"en"> | ||
<head> | ||
<meta charset=3D"UTF-8" /> | ||
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale= | ||
=3D1.0" /> | ||
<title>A test notification</title> | ||
</head> | ||
<body style=3D"margin: 0; padding: 0; font-family: -apple-system, system-= | ||
ui, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarel= | ||
l', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; color: #020617= | ||
; background: #f8fafc;"> | ||
<div style=3D"max-width: 600px; margin: 20px auto; padding: 60px; borde= | ||
r: 1px solid #e2e8f0; border-radius: 8px; background-color: #fff; text-alig= | ||
n: left; font-size: 14px; line-height: 1.5;"> | ||
<div style=3D"text-align: center;"> | ||
<img src=3D"https://coder.com/coder-logo-horizontal.png" alt=3D"Cod= | ||
er Logo" style=3D"height: 40px;" /> | ||
</div> | ||
<h1 style=3D"text-align: center; font-size: 24px; font-weight: 400; m= | ||
argin: 8px 0 32px; line-height: 1.5;"> | ||
A test notification | ||
</h1> | ||
<div style=3D"line-height: 1.5;"> | ||
<p>Hi Bobby,</p> | ||
<p>This is a test notification.</p> | ||
</div> | ||
<div style=3D"text-align: center; margin-top: 32px;"> | ||
=20 | ||
<a href=3D"http://test.com/deployment/notifications?tab=3Dsettings"= | ||
style=3D"display: inline-block; padding: 13px 24px; background-color: #020= | ||
617; color: #f8fafc; text-decoration: none; border-radius: 8px; margin: 0 4= | ||
px;"> | ||
View notification settings | ||
</a> | ||
=20 | ||
</div> | ||
<div style=3D"border-top: 1px solid #e2e8f0; color: #475569; font-siz= | ||
e: 12px; margin-top: 64px; padding-top: 24px; line-height: 1.6;"> | ||
<p>© 2024 Coder. All rights reserved - <a = | ||
href=3D"http://test.com" style=3D"color: #2563eb; text-decoration: none;">h= | ||
ttp://test.com</a></p> | ||
<p><a href=3D"http://test.com/settings/notifications" style=3D"colo= | ||
r: #2563eb; text-decoration: none;">Click here to manage your notification = | ||
settings</a></p> | ||
<p><a href=3D"http://test.com/settings/notifications?disabled=3Dc42= | ||
5f63e-716a-4bf4-ae24-78348f706c3f" style=3D"color: #2563eb; text-decoration= | ||
: none;">Stop receiving emails like this</a></p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4-- |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.