Skip to content

Commit

Permalink
feat: Add template display name (backend) (#4966)
Browse files Browse the repository at this point in the history
* Rename to nameValidator

* Refactor: NameValid

* Fix: comment

* Define new migration

* Include display_name

* Update typesGenerated.ts

* Update meta

* Adjust tests

* CLI tests

* Fix: audit

* Fix: omitempty

* site: display_name is optional

* unit: TestUsernameValid

* entities.ts: add display_name

* site: TemplateSettingsPage.test.tsx

* Fix: TemplateSettingsForm.tsx

* Adjust tests

* Add comment to display_name column

* Fix: rename

* Fix: make

* Loosen regexp

* Fix: err check

* Fix: template name length

* Allow for whitespaces

* Update migration number
  • Loading branch information
mtojek committed Nov 10, 2022
1 parent f3eb662 commit 2042b57
Show file tree
Hide file tree
Showing 23 changed files with 218 additions and 56 deletions.
3 changes: 3 additions & 0 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func templateEdit() *cobra.Command {
var (
name string
displayName string
description string
icon string
defaultTTL time.Duration
Expand All @@ -40,6 +41,7 @@ func templateEdit() *cobra.Command {
// NOTE: coderd will ignore empty fields.
req := codersdk.UpdateTemplateMeta{
Name: name,
DisplayName: displayName,
Description: description,
Icon: icon,
DefaultTTLMillis: defaultTTL.Milliseconds(),
Expand All @@ -55,6 +57,7 @@ func templateEdit() *cobra.Command {
}

cmd.Flags().StringVarP(&name, "name", "", "", "Edit the template name")
cmd.Flags().StringVarP(&displayName, "display-name", "", "", "Edit the template display name")
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.")
Expand Down
56 changes: 42 additions & 14 deletions cli/templateedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/testutil"
)

func TestTemplateEdit(t *testing.T) {
Expand All @@ -23,14 +23,11 @@ func TestTemplateEdit(t *testing.T) {
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.Description = "original description"
ctr.Icon = "/icons/default-icon.png"
ctr.DefaultTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
name := "new-template-name"
displayName := "New Display Name 789"
desc := "lorem ipsum dolor sit amet et cetera"
icon := "/icons/new-icon.png"
defaultTTL := 12 * time.Hour
Expand All @@ -39,37 +36,35 @@ func TestTemplateEdit(t *testing.T) {
"edit",
template.Name,
"--name", name,
"--display-name", displayName,
"--description", desc,
"--icon", icon,
"--default-ttl", defaultTTL.String(),
}
cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
ctx, _ := testutil.Context(t)
err := cmd.ExecuteContext(ctx)

require.NoError(t, err)

// Assert that the template metadata changed.
updated, err := client.Template(context.Background(), template.ID)
require.NoError(t, err)
assert.Equal(t, name, updated.Name)
assert.Equal(t, displayName, updated.DisplayName)
assert.Equal(t, desc, updated.Description)
assert.Equal(t, icon, updated.Icon)
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
})

t.Run("NotModified", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.Description = "original description"
ctr.Icon = "/icons/default-icon.png"
ctr.DefaultTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
cmdArgs := []string{
Expand All @@ -84,7 +79,8 @@ func TestTemplateEdit(t *testing.T) {
cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
ctx, _ := testutil.Context(t)
err := cmd.ExecuteContext(ctx)

require.ErrorContains(t, err, "not modified")

Expand All @@ -96,4 +92,36 @@ func TestTemplateEdit(t *testing.T) {
assert.Equal(t, template.Icon, updated.Icon)
assert.Equal(t, template.DefaultTTLMillis, updated.DefaultTTLMillis)
})
t.Run("InvalidDisplayName", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
cmdArgs := []string{
"templates",
"edit",
template.Name,
"--name", template.Name,
"--display-name", " a-b-c",
}
cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)

ctx, _ := testutil.Context(t)
err := cmd.ExecuteContext(ctx)

require.Error(t, err, "client call must fail")
_, isSdkError := codersdk.AsError(err)
require.True(t, isSdkError, "sdk error is expected")

// Assert that the template metadata did not change.
updated, err := client.Template(context.Background(), template.ID)
require.NoError(t, err)
assert.Equal(t, template.Name, updated.Name)
assert.Equal(t, "", template.DisplayName)
})
}
1 change: 1 addition & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
}
tpl.UpdatedAt = database.Now()
tpl.Name = arg.Name
tpl.DisplayName = arg.DisplayName
tpl.Description = arg.Description
tpl.Icon = arg.Icon
tpl.DefaultTtl = arg.DefaultTtl
Expand Down
5 changes: 4 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE templates DROP COLUMN display_name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE templates ADD COLUMN display_name VARCHAR(64) NOT NULL DEFAULT '';

COMMENT ON COLUMN templates.display_name
IS 'Display name is a custom, human-friendly template name that user can set.';
2 changes: 2 additions & 0 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ INSERT INTO
created_by,
icon,
user_acl,
group_acl
group_acl,
display_name
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *;

-- name: UpdateTemplateActiveVersionByID :exec
UPDATE
Expand Down Expand Up @@ -100,7 +101,8 @@ SET
description = $3,
default_ttl = $4,
name = $5,
icon = $6
icon = $6,
display_name = $7
WHERE
id = $1
RETURNING
Expand Down
2 changes: 1 addition & 1 deletion coderd/gitauth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ConvertConfig(entries []codersdk.GitAuthConfig, accessURL *url.URL) ([]*Con
// Default to the type.
entry.ID = string(typ)
}
if valid := httpapi.UsernameValid(entry.ID); valid != nil {
if valid := httpapi.NameValid(entry.ID); valid != nil {
return nil, xerrors.Errorf("git auth provider %q doesn't have a valid id: %w", entry.ID, valid)
}

Expand Down

0 comments on commit 2042b57

Please sign in to comment.