Skip to content

feat: Add custom version names #4186

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func templateCreate() *cobra.Command {
}

type createValidTemplateVersionArgs struct {
Name string
Client *codersdk.Client
Organization codersdk.Organization
Provisioner database.ProvisionerType
Expand All @@ -162,6 +163,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
client := args.Client

req := codersdk.CreateTemplateVersionRequest{
Name: args.Name,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
StorageSource: args.FileHash,
Provisioner: codersdk.ProvisionerType(args.Provisioner),
Expand Down
3 changes: 3 additions & 0 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
func templatePush() *cobra.Command {
var (
directory string
versionName string
provisioner string
parameterFile string
alwaysPrompt bool
Expand Down Expand Up @@ -75,6 +76,7 @@ func templatePush() *cobra.Command {
spin.Stop()

job, _, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{
Name: versionName,
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
Expand Down Expand Up @@ -107,6 +109,7 @@ func templatePush() *cobra.Command {
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cmd.Flags().StringVarP(&versionName, "name", "", "", "Specify a name for the new template version. It will be automatically generated if not provided.")
cmd.Flags().BoolVar(&alwaysPrompt, "always-prompt", false, "Always prompt all parameters. Does not pull parameter values from active template version")
cliui.AllowSkipPrompt(cmd)
// This is for testing!
Expand Down
3 changes: 2 additions & 1 deletion cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestTemplatePush(t *testing.T) {
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
})
cmd, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
cmd, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
Expand Down Expand Up @@ -153,6 +153,7 @@ func TestTemplatePush(t *testing.T) {
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
require.Equal(t, "example", templateVersions[1].Name)
})

t.Run("UseWorkingDir", func(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,17 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
}
}

if req.Name == "" {
req.Name = namesgenerator.GetRandomName(1)
}

templateVersion, err = db.InsertTemplateVersion(ctx, database.InsertTemplateVersionParams{
ID: uuid.New(),
TemplateID: templateID,
OrganizationID: organization.ID,
CreatedAt: database.Now(),
UpdatedAt: database.Now(),
Name: namesgenerator.GetRandomName(1),
Name: req.Name,
Readme: "",
JobID: provisionerJob.ID,
CreatedBy: uuid.NullUUID{
Expand Down
4 changes: 3 additions & 1 deletion coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {

file, err := client.Upload(ctx, codersdk.ContentTypeTar, data)
require.NoError(t, err)
_, err = client.CreateTemplateVersion(ctx, user.OrganizationID, codersdk.CreateTemplateVersionRequest{
version, err := client.CreateTemplateVersion(ctx, user.OrganizationID, codersdk.CreateTemplateVersionRequest{
Name: "bananas",
StorageMethod: codersdk.ProvisionerStorageMethodFile,
StorageSource: file.Hash,
Provisioner: codersdk.ProvisionerTypeEcho,
Expand All @@ -105,6 +106,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
}},
})
require.NoError(t, err)
require.Equal(t, "bananas", version.Name)

require.Len(t, auditor.AuditLogs, 1)
assert.Equal(t, database.AuditActionCreate, auditor.AuditLogs[0].Action)
Expand Down
1 change: 1 addition & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Organization struct {

// CreateTemplateVersionRequest enables callers to create a new Template Version.
type CreateTemplateVersionRequest struct {
Name string `json:"name,omitempty" validate:"omitempty,template_name"`
// TemplateID optionally associates a version with a template.
TemplateID uuid.UUID `json:"template_id,omitempty"`

Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export interface CreateTemplateVersionDryRunRequest {

// From codersdk/organizations.go
export interface CreateTemplateVersionRequest {
readonly name?: string
readonly template_id?: string
readonly storage_method: ProvisionerStorageMethod
readonly storage_source: string
Expand Down