Skip to content

feat: add support for template version messages in api and cli #8336

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 13 commits into from
Jul 11, 2023
Prev Previous commit
Next Next commit
detect newlines
  • Loading branch information
mafredri committed Jul 6, 2023
commit 93de18a0ce5ddaddcbb4d7bef52b845113323009
7 changes: 6 additions & 1 deletion cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"path/filepath"
"strings"
"time"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -117,9 +118,13 @@ func (pf *templateUploadFlags) checkForLockfile(inv *clibase.Invocation) error {
}

func (pf *templateUploadFlags) templateMessage(inv *clibase.Invocation) string {
if len(pf.message) > 72 {
title := strings.SplitN(pf.message, "\n", 2)[0]
if len(title) > 72 {
cliui.Warn(inv.Stdout, "Template message is longer than 72 characters, it will be displayed as truncated.")
}
if title != pf.message {
cliui.Warn(inv.Stdout, "Template message contains newlines, only the first line will be displayed.")
}
if pf.message != "" {
return pf.message
}
Expand Down
44 changes: 25 additions & 19 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,36 @@ func TestTemplatePush(t *testing.T) {
ProvisionApply: echo.ProvisionComplete,
})

wantMessage := strings.Repeat("a", 73)

inv, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example", "--message", wantMessage, "--yes")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

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

inv = inv.WithContext(ctx)
w := clitest.StartWithWaiter(t, inv)
for i, tt := range []struct {
wantMessage string
wantMatch string
}{
{wantMessage: strings.Repeat("a", 73), wantMatch: "Template message is longer than 72 characters"},
{wantMessage: "This is my title\n\nAnd this is my body.", wantMatch: "Template message contains newlines"},
} {
inv, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example", "--message", tt.wantMessage, "--yes")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

pty.ExpectMatchContext(ctx, "Template message is longer than 72 characters")
inv = inv.WithContext(ctx)
w := clitest.StartWithWaiter(t, inv)

w.RequireSuccess()
pty.ExpectMatchContext(ctx, tt.wantMatch)

// Assert that the template version changed.
templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
require.Equal(t, wantMessage, templateVersions[1].Message)
w.RequireSuccess()

// Assert that the template version changed.
templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
require.NoError(t, err)
assert.Len(t, templateVersions, 2+i)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1+i].ID)
require.Equal(t, tt.wantMessage, templateVersions[1+i].Message)
}
})

t.Run("NoLockfile", func(t *testing.T) {
Expand Down