Skip to content

Commit 521ddb2

Browse files
cliffhallclaude
andcommitted
ci: finalize a cancelled dispatched run's status, and pin the path
A dispatched run cancelled for any reason other than supersession left its "(dispatched)" status pending forever (Copilot). The result job now runs under always(): it stays silent only when a newer dispatched run of the same workflow exists on the commit, and otherwise reports the cancellation as an error. scripts/version-pr-ci.test.mjs pins each link of the version-PR CI path, since nothing exercises it before a release. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 0b58b33 commit 521ddb2

3 files changed

Lines changed: 126 additions & 8 deletions

File tree

‎.github/workflows/python.yml‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ jobs:
207207
-f description="Dispatched run in progress" \
208208
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
209209
210-
# `!cancelled()`: a run superseded by a newer dispatch posts nothing, and the
211-
# newer run reports instead. A job skipped by its own `if:` counts as passed.
210+
# `always()`, so a cancelled run cannot leave the status pending. A run that
211+
# was cancelled because a newer dispatch on the same commit superseded it
212+
# posts nothing, and the newer run reports instead; any other cancellation
213+
# is reported as an error. A job skipped by its own `if:` counts as passed.
212214
dispatch-status:
213215
name: Report the dispatched run's result
214-
if: github.event_name == 'workflow_dispatch' && !cancelled()
216+
if: github.event_name == 'workflow_dispatch' && always()
215217
needs:
216218
[
217219
dispatch-status-pending,
@@ -226,16 +228,30 @@ jobs:
226228
runs-on: ubuntu-latest
227229
permissions:
228230
statuses: write
231+
# To look for a newer dispatched run of this workflow on the commit.
232+
actions: read
229233
steps:
230234
- name: Set the commit status to the run's result
231235
env:
232236
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
237+
GH_REPO: ${{ github.repository }}
233238
NEEDS: ${{ toJSON(needs) }}
234239
run: |
235240
if jq -e 'all(.[]; .result == "success" or .result == "skipped")' <<<"$NEEDS" >/dev/null; then
236241
STATE=success DESCRIPTION="Every job passed"
237-
else
242+
elif jq -e 'any(.[]; .result == "failure")' <<<"$NEEDS" >/dev/null; then
238243
STATE=failure DESCRIPTION="A job failed; open the run for details"
244+
else
245+
NEWER=$(gh run list --workflow python.yml --event workflow_dispatch \
246+
--commit "$GITHUB_SHA" --json databaseId \
247+
--jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0
248+
# A failed lookup counts as no newer run: an error status is
249+
# better than one left pending.
250+
if [ "${NEWER:-0}" -gt 0 ]; then
251+
echo "Superseded by a newer dispatched run, which reports instead"
252+
exit 0
253+
fi
254+
STATE=error DESCRIPTION="The run was cancelled; dispatch it again"
239255
fi
240256
gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \
241257
-f state="$STATE" -f context="Python (dispatched)" \

‎.github/workflows/typescript.yml‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,13 @@ jobs:
233233
-f description="Dispatched run in progress" \
234234
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
235235
236-
# `!cancelled()`: a run superseded by a newer dispatch posts nothing, and the
237-
# newer run reports instead. A job skipped by its own `if:` counts as passed.
236+
# `always()`, so a cancelled run cannot leave the status pending. A run that
237+
# was cancelled because a newer dispatch on the same commit superseded it
238+
# posts nothing, and the newer run reports instead; any other cancellation
239+
# is reported as an error. A job skipped by its own `if:` counts as passed.
238240
dispatch-status:
239241
name: Report the dispatched run's result
240-
if: github.event_name == 'workflow_dispatch' && !cancelled()
242+
if: github.event_name == 'workflow_dispatch' && always()
241243
needs:
242244
[
243245
dispatch-status-pending,
@@ -252,16 +254,30 @@ jobs:
252254
runs-on: ubuntu-latest
253255
permissions:
254256
statuses: write
257+
# To look for a newer dispatched run of this workflow on the commit.
258+
actions: read
255259
steps:
256260
- name: Set the commit status to the run's result
257261
env:
258262
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
263+
GH_REPO: ${{ github.repository }}
259264
NEEDS: ${{ toJSON(needs) }}
260265
run: |
261266
if jq -e 'all(.[]; .result == "success" or .result == "skipped")' <<<"$NEEDS" >/dev/null; then
262267
STATE=success DESCRIPTION="Every job passed"
263-
else
268+
elif jq -e 'any(.[]; .result == "failure")' <<<"$NEEDS" >/dev/null; then
264269
STATE=failure DESCRIPTION="A job failed; open the run for details"
270+
else
271+
NEWER=$(gh run list --workflow typescript.yml --event workflow_dispatch \
272+
--commit "$GITHUB_SHA" --json databaseId \
273+
--jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0
274+
# A failed lookup counts as no newer run: an error status is
275+
# better than one left pending.
276+
if [ "${NEWER:-0}" -gt 0 ]; then
277+
echo "Superseded by a newer dispatched run, which reports instead"
278+
exit 0
279+
fi
280+
STATE=error DESCRIPTION="The run was cancelled; dispatch it again"
265281
fi
266282
gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \
267283
-f state="$STATE" -f context="TypeScript (dispatched)" \

‎scripts/version-pr-ci.test.mjs‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Pins the path by which a version PR opened by automation gets CI (#5110).
2+
//
3+
// The Version Packages and Prepare Python Release workflows push their PR
4+
// branches with the workflow token, which starts no workflow, so they
5+
// dispatch `typescript.yml` and `python.yml` on the branch instead. A
6+
// dispatched run's checks are left out of the status rollup a pull request
7+
// reads, so each CI workflow also mirrors a dispatched run onto the commit as
8+
// a status. Both version workflows run only from `v2/main`, so nothing
9+
// exercises this path before a release needs it; if any link here breaks,
10+
// version PRs silently go back to having no visible CI. Each test names the
11+
// link it guards.
12+
13+
import { test } from "node:test";
14+
import assert from "node:assert/strict";
15+
import { readFileSync } from "node:fs";
16+
import path from "node:path";
17+
import { fileURLToPath } from "node:url";
18+
import { parse } from "yaml";
19+
20+
const repoRoot = path.resolve(
21+
path.dirname(fileURLToPath(import.meta.url)),
22+
"..",
23+
);
24+
const read = (rel) => readFileSync(path.join(repoRoot, rel), "utf8");
25+
const workflow = (name) => parse(read(`.github/workflows/${name}`));
26+
27+
const CI = [
28+
{ file: "typescript.yml", context: "TypeScript (dispatched)" },
29+
{ file: "python.yml", context: "Python (dispatched)" },
30+
];
31+
const VERSION_WORKFLOWS = [
32+
{ file: "version-packages.yml", job: "version" },
33+
{ file: "prepare-python-release.yml", job: "prepare" },
34+
];
35+
36+
for (const { file, context } of CI) {
37+
const wf = workflow(file);
38+
const jobs = wf.jobs;
39+
const pending = jobs["dispatch-status-pending"];
40+
const result = jobs["dispatch-status"];
41+
42+
test(`${file} runs on push and can be dispatched`, () => {
43+
assert.ok("push" in wf.on, "push trigger");
44+
assert.ok("workflow_dispatch" in wf.on, "workflow_dispatch trigger");
45+
});
46+
47+
test(`${file} reports a dispatched run as pending, then its result`, () => {
48+
for (const job of [pending, result]) {
49+
assert.ok(job, "both status jobs exist");
50+
assert.match(job.if, /github\.event_name == 'workflow_dispatch'/);
51+
assert.equal(job.permissions.statuses, "write");
52+
const run = job.steps.map((s) => s.run ?? "").join("\n");
53+
assert.match(run, /statuses\/\$GITHUB_SHA/);
54+
assert.ok(run.includes(`context="${context}"`), `context ${context}`);
55+
}
56+
});
57+
58+
test(`${file}'s result job waits on every other job, even after a cancel`, () => {
59+
const others = Object.keys(jobs).filter((id) => id !== "dispatch-status");
60+
assert.deepEqual([...result.needs].sort(), others.sort());
61+
// `always()`: a cancelled run must not leave the status pending.
62+
assert.match(result.if, /always\(\)/);
63+
assert.equal(result.permissions.actions, "read");
64+
const run = result.steps.map((s) => s.run ?? "").join("\n");
65+
assert.ok(run.includes(`gh run list --workflow ${file}`));
66+
assert.match(run, /STATE=error/);
67+
});
68+
}
69+
70+
for (const { file, job } of VERSION_WORKFLOWS) {
71+
const steps = workflow(file).jobs[job];
72+
73+
test(`${file} dispatches both CI workflows on its PR's branch`, () => {
74+
assert.equal(steps.permissions.actions, "write");
75+
const run = steps.steps.map((s) => s.run ?? "").join("\n");
76+
assert.match(run, /gh workflow run "\$workflow" --ref "\$BRANCH"/);
77+
assert.match(run, /for workflow in typescript\.yml python\.yml; do/);
78+
});
79+
}
80+
81+
test("RELEASING.md names the statuses a version PR is merged on", () => {
82+
const releasing = read("RELEASING.md");
83+
for (const { context } of CI)
84+
assert.ok(releasing.includes(`\`${context}\``), context);
85+
assert.doesNotMatch(releasing, /Close and reopen the PR to run CI/);
86+
});

0 commit comments

Comments
 (0)