Skip to content

Commit bd19801

Browse files
committed
fix(github): PR limits stay off until enabled, and the sweep never acts on stale conflict state
The limit and both windows log what they would do until the PR_LIMITS_ENFORCE repository variable is true. A lookup that comes back without a mergeability answer is asked once more and then skipped. The sweep starts further down the list each day so the lookup cap cannot starve the same PRs, and a new PR counts only the author's older ones, which needs no concurrency group.
1 parent 18db1dd commit bd19801

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

‎.github/scripts/pr-limits.cjs‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const fs = require("node:fs");
22

3+
const DAY_MS = 24 * 60 * 60 * 1000;
34
const OPEN_LIMIT = 8;
4-
const CONFLICT_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
5+
const CONFLICT_WINDOW_MS = 7 * DAY_MS;
56
const CLOSING_WINDOW_MS = 72 * 60 * 60 * 1000;
67
// A first run meets the whole backlog; cap it so one run cannot post hundreds of comments.
78
const MAX_WARNINGS_PER_RUN = 100;
@@ -10,6 +11,7 @@ const MAX_CLOSES_PER_RUN = 50;
1011
const MAX_LOOKUPS_PER_RUN = 300;
1112
const MERGEABLE_ATTEMPTS = 3;
1213
const MERGEABLE_RETRY_MS = 30_000;
14+
const MERGEABLE_LOOKUP_RETRY_MS = 5_000;
1315

1416
const REBASE_LABEL = "needs-rebase";
1517
const CLOSING_LABEL = "triage:closing";
@@ -151,7 +153,12 @@ async function sweep({ github, context, core, dryRun }) {
151153
}
152154
if (!openPrs.some((pr) => pr.mergeable === "UNKNOWN" && readsMergeable(pr))) break;
153155
}
154-
const candidates = openPrs.filter((pr) => sweepStep(pr) !== "skip");
156+
// Start each day further down the list so the lookup cap cannot starve the same PRs every run.
157+
const due = openPrs.filter((pr) => sweepStep(pr) !== "skip");
158+
const start = due.length
159+
? (Math.floor(Date.now() / DAY_MS) * MAX_LOOKUPS_PER_RUN) % due.length
160+
: 0;
161+
const candidates = [...due.slice(start), ...due.slice(0, start)];
155162

156163
const plan = [];
157164
let warnings = 0;
@@ -174,12 +181,17 @@ async function sweep({ github, context, core, dryRun }) {
174181
core.warning(`Stopped at ${MAX_LOOKUPS_PER_RUN} lookups; the next run continues.`);
175182
break;
176183
}
177-
lookups += 1;
178-
// The list can be minutes old by the time we act, so decide again on fresh state. A merge to
179-
// main in between resets mergeability; it cannot resolve a conflict, so keep the listed answer.
180-
const detail = (await github.graphql(PR_DETAIL_QUERY, { ...repo, number })).repository
181-
.pullRequest;
182-
if (detail.mergeable === "UNKNOWN") detail.mergeable = candidate.mergeable;
184+
// The list can be minutes old by the time we act, so decide again on fresh state. A push or a
185+
// merge to main in between resets mergeability: ask once more, and never act on the old answer.
186+
const lookup = async () => {
187+
lookups += 1;
188+
return (await github.graphql(PR_DETAIL_QUERY, { ...repo, number })).repository.pullRequest;
189+
};
190+
let detail = await lookup();
191+
if (detail.state === "OPEN" && detail.mergeable === "UNKNOWN" && readsMergeable(detail)) {
192+
await new Promise((resolve) => setTimeout(resolve, MERGEABLE_LOOKUP_RETRY_MS));
193+
detail = await lookup();
194+
}
183195
const step = detail.state === "OPEN" ? sweepStep(detail) : "skip";
184196
if (step === "skip") continue;
185197

@@ -260,7 +272,7 @@ async function sweep({ github, context, core, dryRun }) {
260272
return plan;
261273
}
262274

263-
async function enforceOpenLimit({ github, context, core }) {
275+
async function enforceOpenLimit({ github, context, core, dryRun }) {
264276
const pr = context.payload.pull_request;
265277
const author = pr.user.login;
266278
const status = process.env.VOUCH_STATUS;
@@ -273,13 +285,21 @@ async function enforceOpenLimit({ github, context, core }) {
273285
// Someone else reopening a PR is a maintainer overriding the limit.
274286
if (context.payload.action === "reopened" && context.payload.sender.login !== author) return;
275287

276-
const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open author:${author}`;
288+
const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open author:${author} sort:created-asc`;
277289
const { search } = await github.graphql(AUTHOR_OPEN_PRS_QUERY, { q });
278-
const others = search.nodes.filter((node) => node.number && node.number !== pr.number).length;
290+
// A new PR counts only the older ones, so two opened together cannot close each other.
291+
const reopened = context.payload.action === "reopened";
292+
const others = search.nodes.filter(
293+
(node) => node.number && (reopened ? node.number !== pr.number : node.number < pr.number),
294+
).length;
279295
if (others < OPEN_LIMIT) {
280296
core.info(`PR #${pr.number}: ${author} has ${others} other open PRs.`);
281297
return;
282298
}
299+
if (dryRun) {
300+
core.notice(`Dry run. PR #${pr.number} would close: ${author} has ${others} other open PRs.`);
301+
return;
302+
}
283303

284304
await closeWithComment(
285305
github,

‎.github/workflows/pr-limits.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: PR Limits
22

33
# Deterministic PR rules from CONTRIBUTING.md: the open PR limit, the conflict
44
# window, and the 72 hour close window that follows a triage notice.
5+
#
6+
# Nothing is labeled, commented on, or closed until the repository variable
7+
# PR_LIMITS_ENFORCE is "true". Until then every run logs what it would do.
58

69
on:
710
pull_request_target:
@@ -29,10 +32,6 @@ jobs:
2932
github.event_name == 'pull_request_target' &&
3033
(github.event.action == 'opened' || github.event.action == 'reopened')
3134
runs-on: ubuntu-24.04
32-
# One author's PRs are counted in turn, so two opened together cannot both pass.
33-
concurrency:
34-
group: pr-open-limit-${{ github.event.pull_request.user.login }}
35-
cancel-in-progress: false
3635
steps:
3736
# pull_request_target has a write token. Load the script from the default
3837
# branch and never run PR code.
@@ -55,10 +54,11 @@ jobs:
5554
uses: actions/github-script@v8
5655
env:
5756
VOUCH_STATUS: ${{ steps.vouch.outputs.status }}
57+
DRY_RUN: ${{ vars.PR_LIMITS_ENFORCE != 'true' }}
5858
with:
5959
script: |
6060
const limits = require("./.github/scripts/pr-limits.cjs");
61-
await limits.enforceOpenLimit({ github, context, core });
61+
await limits.enforceOpenLimit({ github, context, core, dryRun: process.env.DRY_RUN === "true" });
6262
6363
author-activity:
6464
name: Clear windows on author activity
@@ -110,7 +110,7 @@ jobs:
110110
- name: Apply the conflict and close windows
111111
uses: actions/github-script@v8
112112
env:
113-
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && inputs.dry_run }}
113+
DRY_RUN: ${{ vars.PR_LIMITS_ENFORCE != 'true' || (github.event_name == 'workflow_dispatch' && inputs.dry_run) }}
114114
with:
115115
script: |
116116
const limits = require("./.github/scripts/pr-limits.cjs");

0 commit comments

Comments
 (0)