-
Notifications
You must be signed in to change notification settings - Fork 57
allow for stale branch cleanup (& dry run) #772
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
Open
mcncl
wants to merge
2
commits into
main
Choose a base branch
from
TE-5434/branch_cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matthewborden would you want
cleanupwithout args to clean up all branches, then if we supply--preflight-uuidwe only target that one? Or would you wantUUIDto be accepted as an optional positional arg, or allow the user to set--all?