Skip to content

Commit 9091e17

Browse files
committed
fix(github): a close re-reads mergeability, and the open limit decides trust itself
A merge to the base can resolve a conflict without touching the PR, so the last look before a close now reads mergeability too. The open limit reads VOUCHED.td and the author's permission directly: the vouch action reports its own failed lookups as not vouched, and every read here throws instead, which closes nothing. Comments are no longer retried blindly, and the sweep waits long enough for GitHub to compute mergeability.
1 parent c8b01a4 commit 9091e17

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ const NOTICE_MS = 72 * HOUR_MS;
99
const MAX_REQUESTS_PER_RUN = 100;
1010
const MAX_CLOSES_PER_RUN = 50;
1111
const WRITE_PAUSE_MS = 1_000;
12-
const MERGEABLE_WAIT_MS = 60_000;
12+
// Measured here: about two and a half minutes until every open PR has an answer.
13+
const MERGEABLE_WAIT_MS = 150_000;
1314
// GitHub stamps a label event and the PR's updatedAt a second or two apart.
1415
const SAME_MOMENT_MS = 15_000;
1516

1617
const REBASE_LABEL = "needs-rebase";
1718
const CLOSING_LABEL = "triage:closing";
1819
const KEEP_OPEN_LABEL = "triage:keep-open";
1920
const UNVOUCHED_LABEL = "vouch:unvouched";
20-
const TRUSTED_VOUCH_STATUSES = new Set(["bot", "collaborator", "vouched"]);
21+
const WRITE_PERMISSIONS = new Set(["admin", "maintain", "write"]);
2122
const EXEMPTIONS_PATH = ".github/TRIAGE_EXEMPTIONS.td";
23+
const VOUCHED_PATH = ".github/VOUCHED.td";
2224
const LOGIN_PATTERN = /^github:([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38})$/;
2325

2426
// `labels` matches a PR that carries any one of them.
@@ -45,7 +47,12 @@ const SWEEP_QUERY = `
4547
const RECHECK_QUERY = `
4648
query ($owner: String!, $repo: String!, $number: Int!) {
4749
repository(owner: $owner, name: $repo) {
48-
pullRequest(number: $number) { state updatedAt labels(first: 100) { nodes { name } } }
50+
pullRequest(number: $number) {
51+
state
52+
mergeable
53+
updatedAt
54+
labels(first: 100) { nodes { name } }
55+
}
4956
}
5057
}`;
5158

@@ -72,6 +79,17 @@ function parseExemptions(text) {
7279
return logins;
7380
}
7481

82+
// VOUCHED.td: one `github:login` per line, `#` comments, and `-github:login reason` to denounce.
83+
function vouchedLogins(text) {
84+
const logins = new Set();
85+
for (const raw of text.split("\n")) {
86+
const entry = raw.trim().split(/\s+/)[0];
87+
if (entry.startsWith("github:")) logins.add(entry.slice("github:".length).toLowerCase());
88+
}
89+
if (logins.size === 0) throw new Error(`${VOUCHED_PATH}: no entries`);
90+
return logins;
91+
}
92+
7593
function labeledAt(pr, name) {
7694
const last = pr.timelineItems.nodes.filter((event) => event.label?.name === name).at(-1);
7795
return last ? Date.parse(last.createdAt) : undefined;
@@ -167,7 +185,9 @@ async function sweep({ github, context, core, dryRun }) {
167185
const fresh = (await github.graphql(RECHECK_QUERY, { ...repo, number })).repository
168186
.pullRequest;
169187
if (fresh.state !== "OPEN") continue;
170-
action = decide({ ...pr, updatedAt: fresh.updatedAt, labels: fresh.labels }, exemptions, now);
188+
// A merge to the base can resolve a conflict without touching the PR, so this is re-read
189+
// too. An answer that is not ready yet reads as "not conflicting" and waits for tomorrow.
190+
action = decide({ ...pr, ...fresh }, exemptions, now);
171191
if (!action.startsWith("close")) continue;
172192
}
173193

@@ -229,13 +249,16 @@ async function sweep({ github, context, core, dryRun }) {
229249
async function enforceOpenLimit({ github, context, core, dryRun }) {
230250
const pr = context.payload.pull_request;
231251
const author = pr.user.login;
232-
const status = process.env.VOUCH_STATUS;
233-
if (!status) {
234-
core.warning(`PR #${pr.number}: no vouch status, leaving it open.`);
235-
return;
236-
}
237-
if (TRUSTED_VOUCH_STATUSES.has(status)) return;
252+
// Who is trusted is decided here rather than by the vouch action, which reports its own failed
253+
// lookups as "not vouched". Every read below throws on failure, and a throw closes nothing.
254+
if (pr.user.type === "Bot") return;
238255
if (parseExemptions(fs.readFileSync(EXEMPTIONS_PATH, "utf8")).has(author.toLowerCase())) return;
256+
if (vouchedLogins(fs.readFileSync(VOUCHED_PATH, "utf8")).has(author.toLowerCase())) return;
257+
const { data: access } = await github.rest.repos.getCollaboratorPermissionLevel({
258+
...context.repo,
259+
username: author,
260+
});
261+
if (WRITE_PERMISSIONS.has(access.permission)) return;
239262
if (pr.labels.some((label) => label.name === KEEP_OPEN_LABEL)) return;
240263
// Someone else reopening a PR is a maintainer overriding the limit.
241264
const reopened = context.payload.action === "reopened";

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,16 @@ jobs:
3232
steps:
3333
# pull_request_target has a write token. Load the script from the default
3434
# branch and never run PR code. Cone mode also checks out the files beside
35-
# .github/scripts, which is where TRIAGE_EXEMPTIONS.td lives.
35+
# .github/scripts, which is where TRIAGE_EXEMPTIONS.td and VOUCHED.td live.
3636
- name: Checkout trusted scripts
3737
uses: actions/checkout@v6
3838
with:
3939
ref: ${{ github.event.repository.default_branch }}
4040
sparse-checkout: .github/scripts
4141

42-
- id: vouch
43-
name: Check PR author trust
44-
uses: mitchellh/vouch/action/check-user@v1
45-
with:
46-
user: ${{ github.event.pull_request.user.login }}
47-
allow-fail: true
48-
env:
49-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50-
5142
- name: Close PRs over the limit
5243
uses: actions/github-script@v8
5344
env:
54-
VOUCH_STATUS: ${{ steps.vouch.outputs.status }}
5545
DRY_RUN: ${{ vars.PR_LIMITS_ENFORCE != 'true' }}
5646
with:
5747
script: |
@@ -77,7 +67,6 @@ jobs:
7767
env:
7868
DRY_RUN: ${{ vars.PR_LIMITS_ENFORCE != 'true' || (github.event_name == 'workflow_dispatch' && inputs.dry_run) }}
7969
with:
80-
retries: 3
8170
script: |
8271
const limits = require("./.github/scripts/pr-limits.cjs");
8372
await limits.sweep({ github, context, core, dryRun: process.env.DRY_RUN === "true" });

0 commit comments

Comments
 (0)