|
| 1 | +name: Ready for Review Label |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted, dismissed] |
| 6 | + workflow_run: |
| 7 | + workflows: ["Continuous integration", "Pre-commit Checks", "Sanitizer"] |
| 8 | + types: [completed] |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.workflow_run.head_branch }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + pull-requests: write |
| 16 | + checks: read |
| 17 | + |
| 18 | +jobs: |
| 19 | + # Handle CodeRabbit review events |
| 20 | + on-review: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: >- |
| 23 | + github.event_name == 'pull_request_review' && |
| 24 | + github.event.review.user.login == 'coderabbitai[bot]' |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + sparse-checkout: .github/scripts |
| 29 | + sparse-checkout-cone-mode: true |
| 30 | + - name: Evaluate label |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 34 | + REVIEW_STATE: ${{ github.event.review.state }} |
| 35 | + EVENT_ACTION: ${{ github.event.action }} |
| 36 | + REPO: ${{ github.repository }} |
| 37 | + run: | |
| 38 | + # On dismissal or changes requested, always remove label |
| 39 | + if [ "$EVENT_ACTION" = "dismissed" ] || [ "$REVIEW_STATE" = "changes_requested" ]; then |
| 40 | + echo "CodeRabbit review dismissed or changes requested, removing label." |
| 41 | + gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "ready-for-review" || true |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + # On approval, check if all CI checks have also passed |
| 46 | + if [ "$EVENT_ACTION" = "submitted" ] && [ "$REVIEW_STATE" = "approved" ]; then |
| 47 | + echo "CodeRabbit approved. Checking CI status..." |
| 48 | +
|
| 49 | + CI_STATUS=$(.github/scripts/check_ci_status.sh "$PR_NUMBER" "$REPO") |
| 50 | +
|
| 51 | + echo "CI status: $CI_STATUS" |
| 52 | + if [ "$CI_STATUS" = "all_passed" ]; then |
| 53 | + echo "All conditions met. Adding ready-for-review label." |
| 54 | + gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "ready-for-review" |
| 55 | + else |
| 56 | + echo "CI checks not all passing ($CI_STATUS). Label not added." |
| 57 | + fi |
| 58 | + fi |
| 59 | +
|
| 60 | + # Handle CI workflow completions |
| 61 | + on-ci-complete: |
| 62 | + runs-on: ubuntu-latest |
| 63 | + if: github.event_name == 'workflow_run' |
| 64 | + steps: |
| 65 | + - uses: actions/checkout@v4 |
| 66 | + with: |
| 67 | + sparse-checkout: | |
| 68 | + .github/scripts |
| 69 | + .github/workflows |
| 70 | + sparse-checkout-cone-mode: true |
| 71 | + - name: Validate workflow trigger coverage |
| 72 | + run: | |
| 73 | + SELF=".github/workflows/ready-for-review.yml" |
| 74 | +
|
| 75 | + # Extract workflow names from the workflow_run trigger list in this file |
| 76 | + MONITORED=$(grep -A1 'workflows:' "$SELF" \ |
| 77 | + | grep '\[' \ |
| 78 | + | tr ',' '\n' \ |
| 79 | + | sed 's/.*"\([^"]*\)".*/\1/') |
| 80 | +
|
| 81 | + MISSING="" |
| 82 | + for f in .github/workflows/*.yml; do |
| 83 | + # Skip self |
| 84 | + [ "$f" = "$SELF" ] && continue |
| 85 | +
|
| 86 | + # Check if this workflow triggers on pull_request (not |
| 87 | + # pull_request_target which has different semantics, and not |
| 88 | + # workflow_call which is for reusable workflows). |
| 89 | + if ! grep -qE '^[[:space:]]+pull_request:' "$f"; then |
| 90 | + continue |
| 91 | + fi |
| 92 | +
|
| 93 | + # Extract the workflow name |
| 94 | + WF_NAME=$(grep -m1 '^name:' "$f" | sed "s/^name:\s*//; s/[\"']//g") |
| 95 | +
|
| 96 | + # Check if it's in the monitored list |
| 97 | + if ! echo "$MONITORED" | grep -qF "$WF_NAME"; then |
| 98 | + MISSING="$MISSING\n - $WF_NAME ($f)" |
| 99 | + fi |
| 100 | + done |
| 101 | +
|
| 102 | + if [ -n "$MISSING" ]; then |
| 103 | + echo "" |
| 104 | + echo "ERROR: The following workflows trigger on pull_request but are" |
| 105 | + echo "not listed in ready-for-review.yml's workflow_run trigger." |
| 106 | + echo "Please add them to keep the ready-for-review label gate working correctly." |
| 107 | + echo -e "$MISSING" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +
|
| 111 | + echo "All pull_request workflows are covered by workflow_run triggers." |
| 112 | + - name: Evaluate label for associated PRs |
| 113 | + env: |
| 114 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 115 | + REPO: ${{ github.repository }} |
| 116 | + PULL_REQUESTS_JSON: ${{ toJson(github.event.workflow_run.pull_requests) }} |
| 117 | + run: | |
| 118 | + # Get PR numbers associated with this workflow run |
| 119 | + PR_NUMBERS=$(echo "$PULL_REQUESTS_JSON" | jq -r '.[].number') |
| 120 | +
|
| 121 | + if [ -z "$PR_NUMBERS" ]; then |
| 122 | + echo "No associated pull requests found, skipping." |
| 123 | + exit 0 |
| 124 | + fi |
| 125 | +
|
| 126 | + for PR_NUMBER in $PR_NUMBERS; do |
| 127 | + echo "=== Checking PR #$PR_NUMBER ===" |
| 128 | +
|
| 129 | + # Check if CodeRabbit has approved |
| 130 | + CODERABBIT_STATE=$(gh api --paginate "repos/$REPO/pulls/$PR_NUMBER/reviews" | jq -rs ' |
| 131 | + add |
| 132 | + | map(select(.user.login == "coderabbitai[bot]")) |
| 133 | + | sort_by(.submitted_at) |
| 134 | + | last |
| 135 | + | .state // "NONE" |
| 136 | + ') |
| 137 | + echo "CodeRabbit review state: $CODERABBIT_STATE" |
| 138 | +
|
| 139 | + if [ "$CODERABBIT_STATE" != "APPROVED" ]; then |
| 140 | + echo "CodeRabbit has not approved. Skipping." |
| 141 | + continue |
| 142 | + fi |
| 143 | +
|
| 144 | + # Check if all CI checks have passed |
| 145 | + CI_STATUS=$(.github/scripts/check_ci_status.sh "$PR_NUMBER" "$REPO") |
| 146 | +
|
| 147 | + echo "CI status: $CI_STATUS" |
| 148 | + if [ "$CI_STATUS" = "all_passed" ]; then |
| 149 | + echo "Both conditions met. Adding ready-for-review label." |
| 150 | + gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "ready-for-review" |
| 151 | + else |
| 152 | + echo "CI checks not all passing ($CI_STATUS). Removing label if present." |
| 153 | + gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "ready-for-review" || true |
| 154 | + fi |
| 155 | + done |
0 commit comments