Skip to content

Commit 991a53e

Browse files
zoomote[bot]roomoteedelauna
committed
[Fix] has-conflicts label flaps off while GitHub recomputes mergeability (#2048)
* fix: keep has-conflicts label while mergeability is unknown and CI passes (#2045) * test: cover unknown-mergeability operands and clean control --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent df4da75 commit 991a53e

4 files changed

Lines changed: 74 additions & 28 deletions

File tree

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,13 @@ jobs:
354354
}
355355
}
356356
357-
// Strips stateLabels from a PR, optionally keeping one.
357+
// Strips stateLabels from a PR, optionally keeping one desired label
358+
// plus any labels in keepLabels (e.g. has-conflicts while mergeability
359+
// is unknown — kept, never added, per the hoisted check below).
358360
// Also removes stale-awaiting-author when not keeping awaiting-author.
359361
// Only skipped when this run's own token is read-only (see isReadOnlyRun) —
360362
// schedule/workflow_dispatch runs reconcile fork PRs normally.
361-
async function reconcileLabels(pr, desiredLabel) {
363+
async function reconcileLabels(pr, desiredLabel, keepLabels = []) {
362364
if (isReadOnlyRun && isForkPR(pr)) {
363365
core.info(`PR #${pr.number}: fork PR on a read-only run — skipping label mutation`);
364366
return;
@@ -379,7 +381,7 @@ jobs:
379381
}
380382
}
381383
for (const label of stateLabels) {
382-
if (label !== desiredLabel && currentLabels.has(label)) {
384+
if (label !== desiredLabel && !keepLabels.includes(label) && currentLabels.has(label)) {
383385
try {
384386
await github.rest.issues.removeLabel({
385387
owner, repo, issue_number: pr.number, name: label,
@@ -752,6 +754,26 @@ jobs:
752754
continue;
753755
}
754756
757+
// Unknown mergeability is handled once here, ahead of every
758+
// branch that reconciles labels (issue #2045). GitHub recomputes
759+
// mergeability asynchronously after each push and transiently
760+
// reports `mergeable: null` / `mergeable_state: "unknown"` while
761+
// the background job runs, so unknown says nothing about
762+
// conflicts: an existing has-conflicts label is kept through
763+
// reconciliation — unknown is not evidence the conflicts went
764+
// away — and it is never added, since unknown is not evidence
765+
// of conflicts either. Known-dirty never reaches here (the
766+
// conflict check above handles it first); known-clean keeps the
767+
// strip. The awaiting-maintainer branch below still refetches
768+
// mergeability before the maintainer handoff, because a push
769+
// between this fetch and labeling can change the answer.
770+
// A kept has-conflicts label can sit next to other state labels
771+
// until mergeability is known. This is intended.
772+
const mergeabilityUnknown =
773+
prDetail.mergeable === null || prDetail.mergeable_state === 'unknown';
774+
const keepHasConflicts = mergeabilityUnknown &&
775+
pr.labels.some(label => label.name === 'has-conflicts');
776+
755777
// Check CI status for required checks on the PR's head commit only.
756778
// Scoping to required checks avoids advisory checks (e.g. codecov/patch)
757779
// incorrectly blocking label assignment on otherwise-ready PRs.
@@ -851,16 +873,10 @@ jobs:
851873
// Exception (issue #2025): while GitHub is still computing
852874
// mergeability, a missing required check can simply mean a
853875
// conflicting PR got no merge-ref CI run — not that its
854-
// conflicts went away. Keep an existing has-conflicts label
855-
// through this branch instead of stripping it with the other
856-
// state labels. Known-clean mergeability keeps the strip, and
857-
// known-dirty never reaches here (the conflict check above
858-
// handles it first).
876+
// conflicts went away. keepHasConflicts (computed once above)
877+
// keeps an existing has-conflicts label through this branch
878+
// instead of stripping it with the other state labels.
859879
if (ciPending || ciFailed) {
860-
const mergeabilityUnknown =
861-
prDetail.mergeable === null || prDetail.mergeable_state === 'unknown';
862-
const keepHasConflicts = mergeabilityUnknown &&
863-
pr.labels.some(label => label.name === 'has-conflicts');
864880
core.info(
865881
`PR #${pr.number}: CI ${ciPending ? 'pending' : 'failed'} — stripping state labels` +
866882
(keepHasConflicts ? ' (keeping has-conflicts while mergeability is unknown)' : '')
@@ -1133,6 +1149,7 @@ jobs:
11331149
11341150
core.info(
11351151
`PR #${pr.number}: CI passing, reviews=${latest.size}, ` +
1152+
`mergeable=${prDetail.mergeable}, mergeable_state=${prDetail.mergeable_state}, ` +
11361153
`coderabbit=${freshCodeRabbitReview?.state ?? (codeRabbitEligibleAuthor ? 'pending' : 'optional')}, ` +
11371154
`maintainer=${maintainerApproval?.state ?? 'pending'}, blockers=${maintainerBlockers.size} → ${desiredLabel ?? '(none)'}`
11381155
);
@@ -1153,7 +1170,7 @@ jobs:
11531170
if (recyclingActiveLabel) {
11541171
await updateReviewGuide(pr, phase, existingGuide);
11551172
}
1156-
await reconcileLabels(pr, desiredLabel);
1173+
await reconcileLabels(pr, desiredLabel, keepHasConflicts ? ['has-conflicts'] : []);
11571174
await reconcileCommunityApproval(pr, communityDecision);
11581175
if (readyForMaintainer) {
11591176
await updateReviewGate(pr, phase, true);

‎pnpm-lock.yaml‎

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@
531531
"semver-compare": "^1.0.0",
532532
"serialize-error": "^12.0.0",
533533
"shell-quote": "^1.8.2",
534-
"simple-git": "^3.27.0",
534+
"simple-git": "^4.0.0",
535535
"strip-bom": "^5.0.0",
536536
"tiktoken": "^1.0.21",
537537
"tree-sitter-wasms": "^0.1.13",

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,35 @@ describe("PR review-state workflow", () => {
980980
expect(result.addLabels).not.toHaveBeenCalledWith(expect.objectContaining({ labels: ["has-conflicts"] }))
981981
})
982982

983+
// Regression: GitHub transiently reports mergeable: null / "unknown" after
984+
// each push while it recomputes mergeability. With CI passing, the final
985+
// reconcile must keep an existing has-conflicts label instead of stripping
986+
// it with the other state labels — the label previously flapped off until
987+
// a later run restored it. Each operand of the unknown check counts alone.
988+
it.each([
989+
{ mergeable: null, mergeableState: "unknown" },
990+
{ mergeable: null, mergeableState: "clean" },
991+
{ mergeable: true, mergeableState: "unknown" },
992+
])(
993+
"keeps has-conflicts while mergeability is unknown ($mergeable, $mergeableState) and required CI passes (#2045)",
994+
async ({ mergeable, mergeableState }) => {
995+
const result = await runWorkflow({ mergeable, mergeableState, labels: ["has-conflicts"] })
996+
997+
expect(result.removeLabel).not.toHaveBeenCalledWith(expect.objectContaining({ name: "has-conflicts" }))
998+
expect(result.addLabels).not.toHaveBeenCalledWith(expect.objectContaining({ labels: ["has-conflicts"] }))
999+
expect(result.addLabels).toHaveBeenCalledWith(
1000+
expect.objectContaining({ labels: ["coderabbit-review-active"] }),
1001+
)
1002+
},
1003+
)
1004+
1005+
it("strips has-conflicts when mergeability is known clean and required CI passes", async () => {
1006+
const result = await runWorkflow({ labels: ["has-conflicts"] })
1007+
1008+
expect(result.removeLabel).toHaveBeenCalledWith(expect.objectContaining({ name: "has-conflicts" }))
1009+
expect(result.addLabels).toHaveBeenCalledWith(expect.objectContaining({ labels: ["coderabbit-review-active"] }))
1010+
})
1011+
9831012
it("keeps has-conflicts while mergeability is unknown and required CI is missing (#2025)", async () => {
9841013
// A conflicting PR gets no merge-ref CI run, so its required checks are
9851014
// missing. Unknown mergeability must not read that as a reason to strip

0 commit comments

Comments
 (0)