Auto-merge #10601
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto-merge | |
| # Opt-in squash merge once all PR checks are green. Add the `automerge` label when the PR | |
| # is ready (Greptile 5/5, review done, etc.). Uses pull_request_target so fork PRs can merge | |
| # via GITHUB_TOKEN; the job only calls gh APIs and never executes PR head code. | |
| on: | |
| pull_request_target: | |
| types: [labeled, synchronize, reopened] | |
| branches: [main] | |
| workflow_run: | |
| workflows: | |
| - CI | |
| - CodeQL | |
| - Synthetic Deterministic Tests | |
| - Interactive Shell Live (PR + post-merge) | |
| types: [completed] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| concurrency: | |
| # PR number / commit SHA, not branch name: fork branches can share a name, | |
| # and a branch-name group would let one fork's run cancel another's. | |
| group: automerge-${{ github.event.pull_request.number || github.event.workflow_run.head_sha || github.run_id }} | |
| cancel-in-progress: true | |
| jobs: | |
| merge: | |
| name: Merge when CI is green | |
| runs-on: ubuntu-latest | |
| if: >- | |
| (github.event_name == 'workflow_run' && | |
| github.event.workflow_run.event == 'pull_request' && | |
| contains(fromJSON('["success", "neutral"]'), github.event.workflow_run.conclusion)) || | |
| (github.event_name == 'pull_request_target' && | |
| (github.event.action != 'labeled' || github.event.label.name == 'automerge')) | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - name: Resolve pull request number | |
| id: pr | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| EVENT_NAME: ${{ github.event_name }} | |
| PR_NUMBER: ${{ github.event.pull_request.number || '' }} | |
| HEAD_SHA: ${{ github.event.workflow_run.head_sha || '' }} | |
| run: | | |
| set -euo pipefail | |
| if [ "$EVENT_NAME" = "pull_request_target" ]; then | |
| echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| if [ -z "$HEAD_SHA" ]; then | |
| exit 0 | |
| fi | |
| # Resolve by commit SHA (not branch name): the commit-associated-PRs | |
| # endpoint is scoped to the exact commit, so a same-named branch in | |
| # a different fork can't resolve to the wrong PR. | |
| number="$(gh api "repos/$GITHUB_REPOSITORY/commits/$HEAD_SHA/pulls" --jq '[.[] | select(.state == "open" and .base.ref == "main")] | .[0].number // empty')" | |
| if [ -n "$number" ]; then | |
| echo "number=$number" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Merge labeled PR when checks pass | |
| if: steps.pr.outputs.number != '' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ steps.pr.outputs.number }} | |
| run: python3 .github/scripts/automerge_pr.py |