Skip to content

feat: Allow user to cancel workspace jobs - #5115

Merged
mtojek merged 19 commits into
coder:mainfrom
mtojek:5035-allow-user-cancel
Nov 21, 2022
Merged

mtojek merged 19 commits into
coder:mainfrom
mtojek:5035-allow-user-cancel

Conversation

@mtojek

@mtojek mtojek commented Nov 17, 2022 •

Copy link
Copy Markdown
Member

Fixes: #5035

This PR adds a condition to workspacebuilds to allow/block cancel requests to workspace jobs. If the flag allow_user_cancel_workspace_jobs is disabled, only users with the "owner" role can cancel jobs.

Detailed changes:

  • add new DB column allow_user_cancel_workspace_jobs (default: true)
  • expose the field through template metadata
  • expose the field through Workspace API
  • add logic to allow/block cancel requests to workspacebuilds
  • UI changes

Screenshots:

Screenshot 2022-11-18 at 14 05 35

Screenshot 2022-11-18 at 14 05 05

@mtojek mtojek self-assigned this Nov 17, 2022
@mtojek mtojek changed the title Allow user to cancel workspace jobs feat: Allow user to cancel workspace jobs Nov 17, 2022
@mtojek
mtojek requested a review from a team November 18, 2022 13:29
@mtojek
mtojek marked this pull request as ready for review November 18, 2022 13:29
@mtojek
mtojek requested a review from a team as a code owner November 18, 2022 13:29
@mtojek
mtojek requested review from BrunoQuaresma and removed request for a team November 18, 2022 13:29

@BrunoQuaresma BrunoQuaresma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good. Just a few things:

  • I think you could use the field helper {...getFieldHelpers("name")} if it makes sense and works just to keep consistency
  • Would be nice to add this field to the form schema in line 47

I just commented on a few things but it is up to you.

@mtojek

mtojek commented Nov 18, 2022

Copy link
Copy Markdown
Member Author

Thank you for the review, Bruno.

I think you could use the field helper {...getFieldHelpers("name")} if it makes sense and works just to keep consistency

Checkbox doesn't have property helperText, which is processed by getFieldHelpers. I'm afraid that I can't use the field helper without hacking it.

For reference:

Warning: React does not recognize the helperText prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase helpertext instead. If you accidentally passed it from a parent component, remove it from the DOM element.

Would be nice to add this field to the form schema in line 47

Good catch! Fixed.

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend looks good 👍, a few minor suggestions.

Comment thread coderd/workspacebuilds.go Resolved
Comment thread coderd/workspacebuilds.go

valid, err := api.verifyUserCanCancelWorkspaceBuilds(ctx, httpmw.APIKey(r).UserID, workspace.TemplateID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Both errors returned by verify seem like known states and caused by a user doing a nonsense request (vs. server making a mistake), maybe unauthorized would be a good response?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unauthorized might be misleading while debugging potential customer issues. In theory, we don't expect an invalid user or an invalid template, and it doesn't look possible that the user passes it, hence an internal error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. After a second look I agree, didn't look closely enough at the inputs at first. 👍🏻

Comment thread codersdk/templates.go Outdated
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
DefaultTTLMillis int64 `json:"default_ttl_ms,omitempty"`
AllowUserCancelWorkspaceJobs bool `json:"allow_user_cancel_workspace_jobs"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be *bool as well so the value doesn’t need to be provided every request?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly speaking, I would leave it as is and fix it as part of #5066. This is the outcome of API design. If I fix it here, it will be confusing for customers (optional params vs "").

@mtojek
mtojek merged commit e86539d into coder:main Nov 21, 2022
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to disable unprivileged users from canceling workspace builds at the template level?

3 participants