- Notifications
You must be signed in to change notification settings - Fork1k
feat: notify on workspace update#15979
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
541b852
add notification on workspace update
DanielleMaywood835e49c
tests: test expectations
DanielleMaywoodb43a7e6
chore: change error message, add error log
DanielleMaywoodc683985
chore: apply feedback
DanielleMaywoodb3707a1
fix: broken logic
DanielleMaywood7f10ecc
chore: wrap in transaction
DanielleMaywood1a493ff
chore: remove whitespace between call and return err
DanielleMaywood32adc00
chore: actually use the transaction, oops
DanielleMaywood90380f7
chore: changes
DanielleMaywood71ec1ac
chore: move prep of builder and notify out of tx
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionscoderd/database/migrations/000280_workspace_update_notification.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DELETE FROM notification_templates WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392'; |
30 changes: 30 additions & 0 deletionscoderd/database/migrations/000280_workspace_update_notification.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
INSERT INTO notification_templates | ||
(id, name, title_template, body_template, "group", actions) | ||
VALUES ( | ||
'd089fe7b-d5c5-4c0c-aaf5-689859f7d392', | ||
'Workspace Manually Updated', | ||
E'Workspace ''{{.Labels.workspace}}'' has been manually updated', | ||
E'Hello {{.UserName}},\n\n'|| | ||
E'A new workspace build has been manually created for your workspace **{{.Labels.workspace}}** by **{{.Labels.initiator}}** to update it to version **{{.Labels.version}}** of template **{{.Labels.template}}**.', | ||
'Workspace Events', | ||
'[ | ||
{ | ||
"label": "View workspace", | ||
"url": "{{base_url}}/@{{.UserUsername}}/{{.Labels.workspace}}" | ||
}, | ||
{ | ||
"label": "View template version", | ||
"url": "{{base_url}}/templates/{{.Labels.organization}}/{{.Labels.template}}/versions/{{.Labels.version}}" | ||
} | ||
]'::jsonb | ||
); | ||
UPDATE notification_templates | ||
SET | ||
actions = '[ | ||
{ | ||
"label": "View workspace", | ||
"url": "{{base_url}}/@{{.UserUsername}}/{{.Labels.workspace}}" | ||
} | ||
]'::jsonb | ||
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff'; |
1 change: 1 addition & 0 deletionscoderd/notifications/events.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletionscoderd/notifications/notifications_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionscoderd/notifications/testdata/rendered-templates/smtp/TemplateWorkspaceCreated.html.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions...tifications/testdata/rendered-templates/smtp/TemplateWorkspaceManuallyUpdated.html.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
From: system@coder.com | ||
To: bobby@coder.com | ||
Subject: Workspace 'bobby-workspace' has been manually updated | ||
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 | ||
Hello Bobby, | ||
A new workspace build has been manually created for your workspace bobby-wo= | ||
rkspace by bobby to update it to version alpha of template bobby-template. | ||
View workspace: http://test.com/@bobby/bobby-workspace | ||
View template version: http://test.com/templates/bobby-organization/bobby-t= | ||
emplate/versions/alpha | ||
--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>Workspace 'bobby-workspace' has been manually updated</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;"> | ||
Workspace 'bobby-workspace' has been manually updated | ||
</h1> | ||
<div style=3D"line-height: 1.5;"> | ||
<p>Hello Bobby,</p> | ||
<p>A new workspace build has been manually created for your workspace <stro= | ||
ng>bobby-workspace</strong> by <strong>bobby</strong> to update it to versi= | ||
on <strong>alpha</strong> of template <strong>bobby-template</strong>.</p> | ||
</div> | ||
<div style=3D"text-align: center; margin-top: 32px;"> | ||
=20 | ||
<a href=3D"http://test.com/@bobby/bobby-workspace" style=3D"display= | ||
: inline-block; padding: 13px 24px; background-color: #020617; color: #f8fa= | ||
fc; text-decoration: none; border-radius: 8px; margin: 0 4px;"> | ||
View workspace | ||
</a> | ||
=20 | ||
<a href=3D"http://test.com/templates/bobby-organization/bobby-templ= | ||
ate/versions/alpha" style=3D"display: inline-block; padding: 13px 24px; bac= | ||
kground-color: #020617; color: #f8fafc; text-decoration: none; border-radiu= | ||
s: 8px; margin: 0 4px;"> | ||
View template version | ||
</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=3Dd08= | ||
9fe7b-d5c5-4c0c-aaf5-689859f7d392" style=3D"color: #2563eb; text-decoration= | ||
: none;">Stop receiving emails like this</a></p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4-- |
2 changes: 1 addition & 1 deletion...rd/notifications/testdata/rendered-templates/webhook/TemplateWorkspaceCreated.json.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions...ications/testdata/rendered-templates/webhook/TemplateWorkspaceManuallyUpdated.json.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"_version": "1.1", | ||
"msg_id": "00000000-0000-0000-0000-000000000000", | ||
"payload": { | ||
"_version": "1.1", | ||
"notification_name": "Workspace Manually Updated", | ||
"notification_template_id": "00000000-0000-0000-0000-000000000000", | ||
"user_id": "00000000-0000-0000-0000-000000000000", | ||
"user_email": "bobby@coder.com", | ||
"user_name": "Bobby", | ||
"user_username": "bobby", | ||
"actions": [ | ||
{ | ||
"label": "View workspace", | ||
"url": "http://test.com/@bobby/bobby-workspace" | ||
}, | ||
{ | ||
"label": "View template version", | ||
"url": "http://test.com/templates/bobby-organization/bobby-template/versions/alpha" | ||
} | ||
], | ||
"labels": { | ||
"initiator": "bobby", | ||
"organization": "bobby-organization", | ||
"template": "bobby-template", | ||
"version": "alpha", | ||
"workspace": "bobby-workspace" | ||
}, | ||
"data": null | ||
}, | ||
"title": "Workspace 'bobby-workspace' has been manually updated", | ||
"title_markdown": "Workspace 'bobby-workspace' has been manually updated", | ||
"body": "Hello Bobby,\n\nA new workspace build has been manually created for your workspace bobby-workspace by bobby to update it to version alpha of template bobby-template.", | ||
"body_markdown": "Hello Bobby,\n\nA new workspace build has been manually created for your workspace **bobby-workspace** by **bobby** to update it to version **alpha** of template **bobby-template**." | ||
} |
146 changes: 121 additions & 25 deletionscoderd/workspacebuilds.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -27,6 +27,7 @@ import ( | ||
"github.com/coder/coder/v2/coderd/database/provisionerjobs" | ||
"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/provisionerdserver" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
@@ -333,37 +334,59 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { | ||
LogLevel(string(createBuild.LogLevel)). | ||
DeploymentValues(api.Options.DeploymentValues) | ||
var ( | ||
previousWorkspaceBuild database.WorkspaceBuild | ||
workspaceBuild *database.WorkspaceBuild | ||
provisionerJob *database.ProvisionerJob | ||
provisionerDaemons []database.GetEligibleProvisionerDaemonsByProvisionerJobIDsRow | ||
) | ||
err := api.Database.InTx(func(tx database.Store) error { | ||
var err error | ||
previousWorkspaceBuild, err = tx.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID) | ||
if err != nil && !xerrors.Is(err, sql.ErrNoRows) { | ||
api.Logger.Error(ctx, "failed fetching previous workspace build", slog.F("workspace_id", workspace.ID), slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching previous workspace build", | ||
Detail: err.Error(), | ||
}) | ||
return nil | ||
} | ||
if createBuild.TemplateVersionID != uuid.Nil { | ||
builder = builder.VersionID(createBuild.TemplateVersionID) | ||
} | ||
if createBuild.Orphan { | ||
if createBuild.Transition != codersdk.WorkspaceTransitionDelete { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Orphan is only permitted when deleting a workspace.", | ||
}) | ||
return nil | ||
} | ||
if len(createBuild.ProvisionerState) > 0 { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "ProvisionerState cannot be set alongside Orphan since state intent is unclear.", | ||
}) | ||
return nil | ||
} | ||
builder = builder.Orphan() | ||
} | ||
if len(createBuild.ProvisionerState) > 0 { | ||
builder = builder.State(createBuild.ProvisionerState) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
workspaceBuild, provisionerJob, provisionerDaemons, err = builder.Build( | ||
ctx, | ||
tx, | ||
func(action policy.Action, object rbac.Objecter) bool { | ||
return api.Authorize(r, action, object) | ||
}, | ||
audit.WorkspaceBuildBaggageFromRequest(r), | ||
) | ||
return err | ||
}, nil) | ||
var buildErr wsbuilder.BuildError | ||
if xerrors.As(err, &buildErr) { | ||
var authErr dbauthz.NotAuthorizedError | ||
@@ -420,6 +443,12 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// If this workspace build has a different template version ID to the previous build | ||
// we can assume it has just been updated. | ||
if createBuild.TemplateVersionID != uuid.Nil && createBuild.TemplateVersionID != previousWorkspaceBuild.TemplateVersionID { | ||
api.notifyWorkspaceUpdated(ctx, apiKey.UserID, workspace, createBuild.RichParameterValues) | ||
} | ||
api.publishWorkspaceUpdate(ctx, workspace.OwnerID, wspubsub.WorkspaceEvent{ | ||
Kind: wspubsub.WorkspaceEventKindStateChange, | ||
WorkspaceID: workspace.ID, | ||
@@ -428,6 +457,73 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(ctx, rw, http.StatusCreated, apiBuild) | ||
} | ||
func (api *API) notifyWorkspaceUpdated( | ||
ctx context.Context, | ||
initiatorID uuid.UUID, | ||
workspace database.Workspace, | ||
parameters []codersdk.WorkspaceBuildParameter, | ||
) { | ||
log := api.Logger.With(slog.F("workspace_id", workspace.ID)) | ||
template, err := api.Database.GetTemplateByID(ctx, workspace.TemplateID) | ||
if err != nil { | ||
log.Warn(ctx, "failed to fetch template for workspace creation notification", slog.F("template_id", workspace.TemplateID), slog.Error(err)) | ||
return | ||
} | ||
version, err := api.Database.GetTemplateVersionByID(ctx, template.ActiveVersionID) | ||
if err != nil { | ||
log.Warn(ctx, "failed to fetch template version for workspace creation notification", slog.F("template_id", workspace.TemplateID), slog.Error(err)) | ||
return | ||
} | ||
initiator, err := api.Database.GetUserByID(ctx, initiatorID) | ||
if err != nil { | ||
log.Warn(ctx, "failed to fetch user for workspace update notification", slog.F("initiator_id", initiatorID), slog.Error(err)) | ||
return | ||
} | ||
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID) | ||
if err != nil { | ||
log.Warn(ctx, "failed to fetch user for workspace update notification", slog.F("owner_id", workspace.OwnerID), slog.Error(err)) | ||
return | ||
} | ||
buildParameters := make([]map[string]any, len(parameters)) | ||
for idx, parameter := range parameters { | ||
buildParameters[idx] = map[string]any{ | ||
"name": parameter.Name, | ||
"value": parameter.Value, | ||
} | ||
} | ||
if _, err := api.NotificationsEnqueuer.EnqueueWithData( | ||
// nolint:gocritic // Need notifier actor to enqueue notifications | ||
dbauthz.AsNotifier(ctx), | ||
workspace.OwnerID, | ||
notifications.TemplateWorkspaceManuallyUpdated, | ||
map[string]string{ | ||
"organization": template.OrganizationName, | ||
"initiator": initiator.Name, | ||
"workspace": workspace.Name, | ||
"template": template.Name, | ||
"version": version.Name, | ||
}, | ||
map[string]any{ | ||
"workspace": map[string]any{"id": workspace.ID, "name": workspace.Name}, | ||
"template": map[string]any{"id": template.ID, "name": template.Name}, | ||
"template_version": map[string]any{"id": version.ID, "name": version.Name}, | ||
"owner": map[string]any{"id": owner.ID, "name": owner.Name}, | ||
"parameters": buildParameters, | ||
}, | ||
"api-workspaces-updated", | ||
// Associate this notification with all the related entities | ||
workspace.ID, workspace.OwnerID, workspace.TemplateID, workspace.OrganizationID, | ||
); err != nil { | ||
log.Warn(ctx, "failed to notify of workspace update", slog.Error(err)) | ||
} | ||
} | ||
// @Summary Cancel workspace build | ||
// @ID cancel-workspace-build | ||
// @Security CoderSessionToken | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.