Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .github/scripts/android-managed-device-retry.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import path from "node:path";
import test from "node:test";
import { runInNewContext } from "node:vm";

const workflow = await readFile(new URL("../workflows/android-managed-device-retry.yml", import.meta.url), "utf8");
const validationStep = workflow.split("- name: Validate marker and rerun the failed managed-device job")[1];
assert.ok(validationStep, "trusted marker validation step must exist");
const script = validationStep.split(" script: |\n")[1]
.split("\n").map((line) => line.replace(/^ {12}/, "")).join("\n");

async function recover({ state = "open", sha = "current", repository = "owner/repo", references = [{ number: 7 }], lookupFails = false } = {}) {
const calls = [];
const marker = {
device: "api34",
schemaVersion: 1,
sourceRunId: 42,
runAttempt: 1,
headSha: "current",
retryable: true,
reason: "explicit-infrastructure-failure",
testResultCount: 0,
infrastructureSignals: ["emulator process exited"],
bootPhaseSignals: [],
testExecutionSignals: [],
};
const summary = {
addHeading() { return this; },
addRaw() { return this; },
async write() {},
};
await runInNewContext(`(async () => {\n${script}\n})()`, {
require(name) {
if (name === "node:path") return path;
assert.equal(name, "node:fs");
return {
lstatSync: () => ({ isFile: () => true, isSymbolicLink: () => false, size: 512 }),
readFileSync: () => JSON.stringify(marker),
};
},
process: { env: { MARKER_PATH: "/runner/retry.json", MARKER_ARTIFACT_NAME: "android-managed-device-retry-api34-42-1" } },
context: {
repo: { owner: "owner", repo: "repo" },
payload: { workflow_run: { id: 42, run_attempt: 1, head_sha: "current", head_repository: { full_name: "owner/repo" }, pull_requests: references } },
},
core: { info() {}, warning() {}, summary },
github: {
rest: { pulls: { async get(args) {
calls.push("lookup");
assert.equal(args.pull_number, 7);
if (lookupFails) throw new Error("API unavailable");
return { data: { state, head: { sha, repo: repository === null ? null : { full_name: repository } } } };
} } },
async request(route, args) {
if (route.startsWith("GET ")) {
calls.push("jobs");
assert.equal(args.run_id, 42);
assert.equal(args.attempt_number, 1);
return { data: { jobs: [{ id: 99, name: "Pixel 2 API 34 accessibility smoke", conclusion: "failure" }] } };
}
assert.equal(route, "POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun");
assert.equal(args.job_id, 99);
calls.push("rerun");
},
},
});
return calls;
}

test("recovery checks the live PR after validating the failed job and before rerunning", async () => {
assert.deepEqual(await recover(), ["jobs", "lookup", "rerun"]);
});

for (const [name, options] of [
["newer PR head", { sha: "newer" }],
["closed PR", { state: "closed" }],
["different head repository", { repository: "other/repo" }],
["deleted head repository", { repository: null }],
["failed PR lookup", { lookupFails: true }],
]) {
test(`recovery skips ${name}`, async () => {
assert.deepEqual(await recover(options), ["jobs", "lookup"]);
});
}

for (const [name, references] of [
["missing PR link", []],
["ambiguous PR links", [{ number: 7 }, { number: 8 }]],
["invalid PR number", [{ number: "7" }]],
]) {
test(`recovery skips ${name}`, async () => {
assert.deepEqual(await recover({ references }), ["jobs"]);
});
}
24 changes: 24 additions & 0 deletions .github/workflows/android-managed-device-retry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
permissions:
actions: write
contents: read
pull-requests: read
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
Expand Down Expand Up @@ -173,6 +174,29 @@ jobs:
return;
}

const pullRequests = run.pull_requests ?? [];
if (pullRequests.length !== 1 || !Number.isSafeInteger(pullRequests[0].number)) {
core.warning('Cannot identify one pull request for the completed workflow run.');
return;
}
let pullRequest;
try {
({ data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequests[0].number,
}));
} catch (error) {
core.warning(`Cannot verify the current pull request: ${error.message}`);
return;
}
if (pullRequest.state !== 'open' ||
pullRequest.head.sha !== run.head_sha ||
pullRequest.head.repo?.full_name !== run.head_repository.full_name) {
core.info('The pull request no longer matches this run; skipping stale recovery.');
return;
}

await github.request('POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun', {
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
Loading