Skip to content

feat: archive template versions to hide them from the ui #10086

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

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fc715fd
prune template versions
Emyrk Oct 5, 2023
611dac1
Implement fakes and mocks
Emyrk Oct 5, 2023
1de0a5d
Exclude template active versions
Emyrk Oct 5, 2023
3e5f91d
Delete versions are unusable
Emyrk Oct 5, 2023
9200779
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 5, 2023
6e6fbc3
migration bump
Emyrk Oct 5, 2023
2e2c092
Spaces to tabs
Emyrk Oct 5, 2023
7f3d805
add template version prune command
Emyrk Oct 9, 2023
02aa085
Rename to archive
Emyrk Oct 9, 2023
9a416c3
Rename to "archive"
Emyrk Oct 9, 2023
c82ff9a
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 9, 2023
c06d63a
Bump migration file
Emyrk Oct 9, 2023
460b0d2
Make update golden files
Emyrk Oct 9, 2023
d9bfea5
Fix swagger id
Emyrk Oct 9, 2023
655c906
Fix swagger accept json
Emyrk Oct 9, 2023
d4e54b2
Make gen
Emyrk Oct 9, 2023
938f256
Add unarchive
Emyrk Oct 9, 2023
7498c73
Add unarchive api
Emyrk Oct 9, 2023
685512a
Add cli command to unarchive a version
Emyrk Oct 9, 2023
f446ae5
Linting
Emyrk Oct 9, 2023
a9ff9d6
Update golden files
Emyrk Oct 9, 2023
b83a15e
Move cmd commands, allow archiuve deleted
Emyrk Oct 9, 2023
0ecbb79
update golden files
Emyrk Oct 9, 2023
bb3571b
Fix cli errors
Emyrk Oct 9, 2023
533e913
Move cmd
Emyrk Oct 9, 2023
02555b1
Implement fake
Emyrk Oct 9, 2023
1606b99
Linting and gen
Emyrk Oct 9, 2023
3818a57
fixup! Linting and gen
Emyrk Oct 9, 2023
48e44b6
fix: properly trim spaces so multi-line shebang executes (#10146)
kylecarbs Oct 9, 2023
3c97681
chore: reorganize storybook (#10144)
aslilac Oct 9, 2023
f01a4b3
chore: bump the golang-x group with 6 updates (#10128)
dependabot[bot] Oct 9, 2023
e9b4d15
chore: bump google.golang.org/api from 0.143.0 to 0.145.0 (#10130)
dependabot[bot] Oct 9, 2023
4236fc8
ci: bump the github-actions group with 2 updates (#10131)
dependabot[bot] Oct 9, 2023
8c86767
chore: add icons for popular programming languages (#10141)
aslilac Oct 9, 2023
02bcd20
chore: run `go mod tidy`
coadler Oct 9, 2023
91a2025
feat: add `external-auth` cli (#10052)
kylecarbs Oct 9, 2023
54509e5
feat: allow storing extra oauth token properties in the database (#10…
kylecarbs Oct 9, 2023
3fe8966
bump migration number:
Emyrk Oct 10, 2023
44bbbfe
Swagger annotations
Emyrk Oct 10, 2023
f0294c3
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 10, 2023
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
28 changes: 4 additions & 24 deletions cli/templatedelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,13 @@ func (r *RootCmd) templateDelete() *clibase.Cmd {
templates = append(templates, template)
}
} else {
allTemplates, err := client.TemplatesByOrganization(ctx, organization.ID)
template, err := selectTemplate(inv, client, organization)
if err != nil {
return xerrors.Errorf("get templates by organization: %w", err)
return err
}

if len(allTemplates) == 0 {
return xerrors.Errorf("no templates exist in the current organization %q", organization.Name)
}

opts := make([]string, 0, len(allTemplates))
for _, template := range allTemplates {
opts = append(opts, template.Name)
}

selection, err := cliui.Select(inv, cliui.SelectOptions{
Options: opts,
})
if err != nil {
return xerrors.Errorf("select template: %w", err)
}

for _, template := range allTemplates {
if template.Name == selection {
templates = append(templates, template)
templateNames = append(templateNames, template.Name)
}
}
templates = append(templates, template)
templateNames = append(templateNames, template.Name)
}

// Confirm deletion of the template.
Expand Down
37 changes: 35 additions & 2 deletions cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cli
import (
"time"

"github.com/google/uuid"

"github.com/coder/pretty"
"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
Expand Down Expand Up @@ -43,12 +43,45 @@ func (r *RootCmd) templates() *clibase.Cmd {
r.templateVersions(),
r.templateDelete(),
r.templatePull(),
r.archiveTemplateVersions(),
},
}

return cmd
}

func selectTemplate(inv *clibase.Invocation, client *codersdk.Client, organization codersdk.Organization) (codersdk.Template, error) {
var empty codersdk.Template
ctx := inv.Context()
allTemplates, err := client.TemplatesByOrganization(ctx, organization.ID)
if err != nil {
return empty, xerrors.Errorf("get templates by organization: %w", err)
}

if len(allTemplates) == 0 {
return empty, xerrors.Errorf("no templates exist in the current organization %q", organization.Name)
}

opts := make([]string, 0, len(allTemplates))
for _, template := range allTemplates {
opts = append(opts, template.Name)
}

selection, err := cliui.Select(inv, cliui.SelectOptions{
Options: opts,
})
if err != nil {
return empty, xerrors.Errorf("select template: %w", err)
}

for _, template := range allTemplates {
if template.Name == selection {
return template, nil
}
}
return empty, xerrors.Errorf("no template selected")
}

type templateTableRow struct {
// Used by json format:
Template codersdk.Template
Expand Down
184 changes: 184 additions & 0 deletions cli/templateversionarchive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package cli

import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/coder/pretty"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
)

func (r *RootCmd) unarchiveTemplateVersion() *clibase.Cmd {
return r.setArchiveTemplateVersion(false)
}

func (r *RootCmd) archiveTemplateVersion() *clibase.Cmd {
return r.setArchiveTemplateVersion(true)
}

//nolint:revive
func (r *RootCmd) setArchiveTemplateVersion(archive bool) *clibase.Cmd {
presentVerb := "archive"
pastVerb := "archived"
if !archive {
presentVerb = "unarchive"
pastVerb = "unarchived"
}

client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: presentVerb + " <template-name> [template-version-names...] ",
Short: strings.ToUpper(string(presentVerb[0])) + presentVerb[1:] + " a template version(s).",
Middleware: clibase.Chain(
r.InitClient(client),
),
Options: clibase.OptionSet{
cliui.SkipPromptOption(),
},
Handler: func(inv *clibase.Invocation) error {
var (
ctx = inv.Context()
versions []codersdk.TemplateVersion
)

organization, err := CurrentOrganization(inv, client)
if err != nil {
return err
}

if len(inv.Args) == 0 {
return xerrors.Errorf("missing template name")
}
if len(inv.Args) < 2 {
return xerrors.Errorf("missing template version name(s)")
}

templateName := inv.Args[0]
template, err := client.TemplateByName(ctx, organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
for _, versionName := range inv.Args[1:] {
version, err := client.TemplateVersionByOrganizationAndName(ctx, organization.ID, template.Name, versionName)
if err != nil {
return xerrors.Errorf("get template version by name %q: %w", versionName, err)
}
versions = append(versions, version)
}

for _, version := range versions {
if version.Archived == archive {
_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" already "+pastVerb),
)
continue
}

err := client.SetArchiveTemplateVersion(ctx, version.ID, archive)
if err != nil {
return xerrors.Errorf("%s template version %q: %w", presentVerb, version.Name, err)
}

_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" "+pastVerb+" at "+cliui.Timestamp(time.Now())),
)
}
return nil
},
}

return cmd
}

func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
var all clibase.Bool
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "archive [template-name...] ",
Short: "Archive unused failed template versions from a given template(s)",
Middleware: clibase.Chain(
r.InitClient(client),
),
Options: clibase.OptionSet{
cliui.SkipPromptOption(),
clibase.Option{
Name: "all",
Description: "Include all unused template versions. By default, only failed template versions are archived.",
Flag: "all",
Value: &all,
},
},
Handler: func(inv *clibase.Invocation) error {
var (
ctx = inv.Context()
templateNames = []string{}
templates = []codersdk.Template{}
)

organization, err := CurrentOrganization(inv, client)
if err != nil {
return err
}

if len(inv.Args) > 0 {
templateNames = inv.Args

for _, templateName := range templateNames {
template, err := client.TemplateByName(ctx, organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
templates = append(templates, template)
}
} else {
template, err := selectTemplate(inv, client, organization)
if err != nil {
return err
}

templates = append(templates, template)
templateNames = append(templateNames, template.Name)
}

// Confirm archive of the template.
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Archive template versions of these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))),
IsConfirm: true,
Default: cliui.ConfirmNo,
})
if err != nil {
return err
}

