|
| 1 | +name: Pull Request Review |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted, edited, dismissed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + Check-CodeRabbit-Approval: |
| 9 | + name: Check CodeRabbit Approval |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: read |
| 13 | + steps: |
| 14 | + - name: Check CodeRabbit approval using GitHub Script |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 18 | + script: | |
| 19 | + // List all reviews for the PR |
| 20 | + const { data: reviews } = await github.rest.pulls.listReviews({ |
| 21 | + owner: context.repo.owner, |
| 22 | + repo: context.repo.repo, |
| 23 | + pull_number: context.payload.pull_request.number |
| 24 | + }); |
| 25 | +
|
| 26 | + // Filter reviews that have a user login containing "coderabbit" (case-insensitive) |
| 27 | + const codeRabbitReviews = reviews.filter(review => |
| 28 | + review.user.login.toLowerCase().includes('coderabbit') || review.user.login.toLowerCase().includes('coderabbitai') |
| 29 | + ); |
| 30 | +
|
| 31 | + // Fail if no CodeRabbit reviews are found |
| 32 | + if (codeRabbitReviews.length === 0) { |
| 33 | + core.setFailed('ERROR: CodeRabbit has not reviewed this PR.'); |
| 34 | + return; |
| 35 | + } |
| 36 | +
|
| 37 | + // Sort reviews by submitted_at date in descending order |
| 38 | + codeRabbitReviews.sort((a, b) => new Date(b.submitted_at) - new Date(a.submitted_at)); |
| 39 | + const latestReview = codeRabbitReviews[0]; |
| 40 | +
|
| 41 | + // Fail if the latest review from CodeRabbit is not "APPROVED" |
| 42 | + if (latestReview.state !== 'APPROVED') { |
| 43 | + core.setFailed('ERROR: CodeRabbit approval is required before merging this PR.'); |
| 44 | + } else { |
| 45 | + console.log('Success: CodeRabbit has approved this PR.'); |
| 46 | + } |
0 commit comments