|
| 1 | +name: Auto-merge agent PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + auto-merge: |
| 13 | + if: github.event.review.state == 'approved' |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Determine PR number |
| 19 | + id: pr |
| 20 | + run: echo "number=${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT" |
| 21 | + |
| 22 | + - name: Check merge conditions |
| 23 | + id: check |
| 24 | + env: |
| 25 | + GH_TOKEN: ${{ github.token }} |
| 26 | + PR_NUMBER: ${{ steps.pr.outputs.number }} |
| 27 | + run: | |
| 28 | + echo "Checking PR #$PR_NUMBER..." |
| 29 | +
|
| 30 | + # Must have agent-pr label or be from an agent branch |
| 31 | + BRANCH=$(gh pr view "$PR_NUMBER" --json headRefName --jq '.headRefName') |
| 32 | + LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name' 2>/dev/null || echo "") |
| 33 | +
|
| 34 | + if ! echo "$BRANCH" | grep -q "^agent/" && ! echo "$LABELS" | grep -q "agent-pr"; then |
| 35 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 36 | + echo "Skipping: not an agent branch or agent-pr label" |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | +
|
| 40 | + # Skip if from a fork |
| 41 | + IS_FORK=$(gh pr view "$PR_NUMBER" --json isCrossRepository --jq '.isCrossRepository') |
| 42 | + if [ "$IS_FORK" = "true" ]; then |
| 43 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 44 | + echo "Skipping: PR is from a fork" |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | +
|
| 48 | + # Must have at least one approval |
| 49 | + APPROVALS=$(gh pr view "$PR_NUMBER" --json reviews --jq '[.reviews[] | select(.state == "APPROVED")] | length') |
| 50 | + if [ "$APPROVALS" = "0" ]; then |
| 51 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 52 | + echo "Skipping: No approvals" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +
|
| 56 | + # All CI checks must pass (ignore this workflow) |
| 57 | + FAILING=$(gh pr checks "$PR_NUMBER" --json name,state --jq '[.[] | select(.state != "SUCCESS" and .state != "SKIPPED" and .name != "auto-merge")] | length' 2>/dev/null || echo "0") |
| 58 | + if [ "$FAILING" != "0" ]; then |
| 59 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 60 | + echo "Skipping: $FAILING CI checks not passing" |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 65 | + echo "All conditions met: agent branch + approved + CI green" |
| 66 | +
|
| 67 | + - name: Merge |
| 68 | + if: steps.check.outputs.skip != 'true' |
| 69 | + env: |
| 70 | + GH_TOKEN: ${{ github.token }} |
| 71 | + PR_NUMBER: ${{ steps.pr.outputs.number }} |
| 72 | + run: | |
| 73 | + gh pr merge "$PR_NUMBER" --squash --delete-branch |
| 74 | + echo "Merged PR #$PR_NUMBER" |
0 commit comments