|
| 1 | +name: CodeRabbit Review Enforcement (Reusable) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + GITHUB_TOKEN: |
| 7 | + required: true |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-coderabbit-approval: |
| 11 | + name: Check CodeRabbit Approval |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + permissions: |
| 15 | + pull-requests: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Wait for review status propagation |
| 19 | + run: sleep 30s |
| 20 | + shell: bash |
| 21 | + |
| 22 | + - name: Check CodeRabbit approval |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + script: | |
| 27 | + const { owner, repo } = context.repo; |
| 28 | + const pull_number = context.payload.pull_request.number; |
| 29 | +
|
| 30 | + // Fetch all PR reviews |
| 31 | + const { data: reviews } = await github.rest.pulls.listReviews({ |
| 32 | + owner, |
| 33 | + repo, |
| 34 | + pull_number |
| 35 | + }); |
| 36 | +
|
| 37 | + // Identify CodeRabbit reviews (case-insensitive) and ignore COMMENTED |
| 38 | + const codeRabbitReviews = reviews.filter(review => |
| 39 | + review.user?.login && |
| 40 | + ( |
| 41 | + review.user.login.toLowerCase().includes('coderabbit') || |
| 42 | + review.user.login.toLowerCase().includes('coderabbitai') |
| 43 | + ) && |
| 44 | + review.state !== 'COMMENTED' |
| 45 | + ); |
| 46 | +
|
| 47 | + if (codeRabbitReviews.length === 0) { |
| 48 | + core.setFailed('ERROR: CodeRabbit has not reviewed this PR.'); |
| 49 | + return; |
| 50 | + } |
| 51 | +
|
| 52 | + // Sort newest → oldest |
| 53 | + codeRabbitReviews.sort( |
| 54 | + (a, b) => new Date(b.submitted_at) - new Date(a.submitted_at) |
| 55 | + ); |
| 56 | +
|
| 57 | + const latestReview = codeRabbitReviews[0]; |
| 58 | +
|
| 59 | + if (latestReview.state !== 'APPROVED') { |
| 60 | + core.setFailed( |
| 61 | + 'ERROR: CodeRabbit approval is required before merging this PR.' |
| 62 | + ); |
| 63 | + } else { |
| 64 | + console.log('Success: CodeRabbit has approved this PR.'); |
| 65 | + } |
0 commit comments