mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: Allow user to cancel workspace jobs (#5115)
* Add database column allow_user_cancel_workspace_jobs * Adjust API * site: typesGenerated.ts * Expose template.allow_ in Workspaces API * Fix: site tests * Fix: make fmt/prettier * Fix: enterprise * Database tests * Add CLI tests * Add checkbox * i18n * Logic: block cancelling * Unit tests for conditional cancel * Fix: message * Address PR comment * Address PR comments * Fix: make
This commit is contained in:
+13
-10
@@ -13,11 +13,12 @@ import (
|
||||
|
||||
func templateEdit() *cobra.Command {
|
||||
var (
|
||||
name string
|
||||
displayName string
|
||||
description string
|
||||
icon string
|
||||
defaultTTL time.Duration
|
||||
name string
|
||||
displayName string
|
||||
description string
|
||||
icon string
|
||||
defaultTTL time.Duration
|
||||
allowUserCancelWorkspaceJobs bool
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
@@ -40,11 +41,12 @@ func templateEdit() *cobra.Command {
|
||||
|
||||
// NOTE: coderd will ignore empty fields.
|
||||
req := codersdk.UpdateTemplateMeta{
|
||||
Name: name,
|
||||
DisplayName: displayName,
|
||||
Description: description,
|
||||
Icon: icon,
|
||||
DefaultTTLMillis: defaultTTL.Milliseconds(),
|
||||
Name: name,
|
||||
DisplayName: displayName,
|
||||
Description: description,
|
||||
Icon: icon,
|
||||
DefaultTTLMillis: defaultTTL.Milliseconds(),
|
||||
AllowUserCancelWorkspaceJobs: allowUserCancelWorkspaceJobs,
|
||||
}
|
||||
|
||||
_, err = client.UpdateTemplateMeta(cmd.Context(), template.ID, req)
|
||||
@@ -61,6 +63,7 @@ func templateEdit() *cobra.Command {
|
||||
cmd.Flags().StringVarP(&description, "description", "", "", "Edit the template description")
|
||||
cmd.Flags().StringVarP(&icon, "icon", "", "", "Edit the template icon path")
|
||||
cmd.Flags().DurationVarP(&defaultTTL, "default-ttl", "", 0, "Edit the template default time before shutdown - workspaces created from this template to this value.")
|
||||
cmd.Flags().BoolVarP(&allowUserCancelWorkspaceJobs, "allow-user-cancel-workspace-jobs", "", true, "Allow users to cancel in-progress workspace jobs.")
|
||||
cliui.AllowSkipPrompt(cmd)
|
||||
|
||||
return cmd
|
||||
|
||||
@@ -2,6 +2,7 @@ package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -31,6 +32,8 @@ func TestTemplateEdit(t *testing.T) {
|
||||
desc := "lorem ipsum dolor sit amet et cetera"
|
||||
icon := "/icons/new-icon.png"
|
||||
defaultTTL := 12 * time.Hour
|
||||
allowUserCancelWorkspaceJobs := false
|
||||
|
||||
cmdArgs := []string{
|
||||
"templates",
|
||||
"edit",
|
||||
@@ -40,6 +43,7 @@ func TestTemplateEdit(t *testing.T) {
|
||||
"--description", desc,
|
||||
"--icon", icon,
|
||||
"--default-ttl", defaultTTL.String(),
|
||||
"--allow-user-cancel-workspace-jobs=" + strconv.FormatBool(allowUserCancelWorkspaceJobs),
|
||||
}
|
||||
cmd, root := clitest.New(t, cmdArgs...)
|
||||
clitest.SetupConfig(t, client, root)
|
||||
@@ -57,6 +61,7 @@ func TestTemplateEdit(t *testing.T) {
|
||||
assert.Equal(t, desc, updated.Description)
|
||||
assert.Equal(t, icon, updated.Icon)
|
||||
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
|
||||
assert.Equal(t, allowUserCancelWorkspaceJobs, updated.AllowUserCancelWorkspaceJobs)
|
||||
})
|
||||
t.Run("FirstEmptyThenNotModified", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -75,6 +80,7 @@ func TestTemplateEdit(t *testing.T) {
|
||||
"--description", template.Description,
|
||||
"--icon", template.Icon,
|
||||
"--default-ttl", (time.Duration(template.DefaultTTLMillis) * time.Millisecond).String(),
|
||||
"--allow-user-cancel-workspace-jobs=" + strconv.FormatBool(template.AllowUserCancelWorkspaceJobs),
|
||||
}
|
||||
cmd, root := clitest.New(t, cmdArgs...)
|
||||
clitest.SetupConfig(t, client, root)
|
||||
@@ -91,6 +97,7 @@ func TestTemplateEdit(t *testing.T) {
|
||||
assert.Equal(t, template.Description, updated.Description)
|
||||
assert.Equal(t, template.Icon, updated.Icon)
|
||||
assert.Equal(t, template.DefaultTTLMillis, updated.DefaultTTLMillis)
|
||||
assert.Equal(t, template.AllowUserCancelWorkspaceJobs, updated.AllowUserCancelWorkspaceJobs)
|
||||
})
|
||||
t.Run("InvalidDisplayName", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
Reference in New Issue
Block a user