Skip to content

fix(auto-merge): delete head branch after app-merged PRs#30

Merged
github-actions[bot] merged 1 commit into
mainfrom
claude/automerge-branch-cleanup
Jul 4, 2026
Merged

fix(auto-merge): delete head branch after app-merged PRs#30
github-actions[bot] merged 1 commit into
mainfrom
claude/automerge-branch-cleanup

Conversation

@ANcpLua

@ANcpLua ANcpLua commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Complete-and-verified

Gap: repo-level delete_branch_on_merge fires for user-initiated merges but not for merges landed by the github-actions app via native auto-merge. Observed in qyl today: claude/ branches of #452–#456 and #477–#479 (all app-merged) survived; the user-merged #476 was auto-deleted. That's why merged automation branches accumulate across the fleet.

Fix: templates/auto-merge.yml (+ this repo's own copy) gains a delete-merged-branch job on pull_request_target: closed:

  • guarded to merged == true, same-repo head, and the claude//copilot/ prefix policy
  • existing enable-auto-merge job now skips the closed action
  • HEAD_REF passed via env and never interpolated into the script — no shell injection under pull_request_target's write token
  • idempotent (|| echo) for branches already gone via the repo setting

Propagation: next weekly enforce-repo-settings sweep replaces drifted per-repo copies; I'll dispatch it manually after merge so the fleet converges today.

🤖 Generated with Claude Code

delete_branch_on_merge fires for user merges but not for merges landed by
the github-actions app via native auto-merge — observed fleet-wide (qyl
claude/ branches from #452-#456 and #477-#479 all survived their merges;
the user-merged #476 was deleted). Add a delete-merged-branch job on the
closed event, scoped to same-repo claude/ and copilot/ heads, with the
ref passed via env so pull_request_target cannot be shell-injected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 4, 2026 06:28
@github-actions github-actions Bot merged commit 0dae7e9 into main Jul 4, 2026
2 of 3 checks passed
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key: "tools"
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 65582a8c-fb05-4b6c-8c8b-080f36458a79

📥 Commits

Reviewing files that changed from the base of the PR and between 28dd707 and 86811ca.

📒 Files selected for processing (2)
  • .github/workflows/auto-merge.yml
  • templates/auto-merge.yml

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to data retention organization setting


📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Automatically removes merged automation branches after native auto-merge completes, helping keep branch lists clean.
    • Supports this cleanup for trusted automation branches in the usual auto-merge flow.
  • Bug Fixes

    • Prevents auto-merge setup from running during merge-completed events.
    • Improves reliability when merged branches are not deleted automatically.

Walkthrough

Both the reusable GitHub Actions workflow and its template counterpart are updated to handle closed pull request events. The enable-auto-merge job is gated to skip closed events, and a new delete-merged-branch job deletes merged head branches for claude/ and copilot/ prefixed branches via the GitHub CLI.

Changes

Auto-merge branch cleanup

Layer / File(s) Summary
Trigger and gating updates
.github/workflows/auto-merge.yml, templates/auto-merge.yml
Adds closed to the pull_request_target trigger types, adds explanatory comments, and updates enable-auto-merge's condition to exclude closed events.
Delete-merged-branch job
.github/workflows/auto-merge.yml, templates/auto-merge.yml
Introduces a new job that runs on closed+merged PRs from claude/ or copilot/ branches, deleting the head branch via gh api -X DELETE using a HEAD_REF env var and tolerating already-deleted branches.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant GitHub
  participant AutoMergeWorkflow
  participant DeleteMergedBranchJob
  participant GhCli

  GitHub->>AutoMergeWorkflow: pull_request_target (closed, merged=true)
  AutoMergeWorkflow->>DeleteMergedBranchJob: verify head branch prefix (claude/ or copilot/)
  DeleteMergedBranchJob->>GhCli: gh api -X DELETE (HEAD_REF)
  GhCli-->>DeleteMergedBranchJob: branch deleted or already absent
Loading

Related Issues: None specified.

Related PRs: None specified.

Suggested labels: ci, automation, workflow

Suggested reviewers: ANcpLua

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/automerge-branch-cleanup
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/automerge-branch-cleanup
  • 🛠️ repair automation
  • 🛠️ cleanup stale review automation

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a workflow-level cleanup step to ensure automation PR head branches (e.g., claude/, copilot/) are deleted after GitHub Actions app–landed native auto-merges, closing a gap where the repo-level delete_branch_on_merge setting doesn’t reliably delete app-merged branches across the fleet.

Changes:

  • Extend pull_request_target triggers to include closed.
  • Gate the existing enable-auto-merge job to skip closed events.
  • Add a delete-merged-branch job that runs on merged PR close events and deletes same-repo claude/ / copilot/ head branches.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
templates/auto-merge.yml Adds closed trigger + new delete-merged-branch cleanup job to the fleet template.
.github/workflows/auto-merge.yml Applies the same cleanup behavior in this repo’s workflow copy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread templates/auto-merge.yml
Comment on lines +67 to +69
run: |
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/$HEAD_REF" \
|| echo "head branch already deleted"
Comment on lines +67 to +69
run: |
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/$HEAD_REF" \
|| echo "head branch already deleted"
@ANcpLua ANcpLua deleted the claude/automerge-branch-cleanup branch July 4, 2026 06:31

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86811cafa0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread templates/auto-merge.yml
on:
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]
types: [opened, synchronize, reopened, ready_for_review, closed]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Don’t rely on closed events from GITHUB_TOKEN auto-merges

For the app-merged PRs this is meant to clean up, the merge is caused by this workflow’s secrets.GITHUB_TOKEN auto-merge call, and GitHub documents that GITHUB_TOKEN-triggered events do not create workflow runs except workflow_dispatch, repository_dispatch, and pull_request opened/synchronize/reopened; closed is explicitly excluded (docs). Adding closed here therefore will not start the new deletion job for the github-actions native auto-merges whose branches are surviving, so the fleet cleanup remains ineffective unless the merge/delete path uses a PAT/App token or another dispatch/wait mechanism.

Useful? React with 👍 / 👎.

Comment thread templates/auto-merge.yml
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/$HEAD_REF" \
|| echo "head branch already deleted"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fail on branch deletion errors other than 404

When this job does run, this catch-all makes the workflow succeed for every gh api failure, not only the already-deleted branch case; GitHub CLI documents that failed commands return a nonzero exit code (docs), and the delete-ref API has distinct failure statuses such as 409/422 (docs). If a token permission, ruleset, API validation, or transient service failure prevents deletion, the step still ends green after printing “head branch already deleted,” so branch cleanup can silently do nothing.

Useful? React with 👍 / 👎.

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.

2 participants