8000 feat: archive template versions to hide them from the ui by Emyrk · Pull Request #10086 · coder/coder · GitHub
[go: up one dir, main page]

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 1 commit
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 8000
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
Prev Previous commit
Next Next commit
Move cmd commands, allow archiuve deleted
  • Loading branch information
Emyrk committed Oct 9, 2023
commit b83a15e1a73458d5b92949190e3d5c76dcc05936
1 change: 0 additions & 1 deletion cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (r *RootCmd) templates() *clibase.Cmd {
r.templateDelete(),
r.templatePull(),
r.archiveTemplateVersions(),
r.archiveTemplateVersion(),
},
}

Expand Down
31 changes: 26 additions & 5 deletions cli/templateversionarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (r *RootCmd) archiveTemplateVersion() *clibase.Cmd {

client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "archive-version <template-name> [template-version-names...] ",
Use: "archive <template-name> [template-version-names...] ",
Short: "Archive or unarchive a template version(s).",
Middleware: clibase.Chain(
r.InitClient(client),
Expand Down Expand Up @@ -68,17 +68,31 @@ func (r *RootCmd) archiveTemplateVersion() *clibase.Cmd {
if unarchive {
verb = "unarchived"
}
failed := 0
for _, version := range versions {
if version.Archived == !unarchive.Value() {
_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" already "+verb),
)
continue
}

err := client.SetArchiveTemplateVersion(ctx, version.ID, !unarchive.Value())
if err != nil {
return xerrors.Errorf("set archive to %t template versions for %q: %w", !unarchive, template.Name, err)
failed++
_, _ = fmt.Fprintln(inv.Stderr, fmt.Sprintf("Failed to archive template version %q: %s", version.Name,
pretty.Sprint(cliui.DefaultStyles.Error, err.Error())))
continue
}

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

if failed > 0 {
return xerrors.Errorf("failed on %d template versions", failed)
}
return nil
},
}
Expand All @@ -90,7 +104,7 @@ func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
var all clibase.Bool
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "archive-template [template-name...] ",
Use: "archive [template-name...] ",
Short: "Archive unused failed template versions from a given template(s)",
Middleware: clibase.Chain(
r.InitClient(client),
Expand Down Expand Up @@ -146,14 +160,18 @@ func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
return err
}

failed := 0
for _, template := range templates {
resp, err := client.ArchiveTemplateVersions(ctx, template.ID, all.Value())
if err != nil {
return xerrors.Errorf("archive template versions for %q: %w", template.Name, err)
_, _ = fmt.Fprintln(inv.Stderr, fmt.Sprintf("Failed to archive template versions for %q: %s", template.Name,
pretty.Sprint(cliui.DefaultStyles.Error, err.Error())))
failed++
continue
}

_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Archive %s versions from "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()), len(resp.ArchivedIDs)),
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 {
Expand All @@ -167,6 +185,9 @@ func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
}
}

if failed > 0 {
return xerrors.Errorf("failed on %d templates", failed)
}
return nil
},
}
Expand Down
23 changes: 22 additions & 1 deletion cli/templateversions.go
Original file line number Diff line number Diff line change
< 8000 svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-fold-up"> 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,7 @@ func (r *RootCmd) templateVersions() *clibase.Cmd {
},
Children: []*clibase.Cmd{
r.templateVersionsList(),
r.archiveTemplateVersion(),
},
}

Expand All @@ -42,13 +45,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 +72,8 @@ func (r *RootCmd) templateVersionsList() *clibase.Cmd {
return xerrors.Errorf("get template by name: %w", err)
}
req := codersdk.TemplateVersionsByTemplateRequ 67E6 est{
TemplateID: template.ID,
TemplateID: template.ID,
IncludeArchived: includeArchived.Value(),
}

versions, err := client.TemplateVersionsByTemplate(inv.Context(), req)
Expand Down Expand Up @@ -92,6 +106,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 +119,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: 0 additions & 1 deletion coderd/database/querier.go

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

7 changes: 2 additions & 5 deletions coderd/database/queries.sql.go

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

6 changes: 2 additions & 4 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ FROM
ORDER BY workspace_id, build_number DESC
) AS used_versions
WHERE
-- TODO: This is an issue for "deleted workspaces", since a deleted workspace
-- has a build with the transition "delete". This will prevent that template
-- version from ever being archived. We need a method to archive deleted workspaces.
-- used_versions.transition != 'delete',
used_versions.transition != 'delete'
AND
scoped_template_versions.id = used_versions.template_version_id
)
-- Also never archive the active template version
Expand Down
2 changes: 1 addition & 1 deletion coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func (api *API) setArchiveTemplateVersion(archive bool) func(rw http.ResponseWri
err = archiveError
} else {
if len(archived) == 0 {
err = sql.ErrNoRows
err = xerrors.New("Unable to archive specified version, the version is likely in use by a workspace or currently set to the active version")
}
}
} else {
Expand Down
0