Skip to content

Commit d533150

Browse files
cliffhallclaude
andcommitted
fix(ci): a superseded dispatched run posts no status, whatever its jobs did
dispatch-status checked for a newer dispatched run only in its cancellation branch, so a superseded run whose job had already failed posted `failure` over the newer run's status. The check now runs first, in both python.yml and typescript.yml. A behavioral test runs each job's script with a stub gh. Found by Copilot on #5166. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent f1783c3 commit d533150

3 files changed

Lines changed: 115 additions & 23 deletions

File tree

‎.github/workflows/python.yml‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ jobs:
208208
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
209209
210210
# `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
211+
# a newer dispatch on the same commit superseded posts nothing, whatever its
212+
# jobs' results, and the newer run reports instead; any other cancellation
213213
# is reported as an error. A job skipped by its own `if:` counts as passed.
214214
dispatch-status:
215215
name: Report the dispatched run's result
@@ -237,20 +237,22 @@ jobs:
237237
GH_REPO: ${{ github.repository }}
238238
NEEDS: ${{ toJSON(needs) }}
239239
run: |
240+
# A run superseded by a newer dispatch on the same commit posts
241+
# nothing, whatever its jobs' results: the newer run owns the status
242+
# (#5178). A failed lookup counts as no newer run, so this run still
243+
# reports: a stale status is better than one left pending.
244+
NEWER=$(gh run list --workflow python.yml --event workflow_dispatch \
245+
--commit "$GITHUB_SHA" --json databaseId \
246+
--jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0
247+
if [ "${NEWER:-0}" -gt 0 ]; then
248+
echo "Superseded by a newer dispatched run, which reports instead"
249+
exit 0
250+
fi
240251
if jq -e 'all(.[]; .result == "success" or .result == "skipped")' <<<"$NEEDS" >/dev/null; then
241252
STATE=success DESCRIPTION="Every job passed"
242253
elif jq -e 'any(.[]; .result == "failure")' <<<"$NEEDS" >/dev/null; then
243254
STATE=failure DESCRIPTION="A job failed; open the run for details"
244255
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
254256
STATE=error DESCRIPTION="The run was cancelled; dispatch it again"
255257
fi
256258
gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \

‎.github/workflows/typescript.yml‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ jobs:
241241
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
242242
243243
# `always()`, so a cancelled run cannot leave the status pending. A run that
244-
# was cancelled because a newer dispatch on the same commit superseded it
245-
# posts nothing, and the newer run reports instead; any other cancellation
244+
# a newer dispatch on the same commit superseded posts nothing, whatever its
245+
# jobs' results, and the newer run reports instead; any other cancellation
246246
# is reported as an error. A job skipped by its own `if:` counts as passed.
247247
dispatch-status:
248248
name: Report the dispatched run's result
@@ -270,20 +270,22 @@ jobs:
270270
GH_REPO: ${{ github.repository }}
271271
NEEDS: ${{ toJSON(needs) }}
272272
run: |
273+
# A run superseded by a newer dispatch on the same commit posts
274+
# nothing, whatever its jobs' results: the newer run owns the status
275+
# (#5178). A failed lookup counts as no newer run, so this run still
276+
# reports: a stale status is better than one left pending.
277+
NEWER=$(gh run list --workflow typescript.yml --event workflow_dispatch \
278+
--commit "$GITHUB_SHA" --json databaseId \
279+
--jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0
280+
if [ "${NEWER:-0}" -gt 0 ]; then
281+
echo "Superseded by a newer dispatched run, which reports instead"
282+
exit 0
283+
fi
273284
if jq -e 'all(.[]; .result == "success" or .result == "skipped")' <<<"$NEEDS" >/dev/null; then
274285
STATE=success DESCRIPTION="Every job passed"
275286
elif jq -e 'any(.[]; .result == "failure")' <<<"$NEEDS" >/dev/null; then
276287
STATE=failure DESCRIPTION="A job failed; open the run for details"
277288
else
278-
NEWER=$(gh run list --workflow typescript.yml --event workflow_dispatch \
279-
--commit "$GITHUB_SHA" --json databaseId \
280-
--jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0
281-
# A failed lookup counts as no newer run: an error status is
282-
# better than one left pending.
283-
if [ "${NEWER:-0}" -gt 0 ]; then
284-
echo "Superseded by a newer dispatched run, which reports instead"
285-
exit 0
286-
fi
287289
STATE=error DESCRIPTION="The run was cancelled; dispatch it again"
288290
fi
289291
gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212

1313
import { test } from "node:test";
1414
import assert from "node:assert/strict";
15-
import { readFileSync } from "node:fs";
15+
import { spawnSync } from "node:child_process";
16+
import {
17+
chmodSync,
18+
mkdtempSync,
19+
readFileSync,
20+
rmSync,
21+
writeFileSync,
22+
} from "node:fs";
23+
import { tmpdir } from "node:os";
1624
import path from "node:path";
1725
import { fileURLToPath } from "node:url";
1826
import { parse } from "yaml";
@@ -67,6 +75,86 @@ for (const { file, context } of CI) {
6775
});
6876
}
6977

78+
// Run a `dispatch-status` script with a stub `gh` on PATH. The stub answers
79+
// `gh run list` with the number of newer dispatched runs and records every
80+
// other call, so a test sees exactly which status, if any, the run posts.
81+
function runResultScript(script, { needs, newer }) {
82+
const dir = mkdtempSync(path.join(tmpdir(), "dispatch-status-"));
83+
try {
84+
const calls = path.join(dir, "calls");
85+
writeFileSync(calls, "");
86+
writeFileSync(
87+
path.join(dir, "gh"),
88+
[
89+
"#!/usr/bin/env bash",
90+
`if [ "$1 $2" = "run list" ]; then echo ${newer}; exit 0; fi`,
91+
`printf '%s\\n' "$*" >> "${calls}"`,
92+
].join("\n"),
93+
);
94+
chmodSync(path.join(dir, "gh"), 0o755);
95+
const result = spawnSync("bash", ["-e", "-c", script], {
96+
encoding: "utf8",
97+
env: {
98+
...process.env,
99+
PATH: `${dir}${path.delimiter}${process.env.PATH}`,
100+
NEEDS: JSON.stringify(needs),
101+
GITHUB_SHA: "0123abcd",
102+
GITHUB_RUN_ID: "100",
103+
GITHUB_REPOSITORY: "owner/repo",
104+
GITHUB_SERVER_URL: "https://github.com",
105+
},
106+
});
107+
assert.equal(result.status, 0, result.stderr);
108+
const posted = readFileSync(calls, "utf8").trim();
109+
return posted ? posted.match(/state=(\w+)/)?.[1] : null;
110+
} finally {
111+
rmSync(dir, { recursive: true, force: true });
112+
}
113+
}
114+
115+
const FAILED = { a: { result: "failure" }, b: { result: "cancelled" } };
116+
const PASSED = { a: { result: "success" }, b: { result: "skipped" } };
117+
const CANCELLED = { a: { result: "success" }, b: { result: "cancelled" } };
118+
119+
for (const { file } of CI) {
120+
const script = workflow(file).jobs["dispatch-status"].steps[0].run;
121+
122+
// #5178: the newer-run check guarded only the cancellation branch, so a
123+
// superseded run whose job had already failed posted `failure` over the
124+
// newer run's status. A superseded run now posts nothing at all.
125+
test(
126+
`${file}: a superseded run posts no status, whatever its jobs did`,
127+
{
128+
skip: process.platform === "win32" && "runs the job's bash script",
129+
},
130+
() => {
131+
for (const needs of [FAILED, PASSED, CANCELLED])
132+
assert.equal(runResultScript(script, { needs, newer: 1 }), null);
133+
},
134+
);
135+
136+
test(
137+
`${file}: the latest run posts success, failure or error`,
138+
{
139+
skip: process.platform === "win32" && "runs the job's bash script",
140+
},
141+
() => {
142+
assert.equal(
143+
runResultScript(script, { needs: PASSED, newer: 0 }),
144+
"success",
145+
);
146+
assert.equal(
147+
runResultScript(script, { needs: FAILED, newer: 0 }),
148+
"failure",
149+
);
150+
assert.equal(
151+
runResultScript(script, { needs: CANCELLED, newer: 0 }),
152+
"error",
153+
);
154+
},
155+
);
156+
}
157+
70158
for (const { file, job } of VERSION_WORKFLOWS) {
71159
const steps = workflow(file).jobs[job];
72160

0 commit comments

Comments
 (0)