-
Notifications
You must be signed in to change notification settings - Fork 1
Enable automerge and automatic branch cleanup for automated PRs (#56) #57
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 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## 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 are **not** currently included in the automerge eligibility criteria and will **not** be auto-approved or auto-merged by default. | |
| To enable automerge for Dependabot PRs, update the eligibility check in `.github/workflows/automerge.yml` to treat Dependabot as a trusted automation source (for example, by allowing the `dependabot[bot]` author or `dependabot/` branch prefixes). | |
| If you prefer to keep Dependabot PRs manual-only, no changes to the workflow are required. |
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.
These links are broken: from .github/AUTOMERGE.md, ../workflows/... resolves to <repo>/workflows/... (which doesn't exist). Use ./workflows/... or an absolute repo path like .github/workflows/... so the links point to the actual workflow files.
| - [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.
For workflow_dispatch runs, context.payload.pull_request is undefined, so isFork becomes undefined !== <repoFullName> which evaluates to true. This makes manual branch deletions always skip. Compute the fork check only when running on a PR event (e.g., wrap it in if (context.payload.pull_request)), or default isFork to false for manual 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 doc references "edit line 124" to change merge method, but in the current
automerge.ymlthemergeMethod: 'SQUASH'setting is at a different line. Consider referencing the key name/section instead of a hard-coded line number (or update the line number to match).