Repository navigation
ci: delete a PR's head branch after it merges, with an allowlist - #95
Conversation
GitHub has no account-wide setting for this, the per-repo toggle deletes every merged head branch, and Free-plan private repos cannot protect long-lived branches such as develop. This self-contained workflow runs on merged same-repo PRs and deletes the head branch unless it is the default branch, one of main/master/develop/staging/test/gh-pages/ deploy-*/release/*, or the base of another open PR. A branch that is already gone counts as success. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012rYJxu978iUSGFZ1UuqTsi
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (1)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📜 Recent review details⏰ Context from checks skipped due to timeout. (11)
🧰 Additional context used🪛 zizmor (1.30.1).github/workflows/branch-cleanup.yml[warning] 30-30: permissions without explanatory comments (undocumented-permissions): needs an explanatory comment (undocumented-permissions) [info] 25-25: workflow or action definition without a name (anonymous-definition): this job (anonymous-definition) [warning] 18-20: insufficient job-level concurrency limits (concurrency-limits): workflow is missing concurrency setting (concurrency-limits) 🔇 Additional comments (1)
📝 SummarySummary by CodeRabbit
WalkthroughAdds a workflow that runs when a pull request closes. If the pull request was merged and its head branch belongs to the current repository, the workflow preserves specified branches and deletes eligible head branches. ChangesMerged branch cleanup
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Feature Merge Risk: ⚪ Minimal · up to The workflow is ready to merge after normal checks; the reported deletion concern remains unconfirmed. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Apprentice, the merged branch leaves the stage, Comment |
Why
Merged PR branches pile up. GitHub has no account-wide setting to delete them: "Automatically delete head branches" is a per-repository toggle, and it deletes every merged head branch with no exceptions. Private repositories on the Free plan can't protect branches, so that toggle could delete a long-lived branch such as
developthe first time it is merged as a PR head.This workflow does the cleanup with an explicit allowlist instead.
What changed
.github/workflows/branch-cleanup.yml. Self-contained, no reusable workflow (rishitank/.githubmain is ruleset-blocked, so a shared copy can't land yet).pull_requestclosed. The one job runs only when the PR was merged and its head is in this repository (never a fork's branch).::notice::, when it is the default branch, or one ofmain,master,develop,staging,test,gh-pages,deploy-*,release/*, or when any open PR still uses it as its base.gh api -X DELETE repos/$REPO/git/refs/heads/<ref>, the ref URL-encoded withjq @uri. "Reference does not exist" (422) or a 404 counts as success, since the branch is already gone.{}; the job getscontents: write(delete the ref) andpull-requests: read(the open-PR check).timeout-minutes: 2.${{ }}inrun:. Every event value reaches the shell throughenv:and is quoted.ghfor the default branch, each allowlisted name, a branch with open child PRs, a ref containing/, a space and#, an already-deleted ref (422 and 404), and a 403, which fails the job.ci/branch-cleanupbranch.🤖 Generated with Claude Code
https://claude.ai/code/session_012rYJxu978iUSGFZ1UuqTsi