- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: add template delete notification#14250
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
13 commits Select commitHold shift + click to select a range
5915daa feat: add template delete notification
BrunoQuaresma76ee1f6 Merge branch 'main' of https://github.com/coder/coder into bq/notify-…
BrunoQuaresma1f7046c Notify all template admins
BrunoQuaresma30faf4f Apply Dannys suggestions
BrunoQuaresma1e11cb7 Fix lint
BrunoQuaresma99c1950 Merge branch 'main' of https://github.com/coder/coder into bq/notify-…
BrunoQuaresmabe31ed8 Fix migration
BrunoQuaresma9e6462d Merge branch 'main' of https://github.com/coder/coder into bq/notify-…
BrunoQuaresma7fb0fa2 Improve tests
BrunoQuaresma92c11de Filter delete notifications
BrunoQuaresma450235e Improve test
BrunoQuaresmaeaf87c5 Fix test
BrunoQuaresma4688765 Add missing can render test
BrunoQuaresmaFile 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
Empty file.
22 changes: 22 additions & 0 deletionscoderd/database/migrations/000244_notifications_delete_template.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,22 @@ | ||
| INSERT INTO | ||
| notification_templates ( | ||
| id, | ||
| name, | ||
| title_template, | ||
| body_template, | ||
| "group", | ||
| actions | ||
| ) | ||
| VALUES ( | ||
| '29a09665-2a4c-403f-9648-54301670e7be', | ||
| 'Template Deleted', | ||
| E'Template "{{.Labels.name}}" deleted', | ||
| E'Hi {{.UserName}}\n\nThe template **{{.Labels.name}}** was deleted by **{{ .Labels.initiator }}**.', | ||
| 'Template Events', | ||
| '[ | ||
| { | ||
| "label": "View templates", | ||
| "url": "{{ base_url }}/templates" | ||
| } | ||
| ]'::jsonb | ||
| ); |
5 changes: 5 additions & 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
11 changes: 11 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
60 changes: 60 additions & 0 deletionscoderd/templates.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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package coderd | ||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| @@ -12,12 +13,15 @@ import ( | ||
| "github.com/google/uuid" | ||
| "golang.org/x/xerrors" | ||
| "cdr.dev/slog" | ||
| "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/database/dbtime" | ||
| "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/coderd/schedule" | ||
| @@ -56,6 +60,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) { | ||
| // @Router /templates/{template} [delete] | ||
| func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) { | ||
| var ( | ||
| apiKey = httpmw.APIKey(r) | ||
| ctx = r.Context() | ||
| template = httpmw.TemplateParam(r) | ||
| auditor = *api.Auditor.Load() | ||
| @@ -101,11 +106,47 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) { | ||
| }) | ||
| return | ||
| } | ||
| admins, err := findTemplateAdmins(ctx, api.Database) | ||
| if err != nil { | ||
| httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
| Message: "Internal error fetching template admins.", | ||
| Detail: err.Error(), | ||
| }) | ||
| return | ||
| } | ||
| for _, admin := range admins { | ||
| // Don't send notification to user which initiated the event. | ||
| if admin.ID == apiKey.UserID { | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| continue | ||
| } | ||
| api.notifyTemplateDeleted(ctx, template, apiKey.UserID, admin.ID) | ||
| } | ||
| httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{ | ||
| Message: "Template has been deleted!", | ||
| }) | ||
| } | ||
| func (api *API) notifyTemplateDeleted(ctx context.Context, template database.Template, initiatorID uuid.UUID, receiverID uuid.UUID) { | ||
| initiator, err := api.Database.GetUserByID(ctx, initiatorID) | ||
| if err != nil { | ||
| api.Logger.Warn(ctx, "failed to fetch initiator for template deletion notification", slog.F("initiator_id", initiatorID), slog.Error(err)) | ||
| return | ||
| } | ||
| if _, err := api.NotificationsEnqueuer.Enqueue(ctx, receiverID, notifications.TemplateTemplateDeleted, | ||
| map[string]string{ | ||
| "name": template.Name, | ||
| "initiator": initiator.Username, | ||
| }, "api-templates-delete", | ||
| // Associate this notification with all the related entities. | ||
| template.ID, template.OrganizationID, | ||
| ); err != nil { | ||
| api.Logger.Warn(ctx, "failed to notify of template deletion", slog.F("deleted_template_id", template.ID), slog.Error(err)) | ||
| } | ||
| } | ||
| // Create a new template in an organization. | ||
| // Returns a single template. | ||
| // | ||
| @@ -948,3 +989,22 @@ func (api *API) convertTemplate( | ||
| MaxPortShareLevel: maxPortShareLevel, | ||
| } | ||
| } | ||
| // findTemplateAdmins fetches all users with template admin permission including owners. | ||
| func findTemplateAdmins(ctx context.Context, store database.Store) ([]database.GetUsersRow, error) { | ||
| // Notice: we can't scrape the user information in parallel as pq | ||
| // fails with: unexpected describe rows response: 'D' | ||
| owners, err := store.GetUsers(ctx, database.GetUsersParams{ | ||
| RbacRole: []string{codersdk.RoleOwner}, | ||
| }) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get owners: %w", err) | ||
| } | ||
| templateAdmins, err := store.GetUsers(ctx, database.GetUsersParams{ | ||
| RbacRole: []string{codersdk.RoleTemplateAdmin}, | ||
| }) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get template admins: %w", err) | ||
| } | ||
| return append(owners, templateAdmins...), nil | ||
| } | ||
96 changes: 96 additions & 0 deletionscoderd/templates_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
2 changes: 1 addition & 1 deletioncoderd/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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3441,7 +3441,7 @@ func TestWorkspaceUsageTracking(t *testing.T) { | ||
| }) | ||
| } | ||
| funcTestWorkspaceNotifications(t *testing.T) { | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| t.Parallel() | ||
| t.Run("Dormant", func(t *testing.T) { | ||
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.