for _, template := range templates {
resp, err := client.ArchiveTemplateVersions(ctx, template.ID, all.Value())
if err != nil {
return xerrors.Errorf("archive template %q: %w", template.Name, err)
}

_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Archived %d versions from "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()), len(resp.ArchivedIDs)),
)

if ok, _ := inv.ParsedFlags().GetBool("verbose"); err == nil && ok {
data, err := json.Marshal(resp)
if err != nil {
return xerrors.Errorf("marshal verbose response: %w", err)
}
_, _ = fmt.Fprintln(
inv.Stdout, string(data),
)
}
}
return nil
},
}

return cmd
}
24 changes: 23 additions & 1 deletion cli/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"time"

"github.com/coder/pretty"

"github.com/google/uuid"
"golang.org/x/xerrors"

Expand All @@ -29,6 +31,8 @@ func (r *RootCmd) templateVersions() *clibase.Cmd {
},
Children: []*clibase.Cmd{
r.templateVersionsList(),
r.archiveTemplateVersion(),
r.unarchiveTemplateVersion(),
},
}

Expand All @@ -42,13 +46,23 @@ func (r *RootCmd) templateVersionsList() *clibase.Cmd {
)
client := new(codersdk.Client)

var includeArchived clibase.Bool

cmd := &clibase.Cmd{
Use: "list <template>",
Middleware: clibase.Chain(
clibase.RequireNArgs(1),
r.InitClient(client),
),
Short: "List all the versions of the specified template",
Options: clibase.OptionSet{
{
Name: "include-archived",
Description: "Include archived versions in the result list.",
Flag: "include-archived",
Value: &includeArchived,
},
},
Handler: func(inv *clibase.Invocation) error {
organization, err := CurrentOrganization(inv, client)
if err != nil {
Expand All @@ -59,7 +73,8 @@ func (r *RootCmd) templateVersionsList() *clibase.Cmd {
return xerrors.Errorf("get template by name: %w", err)
}
req := codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
TemplateID: template.ID,
IncludeArchived: includeArchived.Value(),
}

versions, err := client.TemplateVersionsByTemplate(inv.Context(), req)
Expand Down Expand Up @@ -92,6 +107,7 @@ type templateVersionRow struct {
CreatedBy string `json:"-" table:"created by"`
Status string `json:"-" table:"status"`
Active string `json:"-" table:"active"`
Archived string `json:"-" table:"archived"`
}

// templateVersionsToRows converts a list of template versions to a list of rows
Expand All @@ -104,13 +120,19 @@ func templateVersionsToRows(activeVersionID uuid.UUID, templateVersions ...coder
activeStatus = cliui.Keyword("Active")
}

archivedStatus := ""
if templateVersion.Archived {
archivedStatus = pretty.Sprint(cliui.DefaultStyles.Warn, "Archived")
}

rows[i] = templateVersionRow{
TemplateVersion: templateVersion,
Name: templateVersion.Name,
CreatedAt: templateVersion.CreatedAt,
CreatedBy: templateVersion.CreatedBy.Username,
Status: strings.Title(string(templateVersion.Job.Status)),
Active: activeStatus,
Archived: archivedStatus,
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/testdata/coder_templates_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ USAGE:
$ coder templates push my-template

SUBCOMMANDS:
archive Archive unused failed template versions from a given template(s)
create Create a template from the current directory or as specified by
flag
delete Delete templates
Expand Down
17 changes: 17 additions & 0 deletions cli/testdata/coder_templates_archive-template_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coder v0.0.0-devel

USAGE:
coder templates archive-template [flags] [template-name...]

Archive unused failed template versions from a given template(s)

OPTIONS:
--all bool
Include all unused template versions. By default, only failed template
versions are archived.

-y, --yes bool
Bypass prompts.

———
Run `coder --help` for a list of global options.
Loading