Skip to content

Commit 9a801b0

Browse files
zoomote[bot]roomoteedelauna
authored
[Fix] Review-state labels swap on unchanged PRs while GitHub computes mergeability (Zoo-Code-Org#2091)
* [Fix] Review-state labels swap on unchanged PRs while GitHub computes mergeability (Zoo-Code-Org#2089) * fix(ci): preserve PR labels on mergeability retry failure --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent 9ef94f2 commit 9a801b0

2 files changed

Lines changed: 276 additions & 71 deletions

File tree

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

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ jobs:
364364
}
365365
}
366366
367-
// Strips stateLabels from a PR, optionally keeping one desired label
368-
// plus any labels in keepLabels (e.g. has-conflicts while mergeability
369-
// is unknown — kept, never added, per the hoisted check below).
367+
// Strips stateLabels from a PR, optionally keeping one desired label.
368+
// Callers skip this entirely while mergeability is unknown (see the
369+
// hoisted check below), so no keep-list is needed.
370370
// Stale-clock labels are owned by the stale step below and are never
371371
// touched here: stripping one on a transient CI or mergeability flap
372372
// would erase a live warning and restart its close grace (issue #2076).
373373
// Only skipped when this run's own token is read-only (see isReadOnlyRun) —
374374
// schedule/workflow_dispatch runs reconcile fork PRs normally.
375-
async function reconcileLabels(pr, desiredLabel, keepLabels = []) {
375+
async function reconcileLabels(pr, desiredLabel) {
376376
if (isReadOnlyRun && isForkPR(pr)) {
377377
core.info(`PR #${pr.number}: fork PR on a read-only run — skipping label mutation`);
378378
return;
@@ -393,7 +393,7 @@ jobs:
393393
}
394394
}
395395
for (const label of stateLabels) {
396-
if (label !== desiredLabel && !keepLabels.includes(label) && currentLabels.has(label)) {
396+
if (label !== desiredLabel && currentLabels.has(label)) {
397397
try {
398398
await github.rest.issues.removeLabel({
399399
owner, repo, issue_number: pr.number, name: label,
@@ -737,8 +737,39 @@ jobs:
737737
// GET endpoint and are computed asynchronously by GitHub — this
738738
// shard's own pulls.get above provides them, but even that can
739739
// return `null`/"unknown" while the merge check is still running.
740-
// Treat "unknown" as not-yet-computed rather than as conflicting.
741-
const prDetail = pr;
740+
// Treat "unknown" as not-yet-computed rather than as conflicting,
741+
// and give GitHub a bounded chance to finish computing it: retry
742+
// the read up to 3 times with a short wait between reads before
743+
// applying the unknown handling below (issue #2089). The wait is
744+
// env-overridable so the test suite can retry without real waits.
745+
const mergeabilityRetryDelayMs = Number(process.env.MERGEABILITY_RETRY_DELAY_MS ?? 5000);
746+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
747+
let prDetail = pr;
748+
for (
749+
let attempt = 1;
750+
attempt <= 3 &&
751+
(prDetail.mergeable === null || prDetail.mergeable_state === 'unknown');
752+
attempt++
753+
) {
754+
await sleep(mergeabilityRetryDelayMs);
755+
core.info(`PR #${pr.number}: mergeability unknown — retrying read (${attempt}/3)`);
756+
try {
757+
const { data: retryDetail } = await github.rest.pulls.get({
758+
owner, repo, pull_number: pr.number,
759+
});
760+
prDetail = retryDetail;
761+
} catch (error) {
762+
// The loop condition guarantees mergeability was still
763+
// unknown when this retry started. A failed retry (for
764+
// example HTTP 503) adds no new mergeability answer, so
765+
// the outer cleanup must keep the current state labels
766+
// exactly like the still-unknown path below (issue
767+
// #2089). Gate invalidation and failure reporting still
768+
// happen in the outer catch.
769+
error.preserveStateLabels = true;
770+
throw error;
771+
}
772+
}
742773
743774
if (prDetail.mergeable === false && prDetail.mergeable_state === 'dirty') {
744775
core.info(`PR #${pr.number}: has merge conflicts — labeling has-conflicts`);
@@ -751,24 +782,29 @@ jobs:
751782
}
752783
753784
// Unknown mergeability is handled once here, ahead of every
754-
// branch that reconciles labels (issue #2045). GitHub recomputes
755-
// mergeability asynchronously after each push and transiently
756-
// reports `mergeable: null` / `mergeable_state: "unknown"` while
757-
// the background job runs, so unknown says nothing about
758-
// conflicts: an existing has-conflicts label is kept through
759-
// reconciliation — unknown is not evidence the conflicts went
760-
// away — and it is never added, since unknown is not evidence
761-
// of conflicts either. Known-dirty never reaches here (the
762-
// conflict check above handles it first); known-clean keeps the
763-
// strip. The awaiting-maintainer branch below still refetches
764-
// mergeability before the maintainer handoff, because a push
765-
// between this fetch and labeling can change the answer.
766-
// A kept has-conflicts label can sit next to other state labels
767-
// until mergeability is known. This is intended.
768-
const mergeabilityUnknown =
785+
// branch that reconciles labels (issues #2045 and #2089). If the
786+
// reads above still report `mergeable: null` /
787+
// `mergeable_state: "unknown"`, GitHub has not finished computing
788+
// the answer, so it says nothing about conflicts and nothing about
789+
// whose turn it is. This run then changes NO state label for the
790+
// PR: every state-label reconcile below is skipped. Acting on
791+
// unknown previously let the normal reconcile swap labels (for
792+
// example has-conflicts to awaiting-author and back on the next
793+
// run's `dirty` read), which flapped labels on unchanged PRs.
794+
// Known-dirty never reaches here (the conflict check above
795+
// handles it first); known-clean reconciles normally. The
796+
// awaiting-maintainer branch below still refetches mergeability
797+
// before the maintainer handoff, because a push between this
798+
// fetch and labeling can change the answer; a known read there
799+
// re-enables the final reconcile.
800+
let mergeabilityUnknown =
769801
prDetail.mergeable === null || prDetail.mergeable_state === 'unknown';
770-
const keepHasConflicts = mergeabilityUnknown &&
771-
pr.labels.some(label => label.name === 'has-conflicts');
802+
if (mergeabilityUnknown) {
803+
core.info(
804+
`PR #${pr.number}: mergeability still unknown after retries — ` +
805+
'leaving state labels unchanged this run'
806+
);
807+
}
772808
773809
// Check CI status for required checks on the PR's head commit only.
774810
// Scoping to required checks avoids advisory checks (e.g. codecov/patch)
@@ -866,20 +902,25 @@ jobs:
866902
867903
// While CI is running or has failed, remove state labels and move on.
868904
// CI failure is its own signal; the label would add noise, not clarity.
869-
// Exception (issue #2025): while GitHub is still computing
870-
// mergeability, a missing required check can simply mean a
871-
// conflicting PR got no merge-ref CI run — not that its
872-
// conflicts went away. keepHasConflicts (computed once above)
873-
// keeps an existing has-conflicts label through this branch
874-
// instead of stripping it with the other state labels.
905+
// Exception (issues #2025 and #2089): while GitHub is still
906+
// computing mergeability, a missing required check can simply
907+
// mean a conflicting PR got no merge-ref CI run — not that its
908+
// conflicts went away. Unknown mergeability changes no state
909+
// label in this run, so this branch keeps every existing state
910+
// label (has-conflicts included) instead of stripping them; a
911+
// later run with a known read reconciles them.
875912
if (ciPending || ciFailed) {
876913
core.info(
877-
`PR #${pr.number}: CI ${ciPending ? 'pending' : 'failed'} — stripping state labels` +
878-
(keepHasConflicts ? ' (keeping has-conflicts while mergeability is unknown)' : '')
914+
`PR #${pr.number}: CI ${ciPending ? 'pending' : 'failed'} — ` +
915+
(mergeabilityUnknown
916+
? 'mergeability unknown; leaving state labels unchanged'
917+
: 'stripping state labels')
879918
);
880919
await updateReviewGate(pr, ciPending ? 'ci-pending' : 'ci-failed', false);
881920
await setCodeRabbitReviewActive(pr, false);
882-
await reconcileLabels(pr, keepHasConflicts ? 'has-conflicts' : null);
921+
if (!mergeabilityUnknown) {
922+
await reconcileLabels(pr, null);
923+
}
883924
await reconcileCommunityApproval(pr, 'remove');
884925
await updateReviewGuide(
885926
pr,
@@ -1132,6 +1173,10 @@ jobs:
11321173
);
11331174
continue;
11341175
}
1176+
// The refetch above settled mergeability with a fresh known read,
1177+
// so the final state-label reconcile below may run even when the
1178+
// shard's first reads were unknown (issue #2089).
1179+
mergeabilityUnknown = false;
11351180
communityDecision = headChanged
11361181
? 'remove'
11371182
: communityApprovalFresh
@@ -1166,7 +1211,9 @@ jobs:
11661211
if (recyclingActiveLabel) {
11671212
await updateReviewGuide(pr, phase, existingGuide);
11681213
}
1169-
await reconcileLabels(pr, desiredLabel, keepHasConflicts ? ['has-conflicts'] : []);
1214+
if (!mergeabilityUnknown) {
1215+
await reconcileLabels(pr, desiredLabel);
1216+
}
11701217
await reconcileCommunityApproval(pr, communityDecision);
11711218
if (readyForMaintainer) {
11721219
await updateReviewGate(pr, phase, true);

0 commit comments

Comments
 (0)