diff --git a/actions/setup/js/workflow_metadata_helpers.cjs b/actions/setup/js/workflow_metadata_helpers.cjs index 9942fdd9199..af519e90fd3 100644 --- a/actions/setup/js/workflow_metadata_helpers.cjs +++ b/actions/setup/js/workflow_metadata_helpers.cjs @@ -13,15 +13,10 @@ function getWorkflowMetadata(owner, repo) { const workflowName = process.env.GH_AW_WORKFLOW_NAME || "Workflow"; const workflowId = process.env.GH_AW_WORKFLOW_ID || ""; - const runId = context.runId || 0; + const runId = context.runId ?? 0; const runUrl = buildWorkflowRunUrl({ runId, serverUrl: context.serverUrl }, { owner, repo }); - return { - workflowName, - workflowId, - runId, - runUrl, - }; + return { workflowName, workflowId, runId, runUrl }; } /** @@ -38,14 +33,14 @@ function getWorkflowMetadata(owner, repo) { */ function buildWorkflowRunUrl(ctx, workflowRepo) { const server = ctx.serverUrl || process.env.GITHUB_SERVER_URL || "https://github.com"; - let { owner, repo } = workflowRepo || {}; + let { owner = "", repo = "" } = workflowRepo ?? {}; if (!owner || !repo) { // When context is spread (e.g. `{...context}`), prototype getters like context.repo // are not included. Fall back to GITHUB_REPOSITORY for the workflow repo. - const parts = (process.env.GITHUB_REPOSITORY || "").split("/"); - if (parts.length === 2 && parts[0] && parts[1]) { - owner = owner || parts[0]; - repo = repo || parts[1]; + const [envOwner = "", envRepo = ""] = (process.env.GITHUB_REPOSITORY || "").split("/"); + if (envOwner && envRepo) { + owner = owner || envOwner; + repo = repo || envRepo; } } return `${server}/${owner}/${repo}/actions/runs/${ctx.runId}`; diff --git a/actions/setup/js/workflow_metadata_helpers.test.cjs b/actions/setup/js/workflow_metadata_helpers.test.cjs index 00240a7dbf1..bfd8ded72e9 100644 --- a/actions/setup/js/workflow_metadata_helpers.test.cjs +++ b/actions/setup/js/workflow_metadata_helpers.test.cjs @@ -115,6 +115,15 @@ describe("getWorkflowMetadata", () => { expect(metadata.runUrl).toBe("https://github.com/my-owner/my-repo/actions/runs/7"); }); + + it("should use 0 as runId when context.runId is explicitly 0", () => { + global.context = { runId: 0, serverUrl: "https://github.com" }; + + const metadata = getWorkflowMetadata("owner", "repo"); + + expect(metadata.runId).toBe(0); + expect(metadata.runUrl).toBe("https://github.com/owner/repo/actions/runs/0"); + }); }); describe("buildWorkflowRunUrl", () => { @@ -200,4 +209,56 @@ describe("buildWorkflowRunUrl", () => { } } }); + + it("should use explicit owner but fall back GITHUB_REPOSITORY for missing repo", () => { + const originalEnv = process.env.GITHUB_REPOSITORY; + process.env.GITHUB_REPOSITORY = "env-owner/env-repo"; + try { + const url = buildWorkflowRunUrl({ serverUrl: "https://github.com", runId: 5 }, { owner: "explicit-owner", repo: "" }); + expect(url).toBe("https://github.com/explicit-owner/env-repo/actions/runs/5"); + } finally { + if (originalEnv === undefined) { + delete process.env.GITHUB_REPOSITORY; + } else { + process.env.GITHUB_REPOSITORY = originalEnv; + } + } + }); + + it("should use explicit repo but fall back GITHUB_REPOSITORY for missing owner", () => { + const originalEnv = process.env.GITHUB_REPOSITORY; + process.env.GITHUB_REPOSITORY = "env-owner/env-repo"; + try { + const url = buildWorkflowRunUrl({ serverUrl: "https://github.com", runId: 6 }, { owner: "", repo: "explicit-repo" }); + expect(url).toBe("https://github.com/env-owner/explicit-repo/actions/runs/6"); + } finally { + if (originalEnv === undefined) { + delete process.env.GITHUB_REPOSITORY; + } else { + process.env.GITHUB_REPOSITORY = originalEnv; + } + } + }); + + it("should not fall back to GITHUB_REPOSITORY when both owner and repo are set", () => { + const originalEnv = process.env.GITHUB_REPOSITORY; + process.env.GITHUB_REPOSITORY = "env-owner/env-repo"; + try { + const url = buildWorkflowRunUrl({ serverUrl: "https://github.com", runId: 8 }, { owner: "wf-owner", repo: "wf-repo" }); + expect(url).toBe("https://github.com/wf-owner/wf-repo/actions/runs/8"); + expect(url).not.toContain("env-owner"); + expect(url).not.toContain("env-repo"); + } finally { + if (originalEnv === undefined) { + delete process.env.GITHUB_REPOSITORY; + } else { + process.env.GITHUB_REPOSITORY = originalEnv; + } + } + }); + + it("should use string runId in URL", () => { + const url = buildWorkflowRunUrl({ serverUrl: "https://github.com", runId: "run-abc123" }, { owner: "owner", repo: "repo" }); + expect(url).toBe("https://github.com/owner/repo/actions/runs/run-abc123"); + }); });