- Notifications
You must be signed in to change notification settings - Fork927
fix: send workspace create/update notifications to template admins only#16071
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
DanielleMaywood merged 6 commits intomainfromdm-send-workspace-created-update-notifications-to-template-admins-onlyJan 15, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
6a54591
fix: send workspace create/update notifications to template admins only
DanielleMaywoodba68c67
chore: improve comments
DanielleMaywood3e695ad
Merge branch 'main' into dm-send-workspace-created-update-notificatio…
DanielleMaywood7a38242
chore: 'owner' -> 'first user'
DanielleMaywood3811133
chore: log instead of returning
DanielleMaywood66bf5b9
chore: appease linter
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
18 changes: 16 additions & 2 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
60 changes: 43 additions & 17 deletionscoderd/workspacebuilds_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -565,30 +565,31 @@ func TestWorkspaceBuildResources(t *testing.T) { | ||
func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T) { | ||
t.Parallel() | ||
t.Run("NoRepeatedNotifications", func(t *testing.T) { | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
t.Parallel() | ||
notify := ¬ificationstest.FakeEnqueuer{} | ||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, NotificationsEnqueuer: notify}) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdminClient, templateAdmin := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
userClient, user := coderdtest.CreateAnotherUser(t, client, first.OrganizationID) | ||
// Create a template with an initial version | ||
version := coderdtest.CreateTemplateVersion(t,templateAdminClient, first.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t,templateAdminClient, version.ID) | ||
template := coderdtest.CreateTemplate(t,templateAdminClient, first.OrganizationID, version.ID) | ||
// Create a workspace using this template | ||
workspace := coderdtest.CreateWorkspace(t, userClient, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, workspace.LatestBuild.ID) | ||
coderdtest.MustTransitionWorkspace(t, userClient, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop) | ||
// Create a new version of the template | ||
newVersion := coderdtest.CreateTemplateVersion(t,templateAdminClient, first.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) { | ||
ctvr.TemplateID = template.ID | ||
}) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t,templateAdminClient, newVersion.ID) | ||
// Create a workspace build using this new template version | ||
build := coderdtest.CreateWorkspaceBuild(t, userClient, workspace, database.WorkspaceTransitionStart, func(cwbr *codersdk.CreateWorkspaceBuildRequest) { | ||
@@ -597,21 +598,45 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, build.ID) | ||
coderdtest.MustTransitionWorkspace(t, userClient, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop) | ||
// Create the workspace build _again_. We are doing this to | ||
// ensure we do not create _another_ notification. This is | ||
// separate to the notifications subsystem dedupe mechanism | ||
// as this build shouldn't create a notification. It shouldn't | ||
// create another notification as this new build isn't changing | ||
// the template version. | ||
build = coderdtest.CreateWorkspaceBuild(t, userClient, workspace, database.WorkspaceTransitionStart, func(cwbr *codersdk.CreateWorkspaceBuildRequest) { | ||
cwbr.TemplateVersionID = newVersion.ID | ||
}) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, build.ID) | ||
coderdtest.MustTransitionWorkspace(t, userClient, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// We're going to have two notifications (one for the first user and one for the template admin) | ||
// By ensuring we only have these two, we are sure the second build didn't trigger more | ||
// notifications. | ||
sent := notify.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceManuallyUpdated)) | ||
require.Len(t, sent, 2) | ||
receivers := make([]uuid.UUID, len(sent)) | ||
for idx, notif := range sent { | ||
receivers[idx] = notif.UserID | ||
} | ||
// Check the notification was sent to the first user and template admin | ||
// (both of whom have the "template admin" role), and explicitly not the | ||
// workspace owner (since they initiated the workspace build). | ||
require.Contains(t, receivers, templateAdmin.ID) | ||
require.Contains(t, receivers, first.UserID) | ||
require.NotContains(t, receivers, user.ID) | ||
require.Contains(t, sent[0].Targets, template.ID) | ||
require.Contains(t, sent[0].Targets, workspace.ID) | ||
require.Contains(t, sent[0].Targets, workspace.OrganizationID) | ||
require.Contains(t, sent[0].Targets, workspace.OwnerID) | ||
require.Contains(t, sent[1].Targets, template.ID) | ||
require.Contains(t, sent[1].Targets, workspace.ID) | ||
require.Contains(t, sent[1].Targets, workspace.OrganizationID) | ||
require.Contains(t, sent[1].Targets, workspace.OwnerID) | ||
}) | ||
t.Run("ToCorrectUser", func(t *testing.T) { | ||
@@ -621,23 +646,24 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T) | ||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, NotificationsEnqueuer: notify}) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdminClient, templateAdmin := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
userClient, _ := coderdtest.CreateAnotherUser(t, client, first.OrganizationID) | ||
// Create a template with an initial version | ||
version := coderdtest.CreateTemplateVersion(t,templateAdminClient, first.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t,templateAdminClient, version.ID) | ||
template := coderdtest.CreateTemplate(t,templateAdminClient, first.OrganizationID, version.ID) | ||
// Create a workspace using this template | ||
workspace := coderdtest.CreateWorkspace(t, userClient, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, workspace.LatestBuild.ID) | ||
coderdtest.MustTransitionWorkspace(t, userClient, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop) | ||
// Create a new version of the template | ||
newVersion := coderdtest.CreateTemplateVersion(t,templateAdminClient, first.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) { | ||
ctvr.TemplateID = template.ID | ||
}) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t,templateAdminClient, newVersion.ID) | ||
// Create a workspace build using this new template version from a different user | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
@@ -652,7 +678,7 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T) | ||
// Ensure we receive only 1 workspace manually updated notification and to the right user | ||
sent := notify.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceManuallyUpdated)) | ||
require.Len(t, sent, 1) | ||
require.Equal(t,templateAdmin.ID, sent[0].UserID) | ||
require.Contains(t, sent[0].Targets, template.ID) | ||
require.Contains(t, sent[0].Targets, workspace.ID) | ||
require.Contains(t, sent[0].Targets, workspace.OrganizationID) | ||
21 changes: 17 additions & 4 deletionscoderd/workspaces.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
37 changes: 27 additions & 10 deletionscoderd/workspaces_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
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.