From 162353bbf5c1714bf5980e605860e4e12f82d0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=BC=ED=98=81?= Date: Sun, 25 Oct 2026 15:42:56 +0900 Subject: [PATCH] =?UTF-8?q?fix(ci):=20=ED=98=84=EC=9E=AC=20PR=20=EB=A6=AC?= =?UTF-8?q?=EB=B9=84=EC=A0=84=EB=A7=8C=20=EA=B8=B0=EA=B8=B0=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=EB=A5=BC=20=EC=9E=AC=EC=8B=9C=EB=8F=84=ED=95=9C?= =?UTF-8?q?=EB=8B=A4=20(#2315)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android-managed-device-retry.test.mjs | 95 +++++++++++++++++++ .../android-managed-device-retry.yml | 24 +++++ 2 files changed, 119 insertions(+) create mode 100644 .github/scripts/android-managed-device-retry.test.mjs diff --git a/.github/scripts/android-managed-device-retry.test.mjs b/.github/scripts/android-managed-device-retry.test.mjs new file mode 100644 index 000000000..f001d7d48 --- /dev/null +++ b/.github/scripts/android-managed-device-retry.test.mjs @@ -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"]); + }); +} diff --git a/.github/workflows/android-managed-device-retry.yml b/.github/workflows/android-managed-device-retry.yml index f0acde088..be9cb0192 100644 --- a/.github/workflows/android-managed-device-retry.yml +++ b/.github/workflows/android-managed-device-retry.yml @@ -27,6 +27,7 @@ jobs: permissions: actions: write contents: read + pull-requests: read runs-on: ubuntu-latest timeout-minutes: 5 steps: @@ -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,