Skip to content
Merged
Prev Previous commit
Next Next commit
feat(coderd): allow template version message to be set to empty
  • Loading branch information
mafredri committed Jul 12, 2023
commit e01ac01a8ed812700a5115686050b96ac9a8815e
4 changes: 2 additions & 2 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (api *API) patchTemplateVersion(rw http.ResponseWriter, r *http.Request) {
updateParams.Name = params.Name
}

if params.Message != "" {
updateParams.Message = params.Message
if params.Message != nil {
updateParams.Message = *params.Message
}

errTemplateVersionNameConflict := xerrors.New("template version name must be unique for a template")
Expand Down
65 changes: 65 additions & 0 deletions coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,71 @@ func TestTemplateVersionPatch(t *testing.T) {
assert.NotEqual(t, updatedVersion.Name, version.Name)
})

t.Run("Update the message", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = "Example message"
})
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

wantMessage := "Updated message"
updatedVersion, err := client.UpdateTemplateVersion(ctx, version.ID, codersdk.PatchTemplateVersionRequest{
Message: &wantMessage,
})

require.NoError(t, err)
assert.Equal(t, wantMessage, updatedVersion.Message)
})

t.Run("Remove the message", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = "Example message"
})
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

wantMessage := ""
updatedVersion, err := client.UpdateTemplateVersion(ctx, version.ID, codersdk.PatchTemplateVersionRequest{
Message: &wantMessage,
})

require.NoError(t, err)
assert.Equal(t, wantMessage, updatedVersion.Message)
})

t.Run("Keep the message", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
wantMessage := "Example message"
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = wantMessage
})
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

t.Log(version.Message)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

updatedVersion, err := client.UpdateTemplateVersion(ctx, version.ID, codersdk.PatchTemplateVersionRequest{
Message: nil,
})

require.NoError(t, err)
assert.Equal(t, wantMessage, updatedVersion.Message)
})

t.Run("Use the same name if a new name is not passed", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
4 changes: 2 additions & 2 deletions codersdk/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type TemplateVersionVariable struct {
}

type PatchTemplateVersionRequest struct {
Name string `json:"name" validate:"omitempty,template_version_name"`
Message string `json:"message" validate:"omitempty"`
Name string `json:"name" validate:"omitempty,template_version_name"`
Message *string `json:"message,omitempty" validate:"omitempty,lt=1048577"`
}

// TemplateVersion returns a template version by ID.
Expand Down