-
Notifications
You must be signed in to change notification settings - Fork 1
Enable automerge and automatic branch cleanup for automated PRs #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,224 @@ | ||||||||||||||||||||||
| # Automerge and Branch Cleanup Documentation | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| This document describes the automatic PR approval, merge, and branch cleanup workflows in the Scripts repository. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Overview | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The repository includes two workflows that automate the lifecycle of Pull Requests created by trusted automation: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 1. **Automerge Workflow** (`.github/workflows/automerge.yml`) - Automatically approves and enables automerge for PRs | ||||||||||||||||||||||
| 2. **Auto Delete Branch Workflow** (`.github/workflows/auto-delete-branch.yml`) - Automatically deletes branches after PRs are merged | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Automerge Workflow | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Purpose | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Automatically approves and enables automerge for Pull Requests created by trusted automation sources, reducing manual overhead while maintaining quality control. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Triggers | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - **Automatic**: When a PR is opened, reopened, or marked ready for review | ||||||||||||||||||||||
| - **Manual**: Via workflow dispatch with a PR number input | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Eligible PRs | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| A PR is eligible for automerge if it meets ALL of the following criteria: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 1. **Created by trusted automation**: | ||||||||||||||||||||||
| - Author is `Claude` (Anthropic AI agent) | ||||||||||||||||||||||
| - Author is `github-actions[bot]` | ||||||||||||||||||||||
| - Branch name starts with `automated-update/` | ||||||||||||||||||||||
| - Branch name starts with `claude/` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 2. **Not a draft PR**: Draft PRs are skipped | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 3. **All checks passed**: All required status checks must pass | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Behavior | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 1. **Check Eligibility**: Verifies the PR meets automerge criteria | ||||||||||||||||||||||
| 2. **Approve PR**: Automatically approves the PR with a standardized message | ||||||||||||||||||||||
| 3. **Enable Automerge**: Uses GitHub's automerge feature with squash merge method | ||||||||||||||||||||||
| 4. **Error Handling**: Comments on the PR if automerge fails | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Configuration | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The workflow uses the following merge method: | ||||||||||||||||||||||
| - **Default**: `SQUASH` - Combines all commits into a single commit | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| To change the merge method, edit line 124 in `.github/workflows/automerge.yml`: | ||||||||||||||||||||||
| ```yaml | ||||||||||||||||||||||
| mergeMethod: 'SQUASH' # Options: MERGE, SQUASH, REBASE | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Permissions Required | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - `contents: write` - To enable automerge | ||||||||||||||||||||||
| - `pull-requests: write` - To approve PRs and add comments | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Auto Delete Branch Workflow | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Purpose | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Automatically cleans up branches after their Pull Requests are merged, keeping the repository tidy and preventing branch accumulation. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Triggers | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - **Automatic**: When a PR is closed (only deletes if merged) | ||||||||||||||||||||||
| - **Manual**: Via workflow dispatch with a branch name input | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Protected Branches | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The following branches are NEVER deleted: | ||||||||||||||||||||||
| - `main` | ||||||||||||||||||||||
| - `master` | ||||||||||||||||||||||
| - `development` | ||||||||||||||||||||||
| - `staging` | ||||||||||||||||||||||
| - `production` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Behavior | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 1. **Verify Merge**: Confirms the PR was actually merged (not just closed) | ||||||||||||||||||||||
| 2. **Check Protection**: Ensures the branch is not in the protected list | ||||||||||||||||||||||
| 3. **Delete Branch**: Removes the branch from the repository | ||||||||||||||||||||||
| 4. **Add Comment**: Posts a comment on the PR confirming deletion | ||||||||||||||||||||||
| 5. **Error Handling**: Gracefully handles cases where the branch doesn't exist | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Fork Handling | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Branches from forked repositories are NOT deleted, as the workflow only has permissions in the main repository. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Permissions Required | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - `contents: write` - To delete branches | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Comment on lines
+91
to
+94
|
||||||||||||||||||||||
| ## Integration with Existing Workflows | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Auto-Update Dependencies Workflow | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The automerge workflow works seamlessly with the existing dependency update automation: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 1. `check-dependencies.yml` creates an issue when a new version is detected | ||||||||||||||||||||||
| 2. `auto-update-dependencies.yml` creates a PR to update the dependency | ||||||||||||||||||||||
| 3. **NEW**: `automerge.yml` automatically approves and enables automerge | ||||||||||||||||||||||
| 4. GitHub merges the PR when all checks pass | ||||||||||||||||||||||
| 5. **NEW**: `auto-delete-branch.yml` deletes the branch after merge | ||||||||||||||||||||||
| 6. The original issue is automatically closed via `Closes #XX` in PR body | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Dependabot PRs | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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`. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
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`. | |
| 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
AI
Mar 29, 2026
There was a problem hiding this comment.
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).
| - [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) |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||
| name: Auto Delete Merged Branches | ||||||||
|
|
||||||||
| # This workflow automatically deletes branches after their PRs are merged | ||||||||
|
|
||||||||
| on: | ||||||||
| pull_request: | ||||||||
| types: [closed] | ||||||||
| workflow_dispatch: | ||||||||
| inputs: | ||||||||
| branch_name: | ||||||||
| description: 'Branch name to delete' | ||||||||
| required: true | ||||||||
| type: string | ||||||||
|
|
||||||||
| permissions: | ||||||||
| contents: write | ||||||||
|
||||||||
| contents: write | |
| contents: write | |
| issues: write |
Copilot
AI
Mar 29, 2026
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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: readforchecks.listForRefandissues: writefor PR failure comments). After updating the workflow permissions, please update this section so operators can correctly configure least-privilege.