Enable automerge and automatic branch cleanup for automated PRs - #56
Conversation
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>
…#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>
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
| types: [opened, ready_for_review, reopened] | |
| types: [opened, ready_for_review, reopened, synchronize] |
| 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.' |
There was a problem hiding this comment.
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.
| 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.' |
| type: string | ||
|
|
||
| permissions: | ||
| contents: write |
There was a problem hiding this comment.
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.
| contents: write | |
| contents: write | |
| issues: write |
| // 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; | ||
| } |
There was a problem hiding this comment.
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).
| - [Auto-Update Dependencies Workflow](../workflows/auto-update-dependencies.yml) | ||
| - [Check Dependencies Workflow](../workflows/check-dependencies.yml) |
There was a problem hiding this comment.
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).
| - [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) |
| 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`. | ||
|
|
There was a problem hiding this comment.
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.
| 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). |
| ### Permissions Required | ||
|
|
||
| - `contents: write` - To enable automerge | ||
| - `pull-requests: write` - To approve PRs and add comments |
There was a problem hiding this comment.
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.
| - `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 |
| ### Permissions Required | ||
|
|
||
| - `contents: write` - To delete branches | ||
|
|
There was a problem hiding this comment.
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).
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write |
There was a problem hiding this comment.
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.
| pull-requests: write | |
| pull-requests: write | |
| issues: write |
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)Claude,github-actions[bot], or branches matchingautomated-update/*andclaude/*Branch Cleanup Workflow (
.github/workflows/auto-delete-branch.yml)main,master,development,staging,productionDocumentation (
.github/AUTOMERGE.md,README.md)auto-update-dependencies.ymlflowIntegration Flow
Existing open PRs #54 and #55 will be processed once workflows activate.