Skip to content

Commit 9a2c3fb

Browse files
zoomote[bot]taltasedelauna
authored
[Feat] Add community-approved label for community code approvals (Zoo-Code-Org#1873)
* feat: add community-approved advisory label for community code approvals (Zoo-Code-Org#1872) * test(workflow): cover community-approved label removal paths --------- Co-authored-by: @taltas <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent ceceb08 commit 9a2c3fb

3 files changed

Lines changed: 719 additions & 12 deletions

File tree

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

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
# This workflow only reads PR metadata and never checks out or executes PR code.
1515
# pull_request_target gives fork PRs a token that can update labels and comments.
1616
pull_request_target:
17-
types: [opened, reopened, ready_for_review, synchronize, review_requested, review_request_removed, labeled, unlabeled]
17+
types: [opened, reopened, ready_for_review, converted_to_draft, synchronize, review_requested, review_request_removed, labeled, unlabeled]
1818
pull_request_review:
1919
types: [submitted, dismissed]
2020
# Fork review events have a read-only token. CodeRabbit's status-comment update
@@ -81,13 +81,21 @@ jobs:
8181
color: '5319e7',
8282
description: 'Required CI passed; CodeRabbit review is active',
8383
},
84+
{
85+
name: 'community-approved',
86+
color: 'c2e0c6',
87+
description: 'Fresh community approval on the current head; maintainer review still required',
88+
},
8489
];
8590
const guideMarker = '<!-- zoo-code-pr-review-process -->';
8691
const codeRabbitLabelMarkerPrefix = '<!-- coderabbit-review-label:';
8792
const codeRabbitLogin = 'coderabbitai[bot]';
8893
const codeRabbitLogins = new Set([codeRabbitLogin, 'coderabbitai']);
8994
const codeRabbitActiveLabel = 'coderabbit-review-active';
9095
const codeRabbitEligibleBotLogins = new Set(['zoomote[bot]']);
96+
// Advisory priority signal only — kept outside stateLabels so it never
97+
// replaces or blocks the review-state queue labels.
98+
const communityApprovedLabel = 'community-approved';
9199
const reviewGateName = 'Zoo Code / PR review gate';
92100
const reconciliationCheckName = 'Zoo Code / reconcile PR review state';
93101
@@ -310,6 +318,48 @@ jobs:
310318
}
311319
}
312320
321+
// Reconciles the advisory community-approved label. The label means
322+
// "fresh community code approval for the current head; maintainer
323+
// review still required" — it is a priority signal only and never
324+
// grants merge authority. Every reconciliation branch routes through
325+
// this one helper with a single decision:
326+
// 'add' — stable head, maintainer queue, qualifying fresh approval
327+
// 'remove' — known non-maintainer phase, known head change, or
328+
// invalidated approval (known invalidation beats uncertainty)
329+
// 'preserve' — unknown mergeability, or a permission lookup that
330+
// fails only in the community check, with no known
331+
// invalidation. A lookup failure that also blocks
332+
// maintainer classification fails the PR and removes
333+
// the label through the outer catch instead.
334+
// A failed community label update is advisory: it must not clear the
335+
// queue label or fail the review gate, so errors are logged, not thrown.
336+
async function reconcileCommunityApproval(pr, decision) {
337+
if (isReadOnlyRun && isForkPR(pr)) return;
338+
if (decision === 'preserve') return;
339+
const hasLabel = pr.labels.some(label => label.name === communityApprovedLabel);
340+
try {
341+
if (decision === 'add' && !hasLabel) {
342+
await github.rest.issues.addLabels({
343+
owner, repo, issue_number: pr.number, labels: [communityApprovedLabel],
344+
});
345+
pr.labels.push({ name: communityApprovedLabel });
346+
} else if (decision === 'remove' && hasLabel) {
347+
await github.rest.issues.removeLabel({
348+
owner, repo, issue_number: pr.number, name: communityApprovedLabel,
349+
});
350+
pr.labels = pr.labels.filter(label => label.name !== communityApprovedLabel);
351+
}
352+
} catch (error) {
353+
if (decision === 'remove' && error.status === 404) {
354+
pr.labels = pr.labels.filter(label => label.name !== communityApprovedLabel);
355+
return;
356+
}
357+
core.warning(
358+
`PR #${pr.number}: could not ${decision} ${communityApprovedLabel}: ${error.message}`
359+
);
360+
}
361+
}
362+
313363
function codeRabbitLabelHead(comment) {
314364
const markerPattern = new RegExp(`${codeRabbitLabelMarkerPrefix}([a-f0-9]{40}) -->`);
315365
const match = comment?.body?.match(markerPattern);
@@ -419,7 +469,10 @@ jobs:
419469
420470
return `${guideMarker}\n### Review status\n\n${authorNote}\n\n` +
421471
`**Current step:** ${phaseMessage(phase)}${phaseHelp}\n\n` +
422-
'Review-state labels are managed by this workflow; do not edit them manually.' +
472+
'Review-state labels are managed by this workflow; do not edit them manually. ' +
473+
'`community-approved` is managed the same way — do not add or remove it manually. ' +
474+
'It signals a fresh community code approval for the current head as an advisory ' +
475+
'priority only; maintainer review is still required.' +
423476
labelMarker;
424477
}
425478
@@ -497,6 +550,7 @@ jobs:
497550
const existingGuide = await findReviewGuide(pr);
498551
await setCodeRabbitReviewActive(pr, false);
499552
await reconcileLabels(pr, null);
553+
await reconcileCommunityApproval(pr, 'remove');
500554
await updateReviewGuide(pr, 'configuration-error', existingGuide);
501555
continue;
502556
}
@@ -518,6 +572,7 @@ jobs:
518572
await updateReviewGate(pr, 'conflict', false);
519573
await setCodeRabbitReviewActive(pr, false);
520574
await reconcileLabels(pr, 'has-conflicts');
575+
await reconcileCommunityApproval(pr, 'remove');
521576
await updateReviewGuide(pr, 'conflict', existingGuide);
522577
continue;
523578
}
@@ -623,6 +678,7 @@ jobs:
623678
await updateReviewGate(pr, ciPending ? 'ci-pending' : 'ci-failed', false);
624679
await setCodeRabbitReviewActive(pr, false);
625680
await reconcileLabels(pr, null);
681+
await reconcileCommunityApproval(pr, 'remove');
626682
await updateReviewGuide(
627683
pr,
628684
ciPending ? 'ci-pending' : 'ci-failed',
@@ -642,7 +698,10 @@ jobs:
642698
// block the PR or indicate the author needs to act.
643699
const latest = new Map();
644700
for (const r of reviews) {
645-
const reviewer = r.user.login.toLowerCase();
701+
// Ghost accounts have no usable identity; their reviews carry
702+
// no actionable state for either review loop.
703+
const reviewer = r.user?.login?.toLowerCase();
704+
if (!reviewer) continue;
646705
const reviewerKey = codeRabbitLogins.has(reviewer) ? codeRabbitLogin : reviewer;
647706
if (r.state === 'DISMISSED') {
648707
latest.delete(reviewerKey);
@@ -742,6 +801,43 @@ jobs:
742801
freshMaintainerReviews.push(review);
743802
}
744803
}
804+
// Community approval eligibility reuses the decisive per-reviewer
805+
// reduction above: the reviewer's latest decisive review (selected
806+
// by review id, not array order) must be an approval on the current
807+
// head. Ghost accounts, bots, CodeRabbit, and the PR author are
808+
// already excluded there; comments are neutral. Permission results
809+
// are shared with maintainer classification through permissionFor:
810+
// a 404 qualifies an otherwise valid reviewer, while a lookup
811+
// failure is unknown permission and never counts as community
812+
// eligibility. One eligible approval is enough; bot-authored PRs
813+
// follow the same rule. A known invalidation — an approval that
814+
// was dismissed, replaced, or left behind by a head change —
815+
// takes priority over any lookup uncertainty.
816+
const approvedByReviewer = new Set();
817+
for (const r of reviews) {
818+
const reviewer = r.user?.login?.toLowerCase();
819+
if (reviewer && r.state === 'APPROVED') approvedByReviewer.add(reviewer);
820+
}
821+
let communityApprovalFresh = false;
822+
let communityPermissionUncertain = false;
823+
let communityApprovalInvalidated = false;
824+
for (const [reviewer, review] of latestHumanReview) {
825+
if (review.state === 'APPROVED' && review.commit_id === pr.head.sha) {
826+
let permission;
827+
try {
828+
permission = await permissionFor(review.user.login);
829+
} catch (error) {
830+
communityPermissionUncertain = true;
831+
continue;
832+
}
833+
if (!['admin', 'maintain', 'write'].includes(permission)) {
834+
communityApprovalFresh = true;
835+
break;
836+
}
837+
} else if (approvedByReviewer.has(reviewer)) {
838+
communityApprovalInvalidated = true;
839+
}
840+
}
745841
const automatedAuthor = pr.user?.type === 'Bot';
746842
const codeRabbitEligibleAuthor = !automatedAuthor ||
747843
codeRabbitEligibleBotLogins.has(pr.user?.login.toLowerCase());
@@ -793,15 +889,24 @@ jobs:
793889
phase = 'approved';
794890
}
795891
892+
// Advisory community label decision for the branches below.
893+
// Non-maintainer phases always remove it; the maintainer queue
894+
// refines the decision after the head/mergeability refetch.
895+
let communityDecision = 'remove';
796896
if (desiredLabel === 'awaiting-maintainer') {
797897
const { data: latestPrDetail } = await github.rest.pulls.get({
798898
owner, repo, pull_number: pr.number,
799899
});
900+
// A head change since the event fetch invalidates every
901+
// approval, even if another check is uncertain.
902+
const headChanged = Boolean(latestPrDetail.head?.sha) &&
903+
latestPrDetail.head.sha !== pr.head.sha;
800904
if (latestPrDetail.mergeable === false && latestPrDetail.mergeable_state === 'dirty') {
801905
core.info(`PR #${pr.number}: has merge conflicts — labeling has-conflicts`);
802906
await updateReviewGate(pr, 'conflict', false);
803907
await setCodeRabbitReviewActive(pr, false);
804908
await reconcileLabels(pr, 'has-conflicts');
909+
await reconcileCommunityApproval(pr, 'remove');
805910
await updateReviewGuide(pr, 'conflict', existingGuide);
806911
continue;
807912
}
@@ -815,8 +920,23 @@ jobs:
815920
error.preserveStateLabels = true;
816921
throw error;
817922
}
923+
// Unknown mergeability alone, with an unchanged head and no
924+
// known invalidation, preserves an existing label and never adds it.
925+
await reconcileCommunityApproval(
926+
pr,
927+
headChanged || communityApprovalInvalidated ? 'remove' : 'preserve'
928+
);
818929
continue;
819930
}
931+
communityDecision = headChanged
932+
? 'remove'
933+
: communityApprovalFresh
934+
? 'add'
935+
: communityApprovalInvalidated
936+
? 'remove'
937+
: communityPermissionUncertain
938+
? 'preserve'
939+
: 'remove';
820940
}
821941
822942
core.info(
@@ -842,6 +962,7 @@ jobs:
842962
await updateReviewGuide(pr, phase, existingGuide);
843963
}
844964
await reconcileLabels(pr, desiredLabel);
965+
await reconcileCommunityApproval(pr, communityDecision);
845966
if (readyForMaintainer) {
846967
await updateReviewGate(pr, phase, true);
847968
}
@@ -864,6 +985,9 @@ jobs:
864985
} catch (cleanupError) {
865986
metadataErrors.push(cleanupError);
866987
}
988+
// A global failure that clears the maintainer queue clears the
989+
// advisory community signal too.
990+
await reconcileCommunityApproval(pr, 'remove');
867991
}
868992
const detail = error.status
869993
? `${error.message} (HTTP ${error.status}${error.response?.data?.message ? `: ${error.response.data.message}` : ''})`

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pnpm install
134134

135135
### Review Process
136136

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.
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. A `community-approved` label marks a fresh community approval on the latest commit while the PR waits for maintainer review. It is an advisory priority signal for maintainers; it does not replace maintainer review or any required check, and it is managed by the review workflow.
138138

139139
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

0 commit comments

Comments
 (0)