Skip to content

Commit f88c242

Browse files
committed
fix(ci): restrict CodeRabbit review controls
1 parent 4aab582 commit f88c242

4 files changed

Lines changed: 97 additions & 30 deletions

File tree

‎.coderabbit.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ tone_instructions: >-
55
Prioritize correctness, security, data loss, lifecycle, and regressions; avoid speculative style
66
comments and unrelated refactors.
77
8+
chat:
9+
allow_non_org_members: false
10+
811
knowledge_base:
912
web_search:
1013
enabled: true
@@ -24,6 +27,7 @@ reviews:
2427
enabled: false
2528
drafts: false
2629
auto_incremental_review: true
30+
auto_pause_after_reviewed_commits: 0
2731
labels:
2832
- "coderabbit-review-active"
2933

@@ -125,6 +129,7 @@ reviews:
125129
and deprioritize prose-only nits that do not affect correctness or usability.
126130
127131
pre_merge_checks:
132+
override_requested_reviewers_only: true
128133
custom_checks:
129134
- name: Regression evidence
130135
mode: warning

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ on:
2424
types: [completed]
2525

2626
permissions:
27-
pull-requests: write
27+
# This privileged workflow only reads PR/check metadata and writes issue labels,
28+
# comments, and commit statuses. All unspecified permissions, including contents,
29+
# are none; no fork code or configuration is checked out or executed.
30+
pull-requests: read
2831
issues: write
2932
checks: read
3033
statuses: write
@@ -59,17 +62,17 @@ jobs:
5962
{
6063
name: 'awaiting-coderabbit',
6164
color: '5319e7',
62-
description: 'Waiting for CodeRabbit to approve the latest commit',
65+
description: 'Waiting for automated review of the latest commit',
6366
},
6467
{
6568
name: 'awaiting-ready',
6669
color: '1d76db',
67-
description: 'CodeRabbit approved; waiting for the draft to be marked ready',
70+
description: 'Automated review complete; waiting for the draft to be marked ready',
6871
},
6972
{
7073
name: 'awaiting-maintainer',
7174
color: '0e8a16',
72-
description: 'CodeRabbit approved; waiting for a human maintainer',
75+
description: 'Waiting for fresh human maintainer or CODEOWNER approval',
7376
},
7477
{
7578
name: 'coderabbit-review-active',
@@ -84,6 +87,13 @@ jobs:
8487
const reviewGateName = 'Zoo Code / PR review gate';
8588
const reconciliationCheckName = 'Zoo Code / reconcile PR review state';
8689
90+
if (context.eventName === 'issue_comment' &&
91+
(!context.payload.issue?.pull_request ||
92+
context.payload.comment?.user?.login?.toLowerCase() !== codeRabbitLogin)) {
93+
core.info('Ignoring untrusted issue comment event');
94+
return;
95+
}
96+
8797
// When triggered by a single PR event, only reconcile that PR.
8898
// The hourly schedule and workflow_dispatch reconcile all open PRs.
8999
let prs;
@@ -316,14 +326,14 @@ jobs:
316326
const messages = {
317327
draft: 'Mark the PR ready. Required CI must pass before CodeRabbit starts.',
318328
conflict: 'Resolve the merge conflicts. The review sequence resumes after the branch is mergeable.',
319-
'ci-pending': 'Wait for required CI checks; awaiting-maintainer requires passing CI and CodeRabbit approval.',
320-
'ci-failed': 'Fix the failing required CI checks; awaiting-maintainer requires passing CI and CodeRabbit approval.',
329+
'ci-pending': 'Wait for required CI checks; awaiting-maintainer requires CI and automated review completion.',
330+
'ci-failed': 'Fix the failing required CI checks; awaiting-maintainer requires CI and automated review completion.',
321331
'configuration-error': 'Repository rules must not require this advisory workflow\'s own gate or reconciliation job.',
322-
'coderabbit-changes': 'Address CodeRabbit findings and push fixes.',
323-
coderabbit: 'Required CI passed. Wait for CodeRabbit to approve the latest commit.',
324-
'draft-approved': 'CodeRabbit approved the latest commit. Mark the draft ready.',
325-
'maintainer-changes': 'Address the maintainer feedback, push an update, and request another review.',
326-
maintainer: 'CodeRabbit approved the latest commit. Ready for human maintainer review.',
332+
'coderabbit-changes': 'Address automated review findings and push fixes.',
333+
coderabbit: 'Required CI passed. Waiting for automated review of the latest commit.',
334+
'draft-approved': 'Automated review complete for the latest commit. Mark the draft ready.',
335+
'maintainer-changes': 'Address maintainer or CODEOWNER feedback, then push an update.',
336+
maintainer: 'Awaiting fresh human maintainer or CODEOWNER approval.',
327337
approved: 'The required review sequence passed. Remaining merge requirements apply.',
328338
};
329339
return messages[phase];
@@ -382,14 +392,16 @@ jobs:
382392
? `\n${codeRabbitLabelMarkerPrefix}${pr.head.sha}${activationPending ? ':pending' : ''} -->`
383393
: '';
384394
385-
const codeRabbitHelp = phase === 'coderabbit-changes'
386-
? '\n\nAfter pushing fixes and required CI passes, comment `@coderabbitai review` if automatic review is paused.'
395+
const phaseHelp = phase === 'coderabbit-changes'
396+
? '\n\nAfter fixes are pushed and required CI passes, automated review restarts.'
387397
: phase === 'coderabbit'
388-
? '\n\nIf CodeRabbit is paused, comment `@coderabbitai resume` or `@coderabbitai review`.'
389-
: '';
398+
? '\n\nIf automated review does not start, a maintainer must restart it.'
399+
: phase === 'maintainer' && !automatedAuthor
400+
? '\n\nAutomated review is complete for the latest commit but does not replace human approval.'
401+
: '';
390402
391403
return `${guideMarker}\n### Review status\n\n${authorNote}\n\n` +
392-
`**Current step:** ${phaseMessage(phase)}${codeRabbitHelp}\n\n` +
404+
`**Current step:** ${phaseMessage(phase)}${phaseHelp}\n\n` +
393405
'Review-state labels are managed by this workflow; do not edit them manually.' +
394406
labelMarker;
395407
}
@@ -640,12 +652,12 @@ jobs:
640652
review => review.state === 'CHANGES_REQUESTED'
641653
);
642654
const automatedAuthor = pr.user?.type === 'Bot';
643-
const codeRabbitApproved = freshCodeRabbitReview?.state === 'APPROVED';
655+
const codeRabbitReviewComplete = freshCodeRabbitReview?.state === 'APPROVED';
644656
const codeRabbitChangesRequested = freshCodeRabbitReview?.state === 'CHANGES_REQUESTED';
645657
const maintainerApproval = freshMaintainerReviews
646658
.filter(review => review.state === 'APPROVED')
647659
.sort((a, b) => b.id - a.id)[0];
648-
const maintainerApprovedAfterCodeRabbit = codeRabbitApproved &&
660+
const maintainerApprovedAfterAutomatedReview = codeRabbitReviewComplete &&
649661
maintainerApproval &&
650662
maintainerApproval.id > freshCodeRabbitReview.id;
651663
@@ -667,7 +679,7 @@ jobs:
667679
desiredLabel = null;
668680
phase = 'approved';
669681
}
670-
} else if (!codeRabbitApproved) {
682+
} else if (!codeRabbitReviewComplete) {
671683
if (pr.draft) {
672684
desiredLabel = null;
673685
phase = 'draft';
@@ -680,7 +692,7 @@ jobs:
680692
} else if (pr.draft) {
681693
desiredLabel = 'awaiting-ready';
682694
phase = 'draft-approved';
683-
} else if (!maintainerApprovedAfterCodeRabbit) {
695+
} else if (!maintainerApprovedAfterAutomatedReview) {
684696
desiredLabel = 'awaiting-maintainer';
685697
phase = 'maintainer';
686698
} else {

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ pnpm install
134134

135135
### Review Process
136136

137-
Ready-for-review PRs must pass required CI checks, address actionable review feedback, and receive maintainer approval. Automated review may add a guidance comment and managed state labels; contributors should follow the indicated next step rather than editing those labels directly. New commits may reset the review state for the updated code.
137+
Ready-for-review PRs must pass required CI checks, address actionable review feedback, and receive fresh maintainer or CODEOWNER approval after automated review completes for the latest commit. Automated review may add a guidance comment and managed state labels; contributors should follow the indicated next step rather than editing those labels directly. New commits reset review state for the updated code.
138138

139-
Automated review supports maintainers but does not replace their judgment. Warnings are advisory unless repository policy says otherwise, and native GitHub required-check and review protections remain authoritative for merging.
139+
Automated review supports maintainers but does not replace human approval. Outside contributors cannot direct CodeRabbit through comments; eligible reviews restart automatically after required CI passes, and maintainers handle any exceptional restart. Warnings are advisory unless repository policy says otherwise, and native GitHub required-check, CODEOWNER, and review protections remain authoritative for merging.
140140

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

‎src/services/__tests__/pr-review-state-workflow.test.ts‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const repositoryRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url))
1010
const workflow = parse(
1111
fs.readFileSync(path.join(repositoryRoot, ".github/workflows/label-pr-review-state.yml"), "utf8"),
1212
)
13+
const codeRabbitConfig = parse(fs.readFileSync(path.join(repositoryRoot, ".coderabbit.yaml"), "utf8"))
1314
const workflowScript = workflow.jobs.reconcile.steps[0].with.script as string
1415

