Skip to content

ci: delete a PR's head branch after it merges, with an allowlist - #52

Closed
rishitank wants to merge 2 commits into
mainfrom
ci/branch-cleanup
Closed

rishitank wants to merge 2 commits into
mainfrom
ci/branch-cleanup

Conversation

@rishitank

Copy link
Copy Markdown
Owner

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 develop the first time it is merged as a PR head.

This workflow does the cleanup with an explicit allowlist instead.

What changed

  • New .github/workflows/branch-cleanup.yml. Self-contained, no reusable workflow (rishitank/.github main is ruleset-blocked, so a shared copy can't land yet).
    • Trigger: pull_request closed. The one job runs only when the PR was merged and its head is in this repository (never a fork's branch).
    • Keeps the head branch, with a ::notice::, when it is the default branch, or one of main, master, develop, staging, test, gh-pages, deploy-*, release/*, or when any open PR still uses it as its base.
    • Otherwise deletes it with gh api -X DELETE repos/$REPO/git/refs/heads/<ref>, the ref URL-encoded with jq @uri. "Reference does not exist" (422) or a 404 counts as success, since the branch is already gone.
    • Permissions: workflow default {}; the job gets contents: write (delete the ref) and pull-requests: read (the open-PR check). timeout-minutes: 2.
    • No ${{ }} in run:. Every event value reaches the shell through env: and is quoted.
  • Checked locally: actionlint and shellcheck are clean. The script was run against a stub gh for 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.
  • The live proof is ci: delete a PR's head branch after it merges, with an allowlist test#15: merging it should delete its own ci/branch-cleanup branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_012rYJxu978iUSGFZ1UuqTsi

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
@rishitank
rishitank enabled auto-merge (squash) October 9, 2026 01:42
@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 47 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: rishitank/animawatch/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: e929d12b-68d3-4f41-bccd-266a215f8b65
📥 Commits

Reviewing files that changed from the base of the PR and between 45c64ad and ae6583f.

📒 Files selected for processing (1)
  • .github/workflows/branch-cleanup.yml
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

… PRs

From CodeRabbit review: keep the branch when another open PR uses it as
its head; read the branch's current SHA first and keep it if it moved
past the merged PR head (REST has no conditional delete, so only the
instant between read and DELETE is unguarded); comment each permission.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012rYJxu978iUSGFZ1UuqTsi
@rishitank

Copy link
Copy Markdown
Owner Author

Closing as redundant. Merged branches are now handled account-wide by the nightly branch-prune workflow in rishitank/.github (it also switches on Automatically delete head branches for every repository), so a per-repository workflow isn't needed and would spend Actions minutes in private repositories. Closed, not merged; the branch is left in place.

🤖 Generated with Claude Code

@rishitank rishitank closed this Oct 10, 2026
auto-merge was automatically disabled October 10, 2026 20:42

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant