Skip to content

Commit aa555be

Browse files
committed
ci: harden the release labeler and the pull request lookup
- Look up the commits of a push ten at a time, label the pull requests that were found when a lookup or a label fails, and fail the run afterwards. - Return the current pull requests of the branch from the shared lookup, so a delayed run for an older commit still labels the latest head. fork_ci_status keeps comparing the commit. - Skip the fork_ci_status job for runs that are not from a fork before a runner starts.
1 parent f64546b commit aa555be

6 files changed

Lines changed: 148 additions & 35 deletions

File tree

‎.github/workflows/fork_ci_status.yml‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ concurrency:
4343
jobs:
4444
bridge:
4545
name: Mirror the fork run
46-
if: github.event_name != 'workflow_run' || github.event.workflow_run.event == 'pull_request'
46+
# A `workflow_run` needs a runner only for a pull request from a fork. The
47+
# base is checked in `notify()`.
48+
if: github.event_name != 'workflow_run' || (github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.head_repository.fork == true)
4749
runs-on: ubuntu-slim
4850
timeout-minutes: 10
4951
steps:
@@ -213,13 +215,18 @@ jobs:
213215
async function notify() {
214216
// The same branch can also target a release branch, which gets no
215217
// mirrored check, so look only at the pull request to `main`.
216-
const [pr] = await require('./ci/scripts/pull_requests_of_run.js')({
218+
const pulls = await require('./ci/scripts/pull_requests_of_run.js')({
217219
github,
218220
context,
219221
base: 'main',
220222
});
223+
// A run for an older commit finds nothing, and the scheduled
224+
// update creates the check of the new head.
225+
const pr = pulls.find(
226+
(pull) => pull.head.sha === context.payload.workflow_run.head_sha
227+
);
221228
if (!pr) {
222-
core.info('No open pull request to main for this commit; nothing to mirror.');
229+
core.info('No open pull request to main at this commit; nothing to mirror.');
223230
return;
224231
}
225232
// Only fork PRs to main skip jobs upstream; release branches have

‎.github/workflows/labeler.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ jobs:
3737
persist-credentials: false
3838

3939
# The same branch can target `main` and a release branch, so label every
40-
# match.
40+
# match. These are the current pull requests, so a delayed run for an
41+
# older commit still labels the latest head.
4142
- name: Find the pull requests
4243
id: pr
4344
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0

‎ci/scripts/pull_requests_of_run.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
// Used by the labeler and fork_ci_status workflows, which run on `workflow_run`.
66
//
77
// `workflow_run.pull_requests` is empty for pull requests from forks, so find
8-
// the open pull requests by head branch and commit. A commit that is no longer
9-
// a head matches nothing, since a newer run handles the new head. The same
10-
// branch can target several bases, so pass `base` to select one.
8+
// the open pull requests of the run's head branch. They are the current ones,
9+
// which can be newer than the run's commit when pushes follow each other, so
10+
// compare `head.sha` with `run.head_sha` where only that commit matters. The
11+
// same branch can target several bases, so pass `base` to select one.
1112
module.exports = async ({ github, context, base }) => {
1213
const run = context.payload.workflow_run;
1314
// The head repository is null when the fork was deleted.
@@ -21,5 +22,5 @@ module.exports = async ({ github, context, base }) => {
2122
head: `${run.head_repository.owner.login}:${run.head_branch}`,
2223
...(base && { base })
2324
});
24-
return data.filter((pull) => pull.head.sha === run.head_sha);
25+
return data;
2526
};

‎ci/scripts/release_version_labeler.js‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
// Used by the release_version_labeler workflow
66

7+
// A push can have many commits. Look them up this many at a time, to stay well
8+
// below GitHub's limit of 100 concurrent requests.
9+
const BATCH_SIZE = 10;
10+
711
// A squash merge or a merge queue commit maps to the merged pull request. The
812
// pull request can lag the push by a few seconds, so retry.
913
async function mergedPulls({ github, context, sha }) {
@@ -44,21 +48,44 @@ module.exports = async ({ github, context }) => {
4448
}
4549
}
4650

47-
// A push can include several merged pull requests. Look the commits up in
48-
// parallel, so the retries of commits without one overlap.
51+
// A push can include several merged pull requests. Look the commits up a
52+
// batch at a time, so the retries of commits without one overlap. A failed
53+
// lookup or label must not cost the other pull requests their label, so
54+
// collect the errors and throw the first one after labeling.
4955
const pushed = (context.payload.commits || []).map((commit) => commit.id);
5056
const shas = pushed.length ? pushed : [context.sha];
51-
const found = await Promise.all(shas.map((sha) => mergedPulls({ github, context, sha })));
52-
const numbers = new Set(found.flat().map((pull) => pull.number));
53-
if (numbers.size === 0) {
57+
const numbers = new Set();
58+
const errors = [];
59+
for (let i = 0; i < shas.length; i += BATCH_SIZE) {
60+
const batch = shas.slice(i, i + BATCH_SIZE);
61+
const settled = await Promise.allSettled(
62+
batch.map((sha) => mergedPulls({ github, context, sha }))
63+
);
64+
for (const result of settled) {
65+
if (result.status === 'fulfilled') {
66+
result.value.forEach((pull) => numbers.add(pull.number));
67+
} else {
68+
errors.push(result.reason);
69+
}
70+
}
71+
}
72+
if (numbers.size === 0 && errors.length === 0) {
5473
console.log(`No merged pull request found for ${shas.join(', ')}`);
5574
}
5675
for (const number of numbers) {
57-
await github.rest.issues.addLabels({
58-
owner: context.repo.owner,
59-
repo: context.repo.repo,
60-
issue_number: number,
61-
labels: [target_version]
62-
});
76+
try {
77+
await github.rest.issues.addLabels({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
issue_number: number,
81+
labels: [target_version]
82+
});
83+
} catch (error) {
84+
errors.push(error);
85+
}
86+
}
87+
if (errors.length > 0) {
88+
console.log(`${errors.length} request(s) failed. The pull requests that were found are labeled.`);
89+
throw errors[0];
6390
}
6491
};

‎ci/scripts/test_fork_ci_status.cjs‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,16 @@ const lookup = async ({ openPrs, base, triggeringRun = workflowRun }) => {
233233
return { found: found.map((p) => p.number), calls };
234234
};
235235

236-
test("lookup returns every pull request from the head commit and no stale one", async () => {
237-
const stale = {
236+
test("lookup returns the current pull requests of the branch whatever the run's commit", async () => {
237+
// A newer push moved the head of one pull request after the run started.
238+
const newer = {
238239
...pr,
239240
number: 3,
240241
head: { ...pr.head, sha: "b".repeat(40) },
241242
};
242243
const release = { ...pr, number: 2, base: { ref: "branch-55" } };
243-
const { found, calls } = await lookup({ openPrs: [stale, release, pr] });
244-
assert.deepEqual(found, [2, 1]);
244+
const { found, calls } = await lookup({ openPrs: [newer, release, pr] });
245+
assert.deepEqual(found, [3, 2, 1]);
245246
assert.deepEqual(calls, [
246247
{
247248
owner: "apache",
@@ -413,21 +414,22 @@ test("a recent active PR in the sweep slice is only polled once", async () => {
413414
assert.deepEqual(calls, ["checks.listForRef", "actions.listWorkflowRuns"]);
414415
});
415416

416-
// The concurrency expressions use string lookups, format() and &&/||, whose
417+
// The workflow expressions use string lookups, format() and &&/||, whose
417418
// semantics match JavaScript for these non-empty strings and booleans. A
418419
// missing or null property is null in a GitHub expression, so read it with ?.
419420
// and print it as an empty string in format().
420421
const format = (template, ...args) =>
421422
template.replace(/\{(\d+)\}/g, (_, index) => args[index] ?? "");
423+
const evaluate = (expression, github) =>
424+
new Function(
425+
"github",
426+
"format",
427+
`return (${expression.replace(/\./g, "?.")});`
428+
)(github, format);
422429
function concurrencyGroup(github, workflow = rustWorkflow) {
423430
return workflow.concurrency.group.replace(
424431
/\$\{\{\s*(.*?)\s*\}\}/g,
425-
(_, expression) =>
426-
new Function(
427-
"github",
428-
"format",
429-
`return (${expression.replace(/\./g, "?.")});`
430-
)(github, format)
432+
(_, expression) => evaluate(expression, github)
431433
);
432434
}
433435
const forkPush = {
@@ -509,3 +511,23 @@ test("a signal run from a deleted fork does not join the group of the sweep", ()
509511
group({ ...sweepRun, event_name: "workflow_dispatch" })
510512
);
511513
});
514+
515+
test("signal runs skip the job unless they come from a fork", () => {
516+
const runs = (github) => evaluate(forkStatus.jobs.bridge.if, github);
517+
const signal = (event, head_repository) => ({
518+
event_name: "workflow_run",
519+
event: { workflow_run: { event, head_repository } },
520+
});
521+
assert.equal(runs(signal("pull_request", { fork: true })), true);
522+
// A branch of this repository, a deleted fork, and another triggering event.
523+
assert.equal(runs(signal("pull_request", { fork: false })), false);
524+
assert.equal(runs(signal("pull_request", null)), false);
525+
assert.equal(runs(signal("push", { fork: true })), false);
526+
});
527+
528+
test("the scheduled and manual sweeps always run the job", () => {
529+
for (const event_name of ["schedule", "workflow_dispatch"]) {
530+
const github = { event_name, event: {} };
531+
assert.equal(evaluate(forkStatus.jobs.bridge.if, github), true);
532+
}
533+
});

‎ci/scripts/test_release_version_labeler.cjs‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const pull = (
1717
base: { ref: base },
1818
});
1919

20-
// `lookups` maps a commit to the pull requests returned on each attempt. The
21-
// last response repeats once the attempts run out.
22-
async function execute({ lookups, commits }) {
20+
// `lookups` maps a commit to the pull requests returned on each attempt (the
21+
// last response repeats once the attempts run out), or to an error to throw.
22+
// `failLabels` lists the pull requests that fail to get the label.
23+
async function run({ lookups, commits, failLabels = [] }) {
2324
const result = {
2425
listed: [],
2526
labeled: [],
@@ -33,8 +34,12 @@ async function execute({ lookups, commits }) {
3334
rest: {
3435
issues: {
3536
getLabel: async () => {},
36-
addLabels: async ({ issue_number, labels }) =>
37-
result.labeled.push([issue_number, labels]),
37+
addLabels: async ({ issue_number, labels }) => {
38+
if (failLabels.includes(issue_number)) {
39+
throw new Error(`cannot label ${issue_number}`);
40+
}
41+
result.labeled.push([issue_number, labels]);
42+
},
3843
},
3944
repos: {
4045
listPullRequestsAssociatedWithCommit: async ({ commit_sha }) => {
@@ -44,6 +49,9 @@ async function execute({ lookups, commits }) {
4449
await Promise.resolve();
4550
result.inFlight--;
4651
const responses = lookups[commit_sha];
52+
if (responses instanceof Error) {
53+
throw responses;
54+
}
4755
attempts[commit_sha] = (attempts[commit_sha] || 0) + 1;
4856
return {
4957
data: responses[
@@ -65,12 +73,23 @@ async function execute({ lookups, commits }) {
6573
callback();
6674
};
6775
console.log = (message) => result.logs.push(message);
76+
let error;
6877
try {
6978
await labeler({ github, context });
79+
} catch (thrown) {
80+
error = thrown;
7081
} finally {
7182
globalThis.setTimeout = realSetTimeout;
7283
console.log = realLog;
7384
}
85+
return { result, error };
86+
}
87+
88+
async function execute(args) {
89+
const { result, error } = await run(args);
90+
if (error) {
91+
throw error;
92+
}
7493
return result;
7594
}
7695

@@ -144,3 +163,39 @@ test("looks up the pushed commit when the payload has no commits", async () => {
144163
[3]
145164
);
146165
});
166+
167+
test("looks up at most ten commits at a time", async () => {
168+
const commits = Array.from({ length: 25 }, (_, i) => `c${i}`);
169+
const lookups = Object.fromEntries(
170+
commits.map((sha, i) => [sha, [[pull(i + 1)]]])
171+
);
172+
const result = await execute({ commits, lookups });
173+
assert.equal(result.maxInFlight, 10);
174+
assert.equal(result.labeled.length, 25);
175+
});
176+
177+
test("labels the other pull requests when a lookup fails, then fails the run", async () => {
178+
const failure = new Error("rate limited");
179+
const { result, error } = await run({
180+
commits: ["a", "b", "c"],
181+
lookups: { a: [[pull(1)]], b: failure, c: [[pull(3)]] },
182+
});
183+
assert.equal(error, failure);
184+
assert.deepEqual(
185+
result.labeled.map(([number]) => number),
186+
[1, 3]
187+
);
188+
});
189+
190+
test("labels the other pull requests when one label fails, then fails the run", async () => {
191+
const { result, error } = await run({
192+
commits: ["a", "b"],
193+
lookups: { a: [[pull(1)]], b: [[pull(2)]] },
194+
failLabels: [1],
195+
});
196+
assert.match(error.message, /cannot label 1/);
197+
assert.deepEqual(
198+
result.labeled.map(([number]) => number),
199+
[2]
200+
);
201+
});

0 commit comments

Comments
 (0)