Skip to content

Commit 5eadccc

Browse files
luanvdwclaude
andcommitted
fix: keep a failure cause another job already recorded on early failure
Jobs of one workflow run share a build, and the failing step and error are not write-once on the platform, so the early-failure report now reads the build first and lets failurePatch keep an existing cause, as the normal failure path does. Adds an entry-point test that runs main.mjs into an early failure against a local API stub. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 696c0f1 commit 5eadccc

3 files changed

Lines changed: 115 additions & 9 deletions

File tree

‎report.mjs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ export function makeReporter({ apiUrl, token, fetchImpl = fetch }) {
117117
* holds a queued build for this run (the workflow_run webhook creates it),
118118
* and the create converges on it through the run identity, so the cause
119119
* lands on that build instead of the platform's generic "workflow run
120-
* finished without reporting to the platform". Best-effort: never throws,
121-
* and a run without a credential reports nothing.
120+
* finished without reporting to the platform". Jobs of one run share that
121+
* build, so a cause another job already recorded is kept (failurePatch).
122+
* Best-effort: never throws, and a run without a credential reports nothing.
122123
* Returns the build id, or null when nothing was reported.
123124
*/
124125
export async function reportEarlyFailure({
@@ -143,8 +144,9 @@ export async function reportEarlyFailure({
143144
const reporter = makeReporter({ apiUrl, token: credential.token, fetchImpl });
144145
const buildId = await guardReport(() => reporter.create(createPayload), "create", log);
145146
if (!buildId) return null;
147+
const existing = await guardReport(() => reporter.get(buildId), "read before failed", log);
146148
const result = await guardReport(
147-
() => reporter.update(buildId, failurePatch(null, failingStep, errorText)),
149+
() => reporter.update(buildId, failurePatch(existing, failingStep, errorText)),
148150
"failed",
149151
log,
150152
);

‎tests/report.test.mjs‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,13 @@ const RUN_PAYLOAD = {
304304
externalLogUrl: "https://github.com/org/repo/actions/runs/11111",
305305
};
306306

307-
function recordingFetch(createStatus = 201) {
307+
function recordingFetch(createStatus = 201, existingBuild = {}) {
308308
const calls = [];
309309
const fetchImpl = async (url, init) => {
310-
calls.push({ url, method: init.method, body: init.body ? JSON.parse(init.body) : undefined });
311-
if (init.method === "POST") return jsonResponse(createStatus, { data: { id: BUILD_ID } });
310+
const method = init.method ?? "GET";
311+
calls.push({ url, method, body: init.body ? JSON.parse(init.body) : undefined });
312+
if (method === "POST") return jsonResponse(createStatus, { data: { id: BUILD_ID } });
313+
if (method === "GET") return jsonResponse(200, { data: existingBuild });
312314
return jsonResponse(200, { data: {} });
313315
};
314316
return { calls, fetchImpl };
@@ -328,16 +330,67 @@ test("reportEarlyFailure creates the run's build and records the cause on it", a
328330
assert.equal(buildId, BUILD_ID);
329331
assert.deepEqual(
330332
calls.map(({ url, method }) => `${method} ${url}`),
331-
[`POST ${API_URL}/v1/builds`, `PATCH ${API_URL}/v1/builds/${BUILD_ID}`],
333+
[
334+
`POST ${API_URL}/v1/builds`,
335+
`GET ${API_URL}/v1/builds/${BUILD_ID}`,
336+
`PATCH ${API_URL}/v1/builds/${BUILD_ID}`,
337+
],
332338
);
333339
assert.deepEqual(calls[0].body.runIdentity, RUN_PAYLOAD.runIdentity);
334-
assert.deepEqual(calls[1].body, {
340+
assert.deepEqual(calls[2].body, {
335341
state: "failed",
336342
failingStep: "config",
337343
errorMessage: "bun not found on PATH",
338344
});
339345
});
340346

347+
test("reportEarlyFailure keeps a cause another job of the run already recorded", async () => {
348+
const { calls, fetchImpl } = recordingFetch(201, {
349+
failingStep: "install",
350+
errorMessage: "install-auto-detect-failed",
351+
});
352+
const buildId = await reportEarlyFailure({
353+
env: { PRISMA_SERVICE_TOKEN: TOKEN },
354+
apiUrl: API_URL,
355+
createPayload: RUN_PAYLOAD,
356+
failingStep: "config",
357+
errorText: "bun not found on PATH",
358+
log: () => {},
359+
fetchImpl,
360+
});
361+
assert.equal(buildId, BUILD_ID);
362+
assert.deepEqual(calls.at(-1), {
363+
url: `${API_URL}/v1/builds/${BUILD_ID}`,
364+
method: "PATCH",
365+
body: { state: "failed" },
366+
});
367+
});
368+
369+
test("reportEarlyFailure still records the cause when the read before it fails", async () => {
370+
const calls = [];
371+
const fetchImpl = async (url, init) => {
372+
const method = init.method ?? "GET";
373+
calls.push({ method, body: init.body ? JSON.parse(init.body) : undefined });
374+
if (method === "POST") return jsonResponse(201, { data: { id: BUILD_ID } });
375+
if (method === "GET") return jsonResponse(404, {});
376+
return jsonResponse(200, { data: {} });
377+
};
378+
const buildId = await reportEarlyFailure({
379+
env: { PRISMA_SERVICE_TOKEN: TOKEN },
380+
apiUrl: API_URL,
381+
createPayload: RUN_PAYLOAD,
382+
failingStep: "config",
383+
errorText: "bun not found on PATH",
384+
log: () => {},
385+
fetchImpl,
386+
});
387+
assert.equal(buildId, BUILD_ID);
388+
assert.deepEqual(calls.at(-1), {
389+
method: "PATCH",
390+
body: { state: "failed", failingStep: "config", errorMessage: "bun not found on PATH" },
391+
});
392+
});
393+
341394
test("reportEarlyFailure sends nothing when the run has no credential", async () => {
342395
const { calls, fetchImpl } = recordingFetch();
343396
const buildId = await reportEarlyFailure({

‎tests/summary.test.mjs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from "node:assert/strict";
2-
import { spawnSync } from "node:child_process";
2+
import { spawn, spawnSync } from "node:child_process";
3+
import { createServer } from "node:http";
34
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
45
import { tmpdir } from "node:os";
56
import { join } from "node:path";
@@ -259,3 +260,53 @@ test("main.mjs keeps its outcome and exit code when the summary cannot be writte
259260
assert.match(outputs, /^outcome=failed$/m);
260261
assert.match(result.stdout, /::warning::could not write the job summary/);
261262
});
263+
264+
test("main.mjs reports an early failure to the run's build when it has a credential", async () => {
265+
const requests = [];
266+
const server = createServer((req, res) => {
267+
let raw = "";
268+
req.on("data", (chunk) => (raw += chunk));
269+
req.on("end", () => {
270+
requests.push({ method: req.method, url: req.url, body: raw ? JSON.parse(raw) : undefined });
271+
res.writeHead(req.method === "POST" ? 201 : 200, { "content-type": "application/json" });
272+
res.end(JSON.stringify({ data: req.method === "POST" ? { id: "bld_early" } : {} }));
273+
});
274+
});
275+
await new Promise((done) => server.listen(0, "127.0.0.1", done));
276+
const dir = mkdtempSync(join(tmpdir(), "summary-main-"));
277+
const output = join(dir, "output");
278+
try {
279+
// spawn, not spawnSync: the stub API answers on this process's event loop.
280+
const status = await new Promise((done, reject) => {
281+
const child = spawn(process.execPath, [new URL("../main.mjs", import.meta.url).pathname], {
282+
env: {
283+
PATH: process.env.PATH,
284+
INPUT_MODE: "bogus",
285+
"INPUT_API-URL": `http://127.0.0.1:${server.address().port}`,
286+
PRISMA_SERVICE_TOKEN: "tok-test",
287+
GITHUB_REF_NAME: "main",
288+
GITHUB_SHA: "095b237a1c0ffee",
289+
GITHUB_RUN_ID: "11111",
290+
GITHUB_OUTPUT: output,
291+
GITHUB_STATE: join(dir, "state"),
292+
GITHUB_STEP_SUMMARY: join(dir, "summary.md"),
293+
RUNNER_TEMP: dir,
294+
},
295+
stdio: "ignore",
296+
});
297+
child.on("error", reject);
298+
child.on("exit", done);
299+
});
300+
assert.equal(status, 1);
301+
assert.deepEqual(
302+
requests.map(({ method, url }) => `${method} ${url}`),
303+
["POST /v1/builds", "GET /v1/builds/bld_early", "PATCH /v1/builds/bld_early"],
304+
);
305+
assert.equal(requests[2].body.failingStep, "config");
306+
const outputs = readFileSync(output, "utf8");
307+
assert.match(outputs, /^build-id=bld_early$/m);
308+
assert.match(outputs, /^outcome=failed$/m);
309+
} finally {
310+
server.close();
311+
}
312+
});

0 commit comments

Comments
 (0)