Skip to content

Commit 9888f1b

Browse files
committed
ci: ignore skipped draft successes when picking the PR bench comment run
1 parent 223330c commit 9888f1b

2 files changed

Lines changed: 141 additions & 34 deletions

File tree

‎.github/workflows/pr-bench-comment.yml‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ jobs:
4949
const { owner, repo } = context.repo;
5050
const sha = context.payload.workflow_run.head_sha;
5151
const trigger = context.payload.workflow_run;
52-
// A newer in-progress, cancelled, or failed rerun must not hide a completed success.
52+
// Prefer a completed success that actually ran its jobs, so a newer
53+
// failure cannot hide it. A draft still concludes success when its
54+
// jobs are skipped: PR bench skips its only job, and CI skips the
55+
// gate while the required-check reporters pass. That empty success
56+
// must not hide the run that produced the report.
5357
const completed = async (workflow_id) => {
5458
const { data } = await github.rest.actions.listWorkflowRuns({
5559
owner,
@@ -65,7 +69,29 @@ jobs:
6569
runs.push(trigger);
6670
}
6771
runs.sort((a, b) => b.id - a.id);
68-
return runs.find((run) => run.conclusion === "success") ?? runs[0];
72+
const didWork = async (run) => {
73+
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
74+
owner,
75+
repo,
76+
run_id: run.id,
77+
filter: "latest",
78+
per_page: 100,
79+
});
80+
// Same gate as benchmarks/ci-timing.mjs `ranGatedJobs`.
81+
return workflow_id === "ci.yml"
82+
? jobs.some(
83+
(job) =>
84+
job.name === "classify changeset release" && job.conclusion === "success",
85+
)
86+
: jobs.some((job) => job.conclusion !== "skipped");
87+
};
88+
for (const run of runs) {
89+
if (run.conclusion === "success" && (await didWork(run))) return run;
90+
}
91+
for (const run of runs) {
92+
if (run.conclusion !== "success" && (await didWork(run))) return run;
93+
}
94+
return runs[0];
6995
};
7096
const bench = await completed("pr-bench.yml");
7197
const ci = await completed("ci.yml");

‎scripts/ci-workflow.test.mjs‎

Lines changed: 113 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,56 +1694,137 @@ describe('Pull request benchmark report', () => {
16941694
assert.match(packageJson.scripts['ci:workflow:test'], /benchmarks\/ci-timing\.test\.mjs/);
16951695
});
16961696

