Skip to content

Commit 6f1bc7a

Browse files
committed
ci: keep completed bench and CI runs when a newer rerun is in flight
The comment job took only the newest workflow row and dropped it unless that row was already completed, so an in-progress, failed, or cancelled rerun hid this commit's finished artifact and timing.
1 parent 29081eb commit 6f1bc7a

2 files changed

Lines changed: 181 additions & 7 deletions

File tree

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,46 @@ jobs:
4848
script: |
4949
const { owner, repo } = context.repo;
5050
const sha = context.payload.workflow_run.head_sha;
51-
const latest = async (workflow_id) => {
51+
const triggering = context.payload.workflow_run;
52+
const fileOf = (run) => (run.path ?? "").split("/").pop();
53+
// The newest row can be an in-progress or cancelled rerun. Keep the
54+
// completed run for this commit, preferring a success so a failed
55+
// rerun does not hide an artifact or a full timing.
56+
const completedFor = async (workflow_id) => {
5257
const { data } = await github.rest.actions.listWorkflowRuns({
5358
owner,
5459
repo,
5560
workflow_id,
5661
head_sha: sha,
5762
event: "pull_request",
58-
per_page: 1,
63+
status: "completed",
64+
per_page: 100,
5965
});
60-
return data.workflow_runs[0];
66+
const runs = new Map();
67+
for (const run of data.workflow_runs ?? []) {
68+
if (
69+
run.head_sha === sha &&
70+
run.status === "completed" &&
71+
run.conclusion !== "cancelled"
72+
) {
73+
runs.set(run.id, run);
74+
}
75+
}
76+
if (
77+
fileOf(triggering) === workflow_id &&
78+
triggering.head_sha === sha &&
79+
triggering.status === "completed" &&
80+
triggering.conclusion !== "cancelled"
81+
) {
82+
runs.set(triggering.id, triggering);
83+
}
84+
const ordered = [...runs.values()].sort((a, b) => b.id - a.id);
85+
return ordered.find((run) => run.conclusion === "success") ?? ordered[0] ?? null;
6186
};
62-
const bench = await latest("pr-bench.yml");
63-
const ci = await latest("ci.yml");
64-
core.setOutput("bench", bench?.status === "completed" ? String(bench.id) : "");
65-
core.setOutput("ci", ci?.status === "completed" ? String(ci.id) : "");
87+
const bench = await completedFor("pr-bench.yml");
88+
const ci = await completedFor("ci.yml");
89+
core.setOutput("bench", bench ? String(bench.id) : "");
90+
core.setOutput("ci", ci ? String(ci.id) : "");
6691
6792
- name: Download benchmark report
6893
id: download

‎scripts/ci-workflow.test.mjs‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,155 @@ describe('Pull request benchmark report', () => {
16931693
assert.ok(prBenchCommentWorkflow.includes('comment.body?.startsWith(COMMENT_MARKER)'));
16941694
assert.match(packageJson.scripts['ci:workflow:test'], /benchmarks\/ci-timing\.test\.mjs/);
16951695
});
1696+
1697+
test('uses the completed run for this commit when a newer rerun is in progress or cancelled', async () => {
1698+
const sha = 'a'.repeat(40);
1699+
const other = 'b'.repeat(40);
1700+
const queries = [];
1701+
const listed = {
1702+
'pr-bench.yml': [
1703+
{
1704+
id: 30,
1705+
status: 'in_progress',
1706+
conclusion: null,
1707+
head_sha: sha,
1708+
path: '.github/workflows/pr-bench.yml',
1709+
},
1710+
{
1711+
id: 20,
1712+
status: 'completed',
1713+
conclusion: 'cancelled',
1714+
head_sha: sha,
1715+
path: '.github/workflows/pr-bench.yml',
1716+
},
1717+
{
1718+
id: 9,
1719+
status: 'completed',
1720+
conclusion: 'success',
1721+
head_sha: sha,
1722+
path: '.github/workflows/pr-bench.yml',
1723+
},
1724+
],
1725+
'ci.yml': [
1726+
{
1727+
id: 60,
1728+
status: 'completed',
1729+
conclusion: 'failure',
1730+
head_sha: sha,
1731+
path: '.github/workflows/ci.yml',
1732+
},
1733+
{
1734+
id: 40,
1735+
status: 'completed',
1736+
conclusion: 'success',
1737+
head_sha: other,
1738+
path: '.github/workflows/ci.yml',
1739+
},
1740+
{
1741+
id: 12,
1742+
status: 'completed',
1743+
conclusion: 'success',
1744+
head_sha: sha,
1745+
path: '.github/workflows/ci.yml',
1746+
},
1747+
],
1748+
};
1749+
const outputs = new Map();
1750+
const execute = new AsyncFunction(
1751+
'github',
1752+
'context',
1753+
'core',
1754+
stepScript(prBenchCommentWorkflow, 'Find runs for the commit'),
1755+
);
1756+
await execute(
1757+
{
1758+
rest: {
1759+
actions: {
1760+
listWorkflowRuns: async (input) => {
1761+
queries.push(input);
1762+
return { data: { workflow_runs: listed[input.workflow_id] ?? [] } };
1763+
},
1764+
},
1765+
},
1766+
},
1767+
{
1768+
repo: { owner: 'octanejs', repo: 'octane' },
1769+
payload: {
1770+
workflow_run: {
1771+
id: 10,
1772+
path: '.github/workflows/pr-bench.yml',
1773+
head_sha: sha,
1774+
status: 'completed',
1775+
conclusion: 'success',
1776+
event: 'pull_request',
1777+
},
1778+
},
1779+
},
1780+
{
1781+
setOutput(name, value) {
1782+
outputs.set(name, value);
1783+
},
1784+
},
1785+
);
1786+
1787+
assert.equal(outputs.get('bench'), '10');
1788+
assert.equal(outputs.get('ci'), '12');
1789+
assert.ok(queries.length === 2);
1790+
assert.ok(
1791+
queries.every(
1792+
(query) => query.head_sha === sha && query.status === 'completed' && query.per_page === 100,
1793+
),
1794+
);
1795+
1796+
listed['ci.yml'] = [
1797+
{
1798+
id: 70,
1799+
status: 'completed',
1800+
conclusion: 'cancelled',
1801+
head_sha: sha,
1802+
path: '.github/workflows/ci.yml',
1803+
},
1804+
{
1805+
id: 61,
1806+
status: 'completed',
1807+
conclusion: 'failure',
1808+
head_sha: sha,
1809+
path: '.github/workflows/ci.yml',
1810+
},
1811+
];
1812+
outputs.clear();
1813+
await execute(
1814+
{
1815+
rest: {
1816+
actions: {
1817+
listWorkflowRuns: async (input) => ({
1818+
data: { workflow_runs: listed[input.workflow_id] ?? [] },
1819+
}),
1820+
},
1821+
},
1822+
},
1823+
{
1824+
repo: { owner: 'octanejs', repo: 'octane' },
1825+
payload: {
1826+
workflow_run: {
1827+
id: 61,
1828+
path: '.github/workflows/ci.yml',
1829+
head_sha: sha,
1830+
status: 'completed',
1831+
conclusion: 'failure',
1832+
event: 'pull_request',
1833+
},
1834+
},
1835+
},
1836+
{
1837+
setOutput(name, value) {
1838+
outputs.set(name, value);
1839+
},
1840+
},
1841+
);
1842+
assert.equal(outputs.get('bench'), '9');
1843+
assert.equal(outputs.get('ci'), '61');
1844+
});
16961845
});
16971846

16981847
describe('Vercel preview workflow', () => {

0 commit comments

Comments
 (0)