Skip to content

Commit f18beb7

Browse files
authored
Merge pull request #5179 from modelcontextprotocol/v2/fix/5178-dispatch-status-supersede
fix(ci): a superseded dispatched run posts no status, whatever its jobs did
2 parents 803df28 + 163a5e9 commit f18beb7

3 files changed

Lines changed: 128 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: 102 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,99 @@ 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+
// The job's script needs bash and jq, as on the ubuntu runner. Without
116+
// either (Windows, or a machine with no jq), skip rather than fail the gate
117+
// over a missing test prerequisite.
118+
const hasTool = (cmd) => spawnSync(cmd, ["--version"]).status === 0;
119+
const SCRIPT_SKIP =
120+
process.platform === "win32"
121+
? "runs the job's bash script"
122+
: !hasTool("bash")
123+
? "needs bash"
124+
: !hasTool("jq")
125+
? "needs jq, which the job's script calls"
126+
: false;
127+
128+
const FAILED = { a: { result: "failure" }, b: { result: "cancelled" } };
129+
const PASSED = { a: { result: "success" }, b: { result: "skipped" } };
130+
const CANCELLED = { a: { result: "success" }, b: { result: "cancelled" } };
131+
132+
for (const { file } of CI) {
133+
const script = workflow(file).jobs["dispatch-status"].steps[0].run;
134+
135+
// #5178: the newer-run check guarded only the cancellation branch, so a
136+
// superseded run whose job had already failed posted `failure` over the
137+
// newer run's status. A superseded run now posts nothing at all.
138+
test(
139+
`${file}: a superseded run posts no status, whatever its jobs did`,
140+
{
141+
skip: SCRIPT_SKIP,
142+
},
143+
() => {
144+
for (const needs of [FAILED, PASSED, CANCELLED])
145+
assert.equal(runResultScript(script, { needs, newer: 1 }), null);
146+
},
147+
);
148+
149+
test(
150+
`${file}: the latest run posts success, failure or error`,
151+
{
152+
skip: SCRIPT_SKIP,
153+
},
154+
() => {
155+
assert.equal(
156+
runResultScript(script, { needs: PASSED, newer: 0 }),
157+
"success",
158+
);
159+
assert.equal(
160+
runResultScript(script, { needs: FAILED, newer: 0 }),
161+
"failure",
162+
);
163+
assert.equal(
164+
runResultScript(script, { needs: CANCELLED, newer: 0 }),
165+
"error",
166+
);
167+
},
168+
);
169+
}
170+
70171
for (const { file, job } of VERSION_WORKFLOWS) {
71172
const steps = workflow(file).jobs[job];
72173

0 commit comments

Comments
 (0)