- Notifications
You must be signed in to change notification settings - Fork1.1k
refactor: use task data model for notifications#20590
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
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
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 |
|---|---|---|
| @@ -18,6 +18,7 @@ type WorkspaceAgentScopeParams struct { | ||
| OwnerID uuid.UUID | ||
| TemplateID uuid.UUID | ||
| VersionID uuid.UUID | ||
| TaskID uuid.NullUUID | ||
| BlockUserData bool | ||
| } | ||
| @@ -42,6 +43,15 @@ func WorkspaceAgentScope(params WorkspaceAgentScopeParams) Scope { | ||
| panic("failed to expand scope, this should never happen") | ||
| } | ||
| // Include task in the allow list if the workspace has an associated task. | ||
| var extraAllowList []AllowListElement | ||
| if params.TaskID.Valid { | ||
| extraAllowList = append(extraAllowList, AllowListElement{ | ||
| Type: ResourceTask.Type, | ||
| ID: params.TaskID.UUID.String(), | ||
| }) | ||
| } | ||
| return Scope{ | ||
| // TODO: We want to limit the role too to be extra safe. | ||
| // Even though the allowlist blocks anything else, it is still good | ||
| @@ -52,12 +62,12 @@ func WorkspaceAgentScope(params WorkspaceAgentScopeParams) Scope { | ||
| // Limit the agent to only be able to access the singular workspace and | ||
| // the template/version it was created from. Add additional resources here | ||
| // as needed, but do not add more workspace or template resource ids. | ||
| AllowIDList:append([]AllowListElement{ | ||
| {Type: ResourceWorkspace.Type, ID: params.WorkspaceID.String()}, | ||
| {Type: ResourceTemplate.Type, ID: params.TemplateID.String()}, | ||
| {Type: ResourceTemplate.Type, ID: params.VersionID.String()}, | ||
| {Type: ResourceUser.Type, ID: params.OwnerID.String()}, | ||
| }, extraAllowList...), | ||
Comment on lines -55 to +70 Member 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. This is nice and narrow 👍 Just for future info, you can also do this: To give it access to all tasks. I assume each workspace just has 1 task though. And being narrow is better 👍 MemberAuthor 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. Yep, tasks and workspaces are 1:1 currently. Good to know about the wildcard though, thanks. 👍🏻 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -461,67 +461,55 @@ func (api *API) enqueueAITaskStateNotification( | ||
| return | ||
| } | ||
| if !workspace.TaskID.Valid { | ||
| // Workspace has no task ID, do nothing. | ||
| return | ||
| } | ||
| task, err := api.Database.GetTaskByID(ctx, workspace.TaskID.UUID) | ||
mafredri marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if err != nil { | ||
| api.Logger.Warn(ctx, "failed to get task", slog.Error(err)) | ||
| return | ||
| } | ||
| if !task.WorkspaceAppID.Valid || task.WorkspaceAppID.UUID != appID { | ||
| // Non-task app, do nothing. | ||
| return | ||
| } | ||
| // Skip if the latest persisted state equals the new state (no new transition) | ||
| if len(latestAppStatus) > 0 && latestAppStatus[0].State == database.WorkspaceAppStatusState(newAppStatus) { | ||
| return | ||
| } | ||
| // Skip the initial "Working" notification when task first starts. | ||
| // This is obvious to the user since they just created the task. | ||
| // We still notify on first "Idle" status and all subsequent transitions. | ||
| if len(latestAppStatus) == 0 && newAppStatus == codersdk.WorkspaceAppStatusStateWorking { | ||
| return | ||
| } | ||
| if _, err := api.NotificationsEnqueuer.EnqueueWithData( | ||
| // nolint:gocritic // Need notifier actor to enqueue notifications | ||
| dbauthz.AsNotifier(ctx), | ||
| workspace.OwnerID, | ||
| notificationTemplate, | ||
| map[string]string{ | ||
| "task": task.Name, | ||
| "workspace": workspace.Name, | ||
| }, | ||
| map[string]any{ | ||
| // Use a 1-minute bucketed timestamp to bypass per-day dedupe, | ||
| // allowing identical content to resend within the same day | ||
| // (but not more than once every 10s). | ||
| "dedupe_bypass_ts": api.Clock.Now().UTC().Truncate(time.Minute), | ||
| }, | ||
| "api-workspace-agent-app-status", | ||
| // Associate this notification with related entities | ||
| workspace.ID, workspace.OwnerID, workspace.OrganizationID, appID, | ||
| ); err != nil { | ||
| api.Logger.Warn(ctx, "failed to notify of task state", slog.Error(err)) | ||
| return | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.