Skip to content

Enable automerge and automatic branch cleanup for automated PRs - #56

Merged
Stensel8 merged 3 commits into
developmentfrom
claude/enable-automerge-branch-cleanup
Mar 29, 2026
Merged

Enable automerge and automatic branch cleanup for automated PRs#56
Stensel8 merged 3 commits into
developmentfrom
claude/enable-automerge-branch-cleanup

Conversation

@Claude

@Claude Claude AI commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

Repository had automated dependency PRs created but required manual approval and merge despite owner approval. Branches accumulated after merge.

Changes

Automerge Workflow (.github/workflows/automerge.yml)

  • Auto-approves PRs from Claude, github-actions[bot], or branches matching automated-update/* and claude/*
  • Enables GitHub automerge via GraphQL API with squash merge
  • Requires all checks passing, skips drafts (e.g., NGINX PRs needing SHA256 verification)
  • Graceful error handling with PR comments on failure

Branch Cleanup Workflow (.github/workflows/auto-delete-branch.yml)

  • Deletes branches automatically after PR merge
  • Protects main, master, development, staging, production
  • Handles forks, non-existent branches, and closed-without-merge cases

Documentation (.github/AUTOMERGE.md, README.md)

  • Workflow behavior, eligibility criteria, security model
  • Manual trigger instructions, troubleshooting guide
  • Integration with existing auto-update-dependencies.yml flow

Integration Flow

check-dependencies.yml → creates issue
auto-update-dependencies.yml → creates PR
automerge.yml → approves + enables automerge ← NEW
GitHub → merges when checks pass
auto-delete-branch.yml → deletes branch ← NEW

Existing open PRs #54 and #55 will be processed once workflows activate.

Claude AI and others added 2 commits March 29, 2026 12:51
Agent-Logs-Url: https://github.com/Stensel8/Scripts/sessions/df6edc3b-7b62-4694-b1ab-8e1558816123

Co-authored-by: Stensel8 <102481635+Stensel8@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Stensel8/Scripts/sessions/df6edc3b-7b62-4694-b1ab-8e1558816123

Co-authored-by: Stensel8 <102481635+Stensel8@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Enable automerge and auto remove branches Enable automerge and automatic branch cleanup for automated PRs Mar 29, 2026
@Claude
Claude AI requested a review from Stensel8 March 29, 2026 12:53
@Stensel8
Stensel8 marked this pull request as ready for review March 29, 2026 12:54
Copilot AI review requested due to automatic review settings March 29, 2026 12:54
@Stensel8
Stensel8 merged commit 102ada2 into development Mar 29, 2026
@Stensel8
Stensel8 deleted the claude/enable-automerge-branch-cleanup branch March 29, 2026 12:54
Stensel8 added a commit that referenced this pull request Mar 29, 2026
…#57)

* Initial plan

* Add automerge and auto-delete branch workflows

Agent-Logs-Url: https://github.com/Stensel8/Scripts/sessions/df6edc3b-7b62-4694-b1ab-8e1558816123



* Add comprehensive automerge documentation

Agent-Logs-Url: https://github.com/Stensel8/Scripts/sessions/df6edc3b-7b62-4694-b1ab-8e1558816123



---------

Co-authored-by: Claude <242468646+Claude@users.noreply.github.com>

Copilot AI left a comment

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.

Pull request overview

Adds GitHub Actions automation to auto-approve/enable automerge for trusted automated PRs and to delete merged branches afterward, with accompanying documentation updates.

Changes:

  • Introduces an automerge workflow that auto-approves eligible automation PRs and enables GitHub auto-merge (squash).
  • Introduces an auto-delete workflow to remove merged PR branches and optionally comment on the PR.
  • Documents the automation behavior and links it from the README.

Reviewed changes

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

File Description
README.md Documents the new automerge + branch cleanup behavior at a high level.
.github/workflows/automerge.yml New workflow to approve trusted automation PRs and enable GitHub automerge.
.github/workflows/auto-delete-branch.yml New workflow to delete merged branches (and comment on PRs).
.github/AUTOMERGE.md Detailed documentation for the new workflows and their intended operating model.

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


on:
pull_request:
types: [opened, ready_for_review, reopened]

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

This workflow only triggers on opened, reopened, and ready_for_review, but enabling automerge is gated on checks already being successful. If checks are still running at open time (common), should_enable_automerge becomes false and the workflow will never re-run when checks complete, so automerge never gets enabled. Consider enabling automerge regardless of current check state (GitHub will wait for required checks), and/or add triggers like pull_request: [synchronize] and check_suite: [completed]/workflow_run to retry.

Suggested change
types: [opened, ready_for_review, reopened]
types: [opened, ready_for_review, reopened, synchronize]

Copilot uses AI. Check for mistakes.
repo: context.repo.repo,
pull_number: prNumber,
event: 'APPROVE',
body: '✅ Automatically approved by automerge workflow.\n\nThis PR was created by trusted automation and has passed all checks.'

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

The auto-approval review body claims the PR "has passed all checks", but this step runs whenever should_approve is true and does not verify check status. Either gate approval on checks passing or change the message to avoid stating something that may be false.

Suggested change
body: '✅ Automatically approved by automerge workflow.\n\nThis PR was created by trusted automation and has passed all checks.'
body: '✅ Automatically approved by automerge workflow.\n\nThis PR was created by trusted automation.'

Copilot uses AI. Check for mistakes.
type: string

permissions:
contents: write

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

This workflow posts a PR comment via github.rest.issues.createComment, but permissions only include contents: write. With explicit permissions, commenting will likely fail without issues: write (or pull-requests: write if you switch to a PR-specific API). Add the needed permission or remove the comment step.

Suggested change
contents: write
contents: write
issues: write

Copilot uses AI. Check for mistakes.
Comment on lines +59 to +64
// Check if it's a head branch from a fork
const isFork = context.payload.pull_request?.head.repo?.full_name !== context.payload.repository?.full_name;
if (isFork) {
console.log('Branch is from a fork, cannot delete from this repository');
return;
}

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

isFork is computed even when context.payload.pull_request is absent (workflow_dispatch). In that case the left side is undefined, so undefined !== <repo full_name> evaluates to true and the script returns early, meaning manual branch deletion will never work. Only perform the fork check when a PR payload exists (or set isFork to false for workflow_dispatch).

Copilot uses AI. Check for mistakes.
Comment thread .github/AUTOMERGE.md
Comment on lines +221 to +222
- [Auto-Update Dependencies Workflow](../workflows/auto-update-dependencies.yml)
- [Check Dependencies Workflow](../workflows/check-dependencies.yml)

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

The relative links in "Related Documentation" point to ../workflows/..., which resolves to a non-existent top-level workflows/ directory from .github/AUTOMERGE.md. Update the links to the actual paths under .github/workflows/ (e.g., workflows/auto-update-dependencies.yml from this file, or .github/workflows/... from repo root).

Suggested change
- [Auto-Update Dependencies Workflow](../workflows/auto-update-dependencies.yml)
- [Check Dependencies Workflow](../workflows/check-dependencies.yml)
- [Auto-Update Dependencies Workflow](workflows/auto-update-dependencies.yml)
- [Check Dependencies Workflow](workflows/check-dependencies.yml)

Copilot uses AI. Check for mistakes.
Comment thread .github/AUTOMERGE.md
Comment on lines +110 to +115
Dependabot PRs are also eligible for automerge if:
- They pass all status checks
- The workflow approves them automatically

To disable automerge for Dependabot PRs, you can modify the eligibility check in `automerge.yml`.

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

Docs state Dependabot PRs are eligible for automerge, but the workflow eligibility check only allows Claude, github-actions[bot], or branch prefixes automated-update/ and claude/—it does not include dependabot[bot]. Either update the workflow to explicitly include Dependabot or adjust this documentation section to match current behavior.

Suggested change
Dependabot PRs are also eligible for automerge if:
- They pass all status checks
- The workflow approves them automatically
To disable automerge for Dependabot PRs, you can modify the eligibility check in `automerge.yml`.
Dependabot PRs follow the same eligibility rules as other PRs.
With the current configuration, Dependabot PRs are not treated as trusted automation for automerge by default.
To enable automerge for Dependabot PRs, you can modify the eligibility check in `automerge.yml` to include `dependabot[bot]` (for example, by treating it as a trusted author or by matching Dependabot-specific branch name patterns).

Copilot uses AI. Check for mistakes.
Comment thread .github/AUTOMERGE.md
### Permissions Required

- `contents: write` - To enable automerge
- `pull-requests: write` - To approve PRs and add comments

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

The "Permissions Required" section for the automerge workflow doesn’t mention the additional permissions needed for the current implementation (e.g., checks: read for checks.listForRef and issues: write for PR failure comments). After updating the workflow permissions, please update this section so operators can correctly configure least-privilege.

Suggested change
- `pull-requests: write` - To approve PRs and add comments
- `pull-requests: write` - To approve PRs
- `issues: write` - To post PR comments when automerge or checks fail
- `checks: read` - To read check statuses (via `checks.listForRef`) for eligibility

Copilot uses AI. Check for mistakes.
Comment thread .github/AUTOMERGE.md
Comment on lines +91 to +94
### Permissions Required

- `contents: write` - To delete branches

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

This workflow’s documentation says only contents: write is required, but the workflow also posts a PR comment via issues.createComment, which typically requires issues: write. Update the permissions section to reflect the actual required permissions (or remove the comment behavior).

Copilot uses AI. Check for mistakes.

permissions:
contents: write
pull-requests: write

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

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

Workflow uses github.rest.checks.listForRef and github.rest.issues.createComment, but the declared permissions only include contents: write and pull-requests: write. With explicit permissions, the token likely lacks checks: read and issues: write, causing 403s when listing check runs or posting the failure comment. Add the minimum required permissions (e.g., checks: read and issues: write) or adjust the workflow to avoid these APIs.

Suggested change
pull-requests: write
pull-requests: write
issues: write

Copilot uses AI. Check for mistakes.
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.

3 participants