1697-
// A newer in-progress, cancelled, or failed rerun used to hide the completed success.
1698-
test('picks the completed successful run for the commit over newer reruns', async () => {
1699-
const sha = 'a'.repeat(40);
1700-
const run = (id, path, conclusion) => ({
1701-
id,
1702-
path: `.github/workflows/${path}`,
1703-
head_sha: sha,
1704-
conclusion,
1705-
});
1706-
const listed = {
1707-
'pr-bench.yml': [run(20, 'pr-bench.yml', 'cancelled'), run(9, 'pr-bench.yml', 'success')],
1708-
'ci.yml': [run(60, 'ci.yml', 'failure'), run(12, 'ci.yml', 'success')],
1709-
};
1697+
async function selectRuns({ listed, jobs = new Map(), trigger }) {
17101698
const queries = [];
17111699
const outputs = {};
1712-
const execute = new AsyncFunction(
1713-
'github',
1714-
'context',
1715-
'core',
1716-
stepScript(prBenchCommentWorkflow, 'Find runs for the commit'),
1717-
);
1700+
const listJobsForWorkflowRun = async () => undefined;
17181701
const github = {
17191702
rest: {
17201703
actions: {
17211704
listWorkflowRuns: async (query) => {
17221705
queries.push(query);
17231706
return { data: { workflow_runs: listed[query.workflow_id] } };
17241707
},
1708+
listJobsForWorkflowRun,
17251709
},
17261710
},
1711+
paginate: async (method, query) => {
1712+
assert.equal(method, listJobsForWorkflowRun);
1713+
if (jobs.has(query.run_id)) return jobs.get(query.run_id);
1714+
return [
1715+
{ name: 'benchmark report', conclusion: 'success' },
1716+
{ name: 'classify changeset release', conclusion: 'success' },
1717+
];
1718+
},
17271719
};
1728-
const core = { setOutput: (name, value) => (outputs[name] = value) };
1729-
const trigger = { ...run(70, 'pr-bench.yml', 'success'), status: 'completed' };
1720+
const execute = new AsyncFunction(
1721+
'github',
1722+
'context',
1723+
'core',
1724+
stepScript(prBenchCommentWorkflow, 'Find runs for the commit'),
1725+
);
17301726
await execute(
17311727
github,
17321728
{ repo: { owner: 'o', repo: 'r' }, payload: { workflow_run: trigger } },
1733-
core,
1729+
{ setOutput: (name, value) => (outputs[name] = value) },
17341730
);
1735-
assert.deepEqual(outputs, { bench: '70', ci: '12' });
1736-
assert.ok(queries.every((query) => query.status === 'completed' && query.head_sha === sha));
1731+
return { outputs, queries };
1732+
}
17371733

1738-
await execute(
1739-
github,
1740-
{
1741-
repo: { owner: 'o', repo: 'r' },
1742-
payload: { workflow_run: { ...trigger, id: 5, conclusion: 'failure' } },
1743-
},
1744-
core,
1734+
// A newer in-progress, cancelled, or failed rerun used to hide the completed success.
1735+
test('picks the completed successful run for the commit over newer reruns', async () => {
1736+
const sha = 'a'.repeat(40);
1737+
const run = (id, path, conclusion) => ({
1738+
id,
1739+
path: `.github/workflows/${path}`,
1740+
head_sha: sha,
1741+
conclusion,
1742+
});
1743+
const listed = {
1744+
'pr-bench.yml': [run(20, 'pr-bench.yml', 'cancelled'), run(9, 'pr-bench.yml', 'success')],
1745+
'ci.yml': [run(60, 'ci.yml', 'failure'), run(12, 'ci.yml', 'success')],
1746+
};
1747+
const trigger = { ...run(70, 'pr-bench.yml', 'success'), status: 'completed' };
1748+
const first = await selectRuns({ listed, trigger });
1749+
assert.deepEqual(first.outputs, { bench: '70', ci: '12' });
1750+
assert.ok(
1751+
first.queries.every((query) => query.status === 'completed' && query.head_sha === sha),
17451752
);
1746-
assert.deepEqual(outputs, { bench: '9', ci: '12' });
1753+
1754+
const second = await selectRuns({
1755+
listed,
1756+
trigger: { ...trigger, id: 5, conclusion: 'failure' },
1757+
});
1758+
assert.deepEqual(second.outputs, { bench: '9', ci: '12' });
1759+
});
1760+
1761+
// Opening a draft still completes PR bench and CI as success while the real
1762+
// jobs are skipped. Marking it ready reuses the SHA, so that empty success
1763+
// must not hide the later run that actually executed.
1764+
test('does not let a skipped draft success hide the run that executed the work', async () => {
1765+
const sha = 'b'.repeat(40);
1766+
const run = (id, workflowPath, conclusion) => ({
1767+
id,
1768+
path: `.github/workflows/${workflowPath}`,
1769+
head_sha: sha,
1770+
conclusion,
1771+
});
1772+
const listed = {
1773+
'pr-bench.yml': [run(30, 'pr-bench.yml', 'failure'), run(8, 'pr-bench.yml', 'success')],
1774+
'ci.yml': [run(40, 'ci.yml', 'failure'), run(7, 'ci.yml', 'success')],
1775+
};
1776+
const jobs = new Map([
1777+
[30, [{ name: 'benchmark report', conclusion: 'failure' }]],
1778+
[8, [{ name: 'benchmark report', conclusion: 'skipped' }]],
1779+
[
1780+
40,
1781+
[
1782+
{ name: 'classify changeset release', conclusion: 'success' },
1783+
{ name: 'test (24)', conclusion: 'failure' },
1784+
],
1785+
],
1786+
[
1787+
7,
1788+
[
1789+
{ name: 'classify changeset release', conclusion: 'skipped' },
1790+
{ name: 'lint', conclusion: 'success' },
1791+
{ name: 'typecheck', conclusion: 'success' },
1792+
],
1793+
],
1794+
]);
1795+
const laterFailure = await selectRuns({
1796+
listed,
1797+
jobs,
1798+
trigger: { ...run(30, 'pr-bench.yml', 'failure'), status: 'completed' },
1799+
});
1800+
assert.deepEqual(laterFailure.outputs, { bench: '30', ci: '40' });
1801+
1802+
const laterDraft = await selectRuns({
1803+
listed: {
1804+
'pr-bench.yml': [run(31, 'pr-bench.yml', 'success'), run(8, 'pr-bench.yml', 'failure')],
1805+
'ci.yml': [run(41, 'ci.yml', 'success'), run(7, 'ci.yml', 'failure')],
1806+
},
1807+
jobs: new Map([
1808+
[31, [{ name: 'benchmark report', conclusion: 'skipped' }]],
1809+
[8, [{ name: 'benchmark report', conclusion: 'failure' }]],
1810+
[
1811+
41,
1812+
[
1813+
{ name: 'classify changeset release', conclusion: 'skipped' },
1814+
{ name: 'lint', conclusion: 'success' },
1815+
],
1816+
],
1817+
[
1818+
7,
1819+
[
1820+
{ name: 'classify changeset release', conclusion: 'success' },
1821+
{ name: 'test (24)', conclusion: 'failure' },
1822+
],
1823+
],
1824+
]),
1825+
trigger: { ...run(31, 'pr-bench.yml', 'success'), status: 'completed' },
1826+
});
1827+
assert.deepEqual(laterDraft.outputs, { bench: '8', ci: '7' });
17471828
});
17481829
});
17491830

0 commit comments

Comments
 (0)