Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions .github/workflows/coderabbit-approval.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: CodeRabbit Approval Handler

on:
pull_request_review:
types: [submitted]
pull_request_target:
types: [opened, reopened, synchronize, ready_for_review]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

permissions:
contents: read
Expand All @@ -18,20 +18,35 @@ jobs:
id: check-approval
uses: actions/github-script@v7
with:
github-token: ${{ secrets.EXTERNAL_LABELLER_TOKEN || secrets.GITHUB_TOKEN }}
github-token: ${{ github.token }}
script: |
const review = context.payload.review;
const reviewer = review.user.login;
const state = review.state;
const prNumber = context.payload.pull_request.number;

console.log(`Reviewer: ${reviewer}`);
console.log(`Review state: ${state}`);

console.log(`PR number: ${prNumber}`);

// Check if the reviewer is CodeRabbit and the review is approved
const isCodeRabbit = reviewer === 'coderabbitai' || reviewer === 'coderabbitai[bot]';
const isApproved = state === 'approved';

// API-only flow: do not checkout or execute PR code.
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100
});

const isCodeRabbitReview = (review) => {
const login = review.user?.login || '';
return login === 'coderabbitai' || login === 'coderabbitai[bot]';
};

const codeRabbitReviews = reviews.filter(isCodeRabbitReview);
const latestCodeRabbitReview = codeRabbitReviews
.sort((a, b) => new Date(a.submitted_at || 0) - new Date(b.submitted_at || 0))
.at(-1);

const isCodeRabbit = Boolean(latestCodeRabbitReview);
const isApproved = latestCodeRabbitReview?.state === 'APPROVED';

console.log(`CodeRabbit reviews found: ${codeRabbitReviews.length}`);
console.log(`Latest CodeRabbit review state: ${latestCodeRabbitReview?.state || 'none'}`);

core.setOutput('is_coderabbit_approved', isCodeRabbit && isApproved);

Expand All @@ -46,7 +61,7 @@ jobs:
if: steps.check-approval.outputs.is_coderabbit_approved == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.EXTERNAL_LABELLER_TOKEN || secrets.GITHUB_TOKEN }}
github-token: ${{ github.token }}
script: |
const prNumber = context.payload.pull_request.number;
const labelToRemove = 'pending-coderabbit-review';
Expand Down Expand Up @@ -91,7 +106,7 @@ jobs:
if: steps.check-approval.outputs.is_coderabbit_approved == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.EXTERNAL_LABELLER_TOKEN || secrets.GITHUB_TOKEN }}
github-token: ${{ github.token }}
script: |
const prNumber = context.payload.pull_request.number;

Expand All @@ -115,7 +130,7 @@ jobs:
if: steps.check-approval.outputs.is_coderabbit_approved == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.EXTERNAL_LABELLER_TOKEN || secrets.GITHUB_TOKEN }}
github-token: ${{ github.token }}
script: |
const prNumber = context.payload.pull_request.number;
const labelWasPresent = '${{ steps.remove-label.outputs.label_was_present }}' === 'true';
Expand Down
Loading