Skip to content

Commit 02a0ff7

Browse files
committed
fix: make CodeRabbit optional for bot PRs
1 parent 2d866c6 commit 02a0ff7

3 files changed

Lines changed: 133 additions & 98 deletions

File tree

‎.github/workflows/label-pr-review-state.yml‎

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ jobs:
7070
},
7171
];
7272
const guideMarker = '<!-- zoo-code-pr-review-process -->';
73-
const codeRabbitSummaryMarker = '<!-- This is an auto-generated comment: summarize by coderabbit.ai -->';
7473
const codeRabbitLabelMarkerPrefix = '<!-- coderabbit-review-label:';
7574
const codeRabbitLogin = 'coderabbitai[bot]';
7675
const codeRabbitActiveLabel = 'coderabbit-review-active';
@@ -171,28 +170,6 @@ jobs:
171170
);
172171
}
173172
174-
async function findCodeRabbitSummary(pr) {
175-
const comments = await github.paginate(github.rest.issues.listComments, {
176-
owner, repo, issue_number: pr.number, per_page: 100,
177-
});
178-
return comments.find(comment =>
179-
comment.user?.login?.toLowerCase() === codeRabbitLogin &&
180-
comment.body?.includes(codeRabbitSummaryMarker)
181-
);
182-
}
183-
184-
function codeRabbitPreMergeState(comment, headSha) {
185-
const body = comment?.body ?? '';
186-
const reviewedHead = body.match(/Merge Risk:[\s\S]{0,200}?up to `([a-f0-9]{5,40})`/i)?.[1];
187-
if (!reviewedHead || !headSha.startsWith(reviewedHead) ||
188-
!body.includes('<!-- pre_merge_checks_walkthrough_start -->')) {
189-
return 'pending';
190-
}
191-
192-
const errorCount = Number(body.match(/Failed checks \((\d+) errors?/i)?.[1] ?? 0);
193-
return errorCount > 0 ? 'failed' : 'passed';
194-
}
195-
196173
async function setCodeRabbitReviewActive(pr, enabled, recycle = false) {
197174
if (isReadOnlyRun && isForkPR(pr)) return;
198175
@@ -248,12 +225,12 @@ jobs:
248225
conflict: 'Resolve the merge conflicts. The review sequence resumes after the branch is mergeable.',
249226
'ci-pending': 'Wait for the required CI checks to finish.',
250227
'ci-failed': 'Fix the failing required CI checks and push an update.',
251-
'coderabbit-changes': 'Address CodeRabbit findings and blocking pre-merge checks, then push an update.',
252-
coderabbit: 'Wait for CodeRabbit approval and blocking pre-merge checks to pass.',
253-
'draft-approved': 'CodeRabbit approval and blocking pre-merge checks passed. Mark the draft ready.',
228+
'coderabbit-changes': 'Address CodeRabbit findings and push an update. Review restarts after CI passes.',
229+
coderabbit: 'Required CI passed. Wait for CodeRabbit to approve the latest commit.',
230+
'draft-approved': 'CodeRabbit approved the latest commit. Mark the draft ready.',
254231
'maintainer-changes': 'Address the maintainer feedback, push an update, and request another review.',
255-
maintainer: 'CodeRabbit approval and blocking pre-merge checks passed. A maintainer must now approve.',
256-
approved: 'CodeRabbit pre-merge checks and both required approvals passed. Remaining merge requirements apply.',
232+
maintainer: 'A human maintainer must now review and approve the latest commit.',
233+
approved: 'The required review sequence passed. Remaining merge requirements apply.',
257234
};
258235
return messages[phase];
259236
}
@@ -264,16 +241,19 @@ jobs:
264241
const state = passed ? 'success' : 'pending';
265242
const description = phaseMessage(phase);
266243
let latestGateStatus = null;
244+
let lookupSucceeded = false;
267245
try {
268246
const { data: combinedStatus } = await github.rest.repos.getCombinedStatusForRef({
269247
owner, repo, ref: pr.head.sha,
270248
});
249+
lookupSucceeded = true;
271250
latestGateStatus = combinedStatus.statuses
272251
.filter(status => status.context === reviewGateName)
273252
.sort((a, b) => b.id - a.id)[0];
274253
} catch (error) {
275254
core.warning(`PR #${pr.number}: could not inspect ${reviewGateName}: ${error.message}`);
276255
}
256+
if (!lookupSucceeded && !required) return;
277257
if (latestGateStatus?.state === state &&
278258
latestGateStatus.description === description &&
279259
latestGateStatus.target_url === pr.html_url) {
@@ -309,7 +289,7 @@ jobs:
309289
return `${guideMarker}\n### Review process\n\n${authorNote}\n\n` +
310290
'1. Required CI checks pass.\n' +
311291
'2. The workflow starts CodeRabbit automatically.\n' +
312-
'3. CodeRabbit approves the latest commit with no blocking pre-merge check errors.\n' +
292+
'3. For eligible human-authored PRs, CodeRabbit reviews and approves the latest commit.\n' +
313293
'4. A human maintainer reviews and approves after CodeRabbit.\n\n' +
314294
`**Current step:** ${phaseMessage(phase)}${labelMarker}`;
315295
}
@@ -507,8 +487,6 @@ jobs:
507487
}
508488
509489
// CI is passing. Now determine review state.
510-
const codeRabbitSummary = await findCodeRabbitSummary(pr);
511-
const codeRabbitPreMerge = codeRabbitPreMergeState(codeRabbitSummary, pr.head.sha);
512490
const reviews = await github.paginate(github.rest.pulls.listReviews, {
513491
owner, repo, pull_number: pr.number, per_page: 100,
514492
});
@@ -545,10 +523,9 @@ jobs:
545523
const maintainerChangeRequest = freshMaintainerReviews.find(
546524
review => review.state === 'CHANGES_REQUESTED'
547525
);
548-
const codeRabbitApproved = freshCodeRabbitReview?.state === 'APPROVED' &&
549-
codeRabbitPreMerge === 'passed';
550-
const codeRabbitChangesRequested = freshCodeRabbitReview?.state === 'CHANGES_REQUESTED' ||
551-
codeRabbitPreMerge === 'failed';
526+
const automatedAuthor = pr.user?.type === 'Bot';
527+
const codeRabbitApproved = freshCodeRabbitReview?.state === 'APPROVED';
528+
const codeRabbitChangesRequested = freshCodeRabbitReview?.state === 'CHANGES_REQUESTED';
552529
const maintainerApproval = freshMaintainerReviews
553530
.filter(review => review.state === 'APPROVED')
554531
.sort((a, b) => b.id - a.id)[0];
@@ -560,9 +537,20 @@ jobs:
560537
let phase;
561538
let activateCodeRabbit = false;
562539
let recycleCodeRabbitLabel = false;
563-
if (codeRabbitChangesRequested) {
540+
if (codeRabbitChangesRequested || maintainerChangeRequest) {
564541
desiredLabel = 'awaiting-author';
565-
phase = 'coderabbit-changes';
542+
phase = codeRabbitChangesRequested ? 'coderabbit-changes' : 'maintainer-changes';
543+
} else if (automatedAuthor) {
544+
if (pr.draft) {
545+
desiredLabel = null;
546+
phase = 'draft';
547+
} else if (!maintainerApproval) {
548+
desiredLabel = 'awaiting-maintainer';
549+
phase = 'maintainer';
550+
} else {
551+
desiredLabel = null;
552+
phase = 'approved';
553+
}
566554
} else if (!codeRabbitApproved) {
567555
if (pr.draft) {
568556
desiredLabel = null;
@@ -573,9 +561,6 @@ jobs:
573561
desiredLabel = 'awaiting-coderabbit';
574562
phase = 'coderabbit';
575563
}
576-
} else if (maintainerChangeRequest) {
577-
desiredLabel = 'awaiting-author';
578-
phase = 'maintainer-changes';
579564
} else if (pr.draft) {
580565
desiredLabel = 'awaiting-ready';
581566
phase = 'draft-approved';
@@ -589,7 +574,7 @@ jobs:
589574
590575
core.info(
591576
`PR #${pr.number}: CI passing, reviews=${latest.size}, ` +
592-
`coderabbit=${freshCodeRabbitReview?.state ?? 'pending'}/${codeRabbitPreMerge}, ` +
577+
`coderabbit=${freshCodeRabbitReview?.state ?? (automatedAuthor ? 'optional' : 'pending')}, ` +
593578
`maintainer=${maintainerApproval?.state ?? 'pending'} → ${desiredLabel ?? '(none)'}`
594579
);
595580

‎CONTRIBUTING.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,21 @@ pnpm install
137137
Ready-for-review PRs move through these gates in order:
138138

139139
1. Required CI checks must pass.
140-
2. The workflow automatically applies the managed `coderabbit-review-active` label to start CodeRabbit review for the latest commit. Contributors and maintainers should not manage this label manually.
141-
3. Address CodeRabbit findings and every error in its persistent **Pre-merge checks** summary. Warnings are advisory unless repository policy says otherwise. Review restarts automatically after required CI passes again.
142-
4. After CodeRabbit approves the current commit and its blocking pre-merge checks pass, a non-author maintainer account with write access performs the final review and approval.
140+
2. For eligible human-authored PRs, the workflow automatically applies the managed `coderabbit-review-active` label to start CodeRabbit review for the latest commit. Contributors and maintainers should not manage this label manually.
141+
3. Address CodeRabbit findings and every error in its persistent **Pre-merge checks** summary. Warnings are advisory unless repository policy says otherwise. CodeRabbit's error-mode checks use its native changes-requested review to block merging.
142+
4. After CodeRabbit approves an eligible human-authored PR, a non-author maintainer account with write access performs the final review and approval.
143143

144144
An automated comment on each PR shows the current gate and next action. The `awaiting-coderabbit`, `awaiting-ready`, `awaiting-maintainer`, `awaiting-author`, and `has-conflicts` labels make the same state visible in the PR list. A new commit invalidates prior approvals; required CI and CodeRabbit rerun for that commit before maintainer review.
145145

146-
CodeRabbit's green commit status only means its review completed; it does not prove that custom pre-merge checks passed. Contributors, maintainers, and automated PR fixers must inspect CodeRabbit's persistent summary comment, resolve all **Error** entries under **Pre-merge checks**, and report that state explicitly in PR update comments.
146+
CodeRabbit's green commit status only means its review completed; it does not prove that custom pre-merge checks passed. Contributors, maintainers, and automated PR fixers must inspect CodeRabbit's persistent summary comment, resolve all **Error** entries under **Pre-merge checks**, and report that state explicitly in PR update comments. Use `@coderabbitai run pre-merge checks` to rerun those checks and `@coderabbitai approve` to resolve CodeRabbit threads and request its approval after fixes.
147147

148148
The `PR review gate` commit status passes only after the sequence completes. It is advisory by default; repository administrators can make it required on `main` if enforcement is desired. The labels and managed comment remain the maintainer-facing review queue either way.
149149

150150
Optional checks such as Codecov do not delay CodeRabbit unless repository rules make them required. Draft PRs are not reviewed automatically, but authors can still request an early review with `@coderabbitai review`.
151151

152152
The workflow reads required checks from the `main` branch ruleset. If those rules cannot be read, the gate fails closed and waits for the hourly reconciliation or a manual workflow run after the ruleset is available again.
153153

154-
PRs opened by bots or other automated accounts follow the same CI and CodeRabbit gates. Final approval requires a non-author, non-bot account with write access, and maintainers remain responsible for verifying the change's intent, provenance, and validation before merging.
154+
PRs opened by bots skip automatic CodeRabbit activation because author exclusions take precedence over label opt-in. They move from required CI directly to human maintainer review. A human may optionally request an incremental `@coderabbitai review` or a fresh `@coderabbitai full review`; once CodeRabbit requests changes, that native review must be resolved or dismissed before merge. Final approval still requires a non-author, non-bot account with write access, and maintainers remain responsible for verifying the change's intent, provenance, and validation.
155155

156156
- **Daily Triage:** Quick checks by maintainers.
157157
- **Weekly In-depth Review:** Comprehensive assessment.

0 commit comments

Comments
 (0)