1516
const SHA = "a".repeat(40)
@@ -26,6 +27,7 @@ interface HarnessOptions {
2627
mergeableState?: string
2728
fork?: boolean
2829
eventName?: string
30+
issueCommentActor?: string
2931
workflowRunAssociated?: boolean
3032
workflowRunFallback?: "match" | "sha-mismatch" | "base-mismatch" | "none"
3133
workflowRunHeadBranch?: string
@@ -334,7 +336,7 @@ async function runWorkflow(options: HarnessOptions = {}) {
334336
: eventName === "issue_comment"
335337
? {
336338
issue: { number: 1437, pull_request: {} },
337-
comment: { user: { login: "coderabbitai[bot]" } },
339+
comment: { user: { login: options.issueCommentActor ?? "coderabbitai[bot]" } },
338340
}
339341
: eventName === "workflow_dispatch"
340342
? { inputs: { pull_request_number: String(options.workflowDispatchPrNumber ?? 1437) } }
@@ -402,6 +404,24 @@ function latestGateStatus(result: Awaited<ReturnType<typeof runWorkflow>>) {
402404
}
403405

404406
describe("PR review-state workflow", () => {
407+
it("uses supported CodeRabbit access and review controls", () => {
408+
expect(codeRabbitConfig.chat.allow_non_org_members).toBe(false)
409+
expect(codeRabbitConfig.reviews.pre_merge_checks.override_requested_reviewers_only).toBe(true)
410+
expect(codeRabbitConfig.reviews.auto_review.auto_pause_after_reviewed_commits).toBe(0)
411+
expect(codeRabbitConfig.reviews.auto_review.labels).toEqual(["coderabbit-review-active"])
412+
})
413+
414+
it("keeps privileged event handling metadata-only and least-privilege", () => {
415+
expect(workflow.permissions).toEqual({
416+
"pull-requests": "read",
417+
issues: "write",
418+
checks: "read",
419+
statuses: "write",
420+
})
421+
expect(JSON.stringify(workflow.jobs.reconcile.steps)).not.toMatch(/actions\/checkout|\bpnpm\b|\bnpm\b/)
422+
expect(workflowScript).not.toContain("@coderabbitai")
423+
})
424+
405425
it("ignores events for closed pull requests", async () => {
406426
const result = await runWorkflow({ prState: "closed" })
407427

@@ -457,6 +477,22 @@ describe("PR review-state workflow", () => {
457477
expect(result.setFailed).not.toHaveBeenCalled()
458478
})
459479

480+
it("ignores contributor comments before privileged reconciliation", async () => {
481+
const result = await runWorkflow({
482+
eventName: "issue_comment",
483+
issueCommentActor: "outside-contributor",
484+
fork: true,
485+
labels: ["awaiting-maintainer"],
486+
})
487+
488+
expect(result.getPullRequest).not.toHaveBeenCalled()
489+
expect(result.listPullRequests).not.toHaveBeenCalled()
490+
expect(result.addLabels).not.toHaveBeenCalled()
491+
expect(result.removeLabel).not.toHaveBeenCalled()
492+
expect(result.createComment).not.toHaveBeenCalled()
493+
expect(result.createCommitStatus).not.toHaveBeenCalled()
494+
})
495+
460496
it("reconciles fork PRs from pull_request_target", async () => {
461497
const result = await runWorkflow({ eventName: "pull_request_target", fork: true })
462498

@@ -499,7 +535,7 @@ describe("PR review-state workflow", () => {
499535
expect.objectContaining({ labels: ["coderabbit-review-active"] }),
500536
)
501537
expect(latestGateStatus(result)?.state).toBe("success")
502-
expect(latestGateStatus(result)?.description).toContain("Ready for human maintainer")
538+
expect(latestGateStatus(result)?.description).toContain("Awaiting fresh human maintainer")
503539
})
504540

505541
it("completes bot-authored PR review after human maintainer approval", async () => {
@@ -559,6 +595,7 @@ describe("PR review-state workflow", () => {
559595
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["coderabbit-review-active"] }))
560596
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["awaiting-coderabbit"] }))
561597
expect(latestGuide(result)).toContain(`coderabbit-review-label:${SHA}`)
598+
expect(latestGuide(result)).toContain("a maintainer must restart it")
562599
})
563600

564601
it("invalidates the gate before fallible metadata updates", async () => {
@@ -877,11 +914,12 @@ describe("PR review-state workflow", () => {
877914
expect(result.removeLabel).toHaveBeenCalledWith(expect.objectContaining({ name: "coderabbit-review-active" }))
878915
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["awaiting-author"] }))
879916
expect(latestGuide(result)).toContain(
880-
"After pushing fixes and required CI passes, comment `@coderabbitai review` if automatic review is paused.",
917+
"After fixes are pushed and required CI passes, automated review restarts.",
881918
)
919+
expect(latestGuide(result)).not.toContain("@coderabbitai")
882920
})
883921

884-
it("moves approved ready PRs to maintainer review", async () => {
922+
it("treats a fresh bot approval as automated review completion only", async () => {
885923
const result = await runWorkflow({
886924
reviews: [
887925
{
@@ -894,7 +932,8 @@ describe("PR review-state workflow", () => {
894932
})
895933

896934
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["awaiting-maintainer"] }))
897-
expect(latestGuide(result)).toContain("CodeRabbit approved the latest commit")
935+
expect(latestGuide(result)).toContain("Automated review is complete for the latest commit")
936+
expect(latestGuide(result)).toContain("does not replace human approval")
898937
})
899938

900939
it.each([
@@ -1186,7 +1225,7 @@ describe("PR review-state workflow", () => {
11861225
{
11871226
context: "Zoo Code / PR review gate",
11881227
state: "pending",
1189-
description: "Required CI passed. Wait for CodeRabbit to approve the latest commit.",
1228+
description: "Required CI passed. Waiting for automated review of the latest commit.",
11901229
targetUrl: "https://github.com/Zoo-Code-Org/Zoo-Code/pull/1437",
11911230
},
11921231
],
@@ -1478,7 +1517,7 @@ describe("PR review-state workflow", () => {
14781517
})
14791518

14801519
expect(latestGateStatus(result)?.state).toBe("success")
1481-
expect(latestGateStatus(result)?.description).toContain("Ready for human maintainer")
1520+
expect(latestGateStatus(result)?.description).toContain("Awaiting fresh human maintainer")
14821521
})
14831522

14841523
it("fails closed when branch rules are unavailable", async () => {
@@ -1614,8 +1653,9 @@ describe("PR review-state workflow", () => {
16141653
expect(result.createCommitStatus).not.toHaveBeenCalled()
16151654
})
16161655

1617-
it("ignores CodeRabbit reviews from an older head", async () => {
1656+
it("invalidates prior automated and human reviews after a new push", async () => {
16181657
const result = await runWorkflow({
1658+
permissions: { maintainer: "write" },
16191659
reviews: [
16201660
{
16211661
login: "coderabbitai[bot]",
@@ -1624,9 +1664,19 @@ describe("PR review-state workflow", () => {
16241664
submittedAt: REVIEWED_AT,
16251665
commitId: OLD_SHA,
16261666
},
1667+
{
1668+
login: "maintainer",
1669+
type: "User",
1670+
state: "APPROVED",
1671+
submittedAt: REVIEWED_AT + 1_000,
1672+
commitId: OLD_SHA,
1673+
},
16271674
],
16281675
})
16291676

16301677
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["coderabbit-review-active"] }))
1678+
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["awaiting-coderabbit"] }))
1679+
expect(result.addLabels).not.toHaveBeenCalledWith(expect.objectContaining({ labels: ["awaiting-maintainer"] }))
1680+
expect(latestGateStatus(result)?.state).toBe("pending")
16311681
})
16321682
})

0 commit comments

Comments
 (0)