Skip to content

Commit a4ad381

Browse files
authored
ci: Preserve shared Codex findings
1 parent 547a1f9 commit a4ad381

1 file changed

Lines changed: 64 additions & 31 deletions

File tree

‎.github/workflows/codex-review-gate.yml‎

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -533,20 +533,24 @@ jobs:
533533
const pullRequests = await github.paginate(github.rest.pulls.list, {
534534
owner,
535535
repo,
536-
state: 'open',
536+
state: 'all',
537537
per_page: 100,
538538
});
539539
// The required gate protects the default branch. Two reviewable PRs
540540
// with the same head and the same default-branch base have the same
541541
// diff, so sharing the commit-scoped gate is safe. PRs targeting a
542542
// different base never get a successful gate from this workflow.
543543
const reviewablePulls = pullRequests.filter(
544-
(pull) => !pull.draft && pull.base.ref === defaultBranch,
544+
(pull) =>
545+
pull.state === 'open' &&
546+
!pull.draft &&
547+
pull.base.ref === defaultBranch,
545548
);
546549
547550
const pending = [];
548551
const terminalEvents = [];
549552
const quotaEvents = [];
553+
const inspectedClosedSiblings = new Set();
550554
551555
for (const pull of reviewablePulls) {
552556
let run = await latestGateRun(pull.head.sha);
@@ -602,6 +606,36 @@ jobs:
602606
603607
const outcome = await inspectCodexOutcome(pull, run, availableSince);
604608
if (outcome.quota) quotaEvents.push(outcome.quota);
609+
610+
// A commit-scoped check can be shared by multiple PRs with the
611+
// same head and base. A late finding does not become safe merely
612+
// because the PR that received it was closed before this run.
613+
for (const sibling of pullRequests) {
614+
if (sibling.state !== 'closed') continue;
615+
if (sibling.head.sha !== pull.head.sha) continue;
616+
if (sibling.base.ref !== defaultBranch) continue;
617+
if (sibling.base.sha !== pull.base.sha) continue;
618+
const siblingKey = `${run.id}:${sibling.number}`;
619+
if (inspectedClosedSiblings.has(siblingKey)) continue;
620+
inspectedClosedSiblings.add(siblingKey);
621+
622+
const siblingOutcome = await inspectCodexOutcome(
623+
sibling,
624+
run,
625+
availableSince,
626+
);
627+
if (siblingOutcome.quota) quotaEvents.push(siblingOutcome.quota);
628+
if (siblingOutcome.terminal?.kind === 'findings') {
629+
terminalEvents.push({
630+
...siblingOutcome.terminal,
631+
base_sha: pull.base.sha,
632+
check_run_id: run.id,
633+
head_sha: pull.head.sha,
634+
pr: sibling.number,
635+
});
636+
}
637+
}
638+
605639
if (outcome.terminal) {
606640
if (completedSuccess && outcome.terminal.kind !== 'findings') {
607641
continue;
@@ -651,35 +685,27 @@ jobs:
651685
652686
const verdictEvents = [...verdictsByCheckRun.values()];
653687
verdictEvents.sort((a, b) => a.at - b.at);
688+
const currentOpenPulls = await github.paginate(github.rest.pulls.list, {
689+
owner,
690+
repo,
691+
state: 'open',
692+
per_page: 100,
693+
});
654694
for (const event of verdictEvents) {
655-
let stillRelevant = false;
656-
let baseChangedWithSameHead = null;
657-
for (const pullNumber of event.prs) {
658-
const current = await github.rest.pulls.get({
659-
owner,
660-
repo,
661-
pull_number: pullNumber,
662-
});
663-
if (
664-
current.data.state === 'open' &&
665-
!current.data.draft &&
666-
current.data.base.ref === defaultBranch &&
667-
current.data.head.sha === event.head_sha &&
668-
current.data.base.sha === event.base_sha
669-
) {
670-
stillRelevant = true;
671-
break;
672-
}
673-
if (
674-
current.data.state === 'open' &&
675-
!current.data.draft &&
676-
current.data.base.ref === defaultBranch &&
677-
current.data.head.sha === event.head_sha &&
678-
current.data.base.sha !== event.base_sha
679-
) {
680-
baseChangedWithSameHead = current.data;
681-
}
682-
}
695+
const stillRelevant = currentOpenPulls.some(
696+
(current) =>
697+
!current.draft &&
698+
current.base.ref === defaultBranch &&
699+
current.head.sha === event.head_sha &&
700+
current.base.sha === event.base_sha,
701+
);
702+
const baseChangedWithSameHead = currentOpenPulls.find(
703+
(current) =>
704+
!current.draft &&
705+
current.base.ref === defaultBranch &&
706+
current.head.sha === event.head_sha &&
707+
current.base.sha !== event.base_sha,
708+
);
683709
684710
if (!stillRelevant) {
685711
// If only the base moved, neutral would satisfy a required
@@ -695,14 +721,21 @@ jobs:
695721
repo,
696722
check_run_id: event.check_run_id,
697723
status: 'completed',
698-
conclusion: baseChangedWithSameHead ? 'failure' : 'neutral',
724+
conclusion:
725+
baseChangedWithSameHead || event.kind === 'findings'
726+
? 'failure'
727+
: 'neutral',
699728
completed_at: now.toISOString(),
700729
output: {
701730
title: baseChangedWithSameHead
702731
? 'Codex review stale after base update'
732+
: event.kind === 'findings'
733+
? 'Codex review found blocking issues'
703734
: 'Codex review superseded',
704735
summary: baseChangedWithSameHead
705736
? 'The default branch changed while the Codex result was being reconciled. Update or rebase the pull request so its new base can be reviewed.'
737+
: event.kind === 'findings'
738+
? `Managed Codex posted ${event.findingCount} review finding(s) for this commit.`
706739
: 'The pull request head or base changed while its Codex result was being reconciled.',
707740
},
708741
});

0 commit comments

Comments
 (0)