|
| 1 | +name: Auto Approve |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger when check runs complete |
| 5 | + check_suite: |
| 6 | + types: [completed] |
| 7 | + # Also trigger on workflow run completion for reusable workflows |
| 8 | + workflow_run: |
| 9 | + workflows: ["PR Tests", "Claude Code Review"] |
| 10 | + types: [completed] |
| 11 | + |
| 12 | +permissions: read-all |
| 13 | + |
| 14 | +jobs: |
| 15 | + auto-approve: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + # Only run on pull requests, not pushes |
| 18 | + if: | |
| 19 | + github.event.check_suite.pull_requests[0] != null || |
| 20 | + github.event.workflow_run.pull_requests[0] != null |
| 21 | + permissions: |
| 22 | + pull-requests: write |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Get PR number |
| 26 | + id: pr |
| 27 | + run: | |
| 28 | + if [ "${{ github.event_name }}" == "check_suite" ]; then |
| 29 | + PR_NUMBER="${{ github.event.check_suite.pull_requests[0].number }}" |
| 30 | + else |
| 31 | + PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" |
| 32 | + fi |
| 33 | + echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 34 | + echo "PR number: $PR_NUMBER" |
| 35 | +
|
| 36 | + - name: Check required statuses |
| 37 | + id: check |
| 38 | + env: |
| 39 | + GH_TOKEN: ${{ github.token }} |
| 40 | + run: | |
| 41 | + PR_NUMBER="${{ steps.pr.outputs.number }}" |
| 42 | +
|
| 43 | + if [ -z "$PR_NUMBER" ]; then |
| 44 | + echo "No PR number found, skipping" |
| 45 | + echo "should_approve=false" >> $GITHUB_OUTPUT |
| 46 | + exit 0 |
| 47 | + fi |
| 48 | +
|
| 49 | + # Get PR head SHA |
| 50 | + HEAD_SHA=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER --jq '.head.sha') |
| 51 | + echo "Head SHA: $HEAD_SHA" |
| 52 | +
|
| 53 | + # Check Claude review status |
| 54 | + CLAUDE_STATUS=$(gh api repos/${{ github.repository }}/commits/$HEAD_SHA/check-runs --jq '.check_runs[] | select(.name == "claude-review") | .conclusion' | head -1) |
| 55 | + echo "Claude review status: $CLAUDE_STATUS" |
| 56 | +
|
| 57 | + # Check Unity Tests status (commit status, not check run) |
| 58 | + UNITY_STATUS=$(gh api repos/${{ github.repository }}/commits/$HEAD_SHA/status --jq '.statuses[] | select(.context == "Unity Tests") | .state' | head -1) |
| 59 | + echo "Unity Tests status: $UNITY_STATUS" |
| 60 | +
|
| 61 | + # If Unity Tests doesn't exist (skipped scenario), check if Skip Unity Tests completed |
| 62 | + if [ -z "$UNITY_STATUS" ]; then |
| 63 | + SKIP_STATUS=$(gh api repos/${{ github.repository }}/commits/$HEAD_SHA/check-runs --jq '.check_runs[] | select(.name == "Skip Unity Tests") | .conclusion' | head -1) |
| 64 | + echo "Skip Unity Tests status: $SKIP_STATUS" |
| 65 | + if [ "$SKIP_STATUS" == "skipped" ] || [ "$SKIP_STATUS" == "success" ]; then |
| 66 | + UNITY_STATUS="success" |
| 67 | + fi |
| 68 | + fi |
| 69 | +
|
| 70 | + # Determine if we should approve |
| 71 | + if [ "$CLAUDE_STATUS" == "success" ] && [ "$UNITY_STATUS" == "success" ]; then |
| 72 | + echo "All required checks passed!" |
| 73 | + echo "should_approve=true" >> $GITHUB_OUTPUT |
| 74 | + else |
| 75 | + echo "Required checks not yet passed" |
| 76 | + echo "should_approve=false" >> $GITHUB_OUTPUT |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Check if already approved |
| 80 | + id: existing |
| 81 | + if: steps.check.outputs.should_approve == 'true' |
| 82 | + env: |
| 83 | + GH_TOKEN: ${{ github.token }} |
| 84 | + run: | |
| 85 | + PR_NUMBER="${{ steps.pr.outputs.number }}" |
| 86 | +
|
| 87 | + # Check for existing approval from github-actions bot |
| 88 | + EXISTING=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews --jq '[.[] | select(.user.login == "github-actions[bot]" and .state == "APPROVED")] | length') |
| 89 | +
|
| 90 | + if [ "$EXISTING" -gt 0 ]; then |
| 91 | + echo "Already approved by bot" |
| 92 | + echo "already_approved=true" >> $GITHUB_OUTPUT |
| 93 | + else |
| 94 | + echo "Not yet approved by bot" |
| 95 | + echo "already_approved=false" >> $GITHUB_OUTPUT |
| 96 | + fi |
| 97 | +
|
| 98 | + - name: Auto approve PR |
| 99 | + if: steps.check.outputs.should_approve == 'true' && steps.existing.outputs.already_approved == 'false' |
| 100 | + env: |
| 101 | + GH_TOKEN: ${{ github.token }} |
| 102 | + run: | |
| 103 | + PR_NUMBER="${{ steps.pr.outputs.number }}" |
| 104 | +
|
| 105 | + gh pr review $PR_NUMBER --approve --body "Auto-approved: Claude review passed and Unity Tests passed (or were skipped for non-code changes)." |
| 106 | +
|
| 107 | + echo "PR #$PR_NUMBER approved!" |
0 commit comments