|
| 1 | +name: Auto-Merge PRs |
| 2 | + |
| 3 | +# Fires when a PR is opened/updated (to enable native auto-merge) and when a |
| 4 | +# check suite completes (to directly merge if every check went green and the |
| 5 | +# branch has no conflicts). |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened, ready_for_review] |
| 9 | + branches: [main] |
| 10 | + check_suite: |
| 11 | + types: [completed] |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write # needed to merge |
| 15 | + pull-requests: write # needed to update PR auto-merge flag |
| 16 | + |
| 17 | +jobs: |
| 18 | + # ── 1. Enable GitHub's built-in auto-merge the moment a PR is opened / updated |
| 19 | + enable-auto-merge: |
| 20 | + name: Enable auto-merge on PR |
| 21 | + if: | |
| 22 | + github.event_name == 'pull_request' && |
| 23 | + github.event.pull_request.draft == false |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Enable squash auto-merge |
| 27 | + # continue-on-error so a repo without branch-protection rules doesn't |
| 28 | + # block the workflow; the merge-when-green job is the primary mechanism. |
| 29 | + continue-on-error: true |
| 30 | + env: |
| 31 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + run: | |
| 33 | + gh pr merge --auto --squash \ |
| 34 | + --repo "${{ github.repository }}" \ |
| 35 | + "${{ github.event.pull_request.number }}" |
| 36 | +
|
| 37 | + # ── 2. Directly merge when ALL check runs on the suite pass ────────────── |
| 38 | + merge-when-green: |
| 39 | + name: Merge PR when all checks pass |
| 40 | + if: | |
| 41 | + github.event_name == 'check_suite' && |
| 42 | + github.event.check_suite.conclusion == 'success' |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - name: Merge passing, conflict-free PRs |
| 46 | + uses: actions/github-script@v7 |
| 47 | + with: |
| 48 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + script: | |
| 50 | + const headSha = context.payload.check_suite.head_sha; |
| 51 | + const prs = context.payload.check_suite.pull_requests ?? []; |
| 52 | +
|
| 53 | + if (prs.length === 0) { |
| 54 | + core.info('No open PRs linked to this check suite — nothing to do.'); |
| 55 | + return; |
| 56 | + } |
| 57 | +
|
| 58 | + for (const pr of prs) { |
| 59 | + // Fetch the full PR object (includes mergeable + draft fields) |
| 60 | + const { data: pull } = await github.rest.pulls.get({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + pull_number: pr.number, |
| 64 | + }); |
| 65 | +
|
| 66 | + if (pull.draft) { |
| 67 | + core.info(`PR #${pr.number} is a draft — skipping.`); |
| 68 | + continue; |
| 69 | + } |
| 70 | +
|
| 71 | + if (pull.state !== 'open') { |
| 72 | + core.info(`PR #${pr.number} is already closed — skipping.`); |
| 73 | + continue; |
| 74 | + } |
| 75 | +
|
| 76 | + // GitHub computes mergeability asynchronously; poll once if null. |
| 77 | + let mergeable = pull.mergeable; |
| 78 | + if (mergeable === null) { |
| 79 | + core.info(`PR #${pr.number}: mergeability not yet computed, waiting 6 s…`); |
| 80 | + await new Promise(r => setTimeout(r, 6000)); |
| 81 | + const { data: refreshed } = await github.rest.pulls.get({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + pull_number: pr.number, |
| 85 | + }); |
| 86 | + mergeable = refreshed.mergeable; |
| 87 | + } |
| 88 | +
|
| 89 | + if (mergeable === false) { |
| 90 | + core.info(`PR #${pr.number} has merge conflicts — skipping.`); |
| 91 | + continue; |
| 92 | + } |
| 93 | +
|
| 94 | + // Confirm every completed check run passed (or was neutral/skipped). |
| 95 | + const { data: { check_runs } } = await github.rest.checks.listForRef({ |
| 96 | + owner: context.repo.owner, |
| 97 | + repo: context.repo.repo, |
| 98 | + ref: headSha, |
| 99 | + per_page: 100, |
| 100 | + }); |
| 101 | +
|
| 102 | + const failing = check_runs.filter(r => |
| 103 | + r.status === 'completed' && |
| 104 | + !['success', 'skipped', 'neutral'].includes(r.conclusion) |
| 105 | + ); |
| 106 | +
|
| 107 | + if (failing.length > 0) { |
| 108 | + core.info( |
| 109 | + `PR #${pr.number} has failing checks ` + |
| 110 | + `(${failing.map(r => r.name).join(', ')}) — skipping.` |
| 111 | + ); |
| 112 | + continue; |
| 113 | + } |
| 114 | +
|
| 115 | + // Everything is green and conflict-free — merge! |
| 116 | + try { |
| 117 | + await github.rest.pulls.merge({ |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + pull_number: pr.number, |
| 121 | + merge_method: 'squash', |
| 122 | + commit_title: `${pull.title} (#${pr.number})`, |
| 123 | + commit_message: pull.body ?? '', |
| 124 | + }); |
| 125 | + core.info(`✅ Auto-merged PR #${pr.number}: ${pull.title}`); |
| 126 | + } catch (err) { |
| 127 | + core.error(`Failed to merge PR #${pr.number}: ${err.message}`); |
| 128 | + } |
| 129 | + } |
0 commit comments