Skip to content
Open
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
99 changes: 99 additions & 0 deletions cmd/preflight/cleanup_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package preflight

import (
"context"
"errors"
"fmt"
"os"
"time"

"github.com/alecthomas/kong"

"github.com/buildkite/cli/v3/internal/cli"
bkErrors "github.com/buildkite/cli/v3/internal/errors"
"github.com/buildkite/cli/v3/internal/preflight"
)

// CleanupCmd deletes remote bk/preflight/* branches whose builds have completed.
type CleanupCmd struct {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should allow providing a specific preflight UUID to target.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewborden would you want cleanup without args to clean up all branches, then if we supply --preflight-uuidwe only target that one? Or would you want UUID to be accepted as an optional positional arg, or allow the user to set --all?

Pipeline string `help:"The pipeline to check builds against. This can be a {pipeline slug} or in the format {org slug}/{pipeline slug}." short:"p"`
DryRun bool `help:"Show which branches would be deleted without actually deleting them." name:"dry-run"`
Text bool `help:"Use plain text output instead of interactive terminal UI." xor:"output"`
JSON bool `help:"Emit one JSON object per event (JSONL)." xor:"output"`
}

func (c *CleanupCmd) Help() string {
return `Deletes remote bk/preflight/* branches whose builds have completed (passed, failed, canceled). Branches with in-progress builds are left untouched to avoid interrupting concurrent preflight runs.`
}

func (c *CleanupCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
pCtx, err := setup(c.Pipeline, globals)
if err != nil {
return err
}
defer pCtx.Stop()

ctx := pCtx.Ctx
repoRoot := pCtx.RepoRoot
resolvedPipeline := pCtx.Pipeline

branches, err := preflight.ListRemotePreflightBranches(repoRoot, globals.EnableDebug())
if err != nil {
return bkErrors.NewInternalError(err, "failed to list remote preflight branches")
}

if len(branches) == 0 {
fmt.Fprintln(os.Stdout, "No preflight branches found")
return nil
}

renderer := newRenderer(os.Stdout, c.JSON, c.Text, pCtx.Stop)
defer renderer.Close()

_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: fmt.Sprintf("Found %d preflight branch(es), checking build status...", len(branches))})

if err := preflight.ResolveBuilds(ctx, pCtx.Factory.RestAPIClient, resolvedPipeline.Org, resolvedPipeline.Name, branches); err != nil {
if errors.Is(err, context.Canceled) {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: "Cleanup interrupted"})
return nil
}
return bkErrors.NewInternalError(err, "failed to check build status for preflight branches")
}

var toDelete []string
var deleted, skipped int
for i := range branches {
bb := branches[i]
if !bb.IsCompleted() {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: fmt.Sprintf("Skipping %s (build state: %s)", bb.Branch, bb.Build.State)})
skipped++
continue
}

state := "no build found"
if bb.Build != nil {
state = bb.Build.State
}

if c.DryRun {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: fmt.Sprintf("Would delete %s (%s)", bb.Branch, state)})
} else {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: fmt.Sprintf("Deleting %s (%s)", bb.Branch, state)})
toDelete = append(toDelete, bb.Ref)
}
deleted++
}

if !c.DryRun && len(toDelete) > 0 {
if err := preflight.CleanupRefs(repoRoot, toDelete, globals.EnableDebug()); err != nil {
return bkErrors.NewInternalError(err, "failed to delete preflight branches from remote")
}
}

verb := "deleted"
if c.DryRun {
verb = "would delete"
}
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), Title: fmt.Sprintf("Cleanup complete: %d %s, %d skipped", deleted, verb, skipped)})
return nil
}
Loading