|
| 1 | +// @ts-check |
| 2 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 3 | +import { main, parseDispatchRef } from "./resolve_host_repo.cjs"; |
| 4 | + |
| 5 | +describe("parseDispatchRef", () => { |
| 6 | + it("parses refs/heads/main from a standard workflow_ref", () => { |
| 7 | + expect(parseDispatchRef("owner/repo/.github/workflows/file.yml@refs/heads/main")).toBe("refs/heads/main"); |
| 8 | + }); |
| 9 | + |
| 10 | + it("parses refs/tags/v1.2.3 from a tag-triggered workflow_ref", () => { |
| 11 | + expect(parseDispatchRef("owner/repo/.github/workflows/file.yml@refs/tags/v1.2.3")).toBe("refs/tags/v1.2.3"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("parses refs/heads/feature/my-branch from a nested branch name", () => { |
| 15 | + expect(parseDispatchRef("org/repo/.github/workflows/ci.yml@refs/heads/feature/my-branch")).toBe("refs/heads/feature/my-branch"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns empty string when JOB_WORKFLOW_REF is empty", () => { |
| 19 | + expect(parseDispatchRef("")).toBe(""); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns empty string when JOB_WORKFLOW_REF has no '@' separator", () => { |
| 23 | + expect(parseDispatchRef("owner/repo/.github/workflows/file.yml")).toBe(""); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns empty string when JOB_WORKFLOW_REF ends with a 40-hex commit SHA", () => { |
| 27 | + // workflow_call can reference a workflow by commit SHA, but createWorkflowDispatch |
| 28 | + // rejects SHAs — parseDispatchRef must return "" to prevent dispatch failures. |
| 29 | + const sha = "abc123def456abc123def456abc123def456abc1"; |
| 30 | + expect(parseDispatchRef(`owner/repo/.github/workflows/file.yml@${sha}`)).toBe(""); |
| 31 | + }); |
| 32 | + |
| 33 | + it("uses lastIndexOf so an '@' in the workflow path does not mis-parse the ref", () => { |
| 34 | + // Pathological but valid: if the path segment contained '@', lastIndexOf ensures |
| 35 | + // we capture the ref portion after the final '@'. |
| 36 | + expect(parseDispatchRef("owner/repo@org/.github/workflows/file.yml@refs/heads/main")).toBe("refs/heads/main"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("does not return a SHA-like value for a tag ref", () => { |
| 40 | + const result = parseDispatchRef("owner/repo/.github/workflows/file.yml@refs/tags/v2.0.0"); |
| 41 | + expect(result).toBe("refs/tags/v2.0.0"); |
| 42 | + // Must not look like a SHA (40 hex chars) |
| 43 | + expect(result).not.toMatch(/^[0-9a-f]{40}$/); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("resolve_host_repo main", () => { |
| 48 | + let outputs; |
| 49 | + let warnings; |
| 50 | + let infos; |
| 51 | + let originalEnv; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + outputs = {}; |
| 55 | + warnings = []; |
| 56 | + infos = []; |
| 57 | + |
| 58 | + global.core = { |
| 59 | + info: vi.fn(msg => infos.push(msg)), |
| 60 | + warning: vi.fn(msg => warnings.push(msg)), |
| 61 | + error: vi.fn(), |
| 62 | + setOutput: vi.fn((key, value) => { |
| 63 | + outputs[key] = value; |
| 64 | + }), |
| 65 | + }; |
| 66 | + |
| 67 | + originalEnv = { |
| 68 | + JOB_WORKFLOW_REPOSITORY: process.env.JOB_WORKFLOW_REPOSITORY, |
| 69 | + JOB_WORKFLOW_SHA: process.env.JOB_WORKFLOW_SHA, |
| 70 | + JOB_WORKFLOW_REF: process.env.JOB_WORKFLOW_REF, |
| 71 | + JOB_WORKFLOW_FILE_PATH: process.env.JOB_WORKFLOW_FILE_PATH, |
| 72 | + GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + for (const [key, value] of Object.entries(originalEnv)) { |
| 78 | + if (value === undefined) { |
| 79 | + delete process.env[key]; |
| 80 | + } else { |
| 81 | + process.env[key] = value; |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it("emits target_checkout_ref as the commit SHA from JOB_WORKFLOW_SHA", async () => { |
| 87 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 88 | + process.env.JOB_WORKFLOW_SHA = "abc123def456abc123def456abc123def456abc1"; |
| 89 | + process.env.JOB_WORKFLOW_REF = "owner/platform-repo/.github/workflows/orchestrator.yml@refs/heads/main"; |
| 90 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 91 | + |
| 92 | + await main(); |
| 93 | + |
| 94 | + expect(outputs["target_checkout_ref"]).toBe("abc123def456abc123def456abc123def456abc1"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("emits target_ref as the dispatch-compatible branch ref from JOB_WORKFLOW_REF", async () => { |
| 98 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 99 | + process.env.JOB_WORKFLOW_SHA = "abc123def456abc123def456abc123def456abc1"; |
| 100 | + process.env.JOB_WORKFLOW_REF = "owner/platform-repo/.github/workflows/orchestrator.yml@refs/heads/main"; |
| 101 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 102 | + |
| 103 | + await main(); |
| 104 | + |
| 105 | + expect(outputs["target_ref"]).toBe("refs/heads/main"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("emits a tag as target_ref when workflow_ref contains a tag", async () => { |
| 109 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 110 | + process.env.JOB_WORKFLOW_SHA = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; |
| 111 | + process.env.JOB_WORKFLOW_REF = "owner/platform-repo/.github/workflows/release.yml@refs/tags/v1.2.3"; |
| 112 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 113 | + |
| 114 | + await main(); |
| 115 | + |
| 116 | + expect(outputs["target_ref"]).toBe("refs/tags/v1.2.3"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("target_ref is never a SHA when JOB_WORKFLOW_REF is provided", async () => { |
| 120 | + const sha = "abc123def456abc123def456abc123def456abc1"; |
| 121 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 122 | + process.env.JOB_WORKFLOW_SHA = sha; |
| 123 | + process.env.JOB_WORKFLOW_REF = "owner/platform-repo/.github/workflows/file.yml@refs/heads/main"; |
| 124 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 125 | + |
| 126 | + await main(); |
| 127 | + |
| 128 | + expect(outputs["target_ref"]).not.toBe(sha); |
| 129 | + expect(outputs["target_ref"]).toBe("refs/heads/main"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("emits target_ref as empty string and warns when JOB_WORKFLOW_REF is missing", async () => { |
| 133 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 134 | + process.env.JOB_WORKFLOW_SHA = "abc123def456abc123def456abc123def456abc1"; |
| 135 | + delete process.env.JOB_WORKFLOW_REF; |
| 136 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 137 | + |
| 138 | + await main(); |
| 139 | + |
| 140 | + expect(outputs["target_ref"]).toBe(""); |
| 141 | + expect(warnings.length).toBeGreaterThan(0); |
| 142 | + expect(warnings[0]).toContain("Could not parse"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("emits target_ref as empty string and warns when JOB_WORKFLOW_REF has no '@' separator", async () => { |
| 146 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 147 | + process.env.JOB_WORKFLOW_SHA = "abc123def456abc123def456abc123def456abc1"; |
| 148 | + process.env.JOB_WORKFLOW_REF = "owner/platform-repo/.github/workflows/file.yml"; |
| 149 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 150 | + |
| 151 | + await main(); |
| 152 | + |
| 153 | + expect(outputs["target_ref"]).toBe(""); |
| 154 | + expect(warnings.length).toBeGreaterThan(0); |
| 155 | + }); |
| 156 | + |
| 157 | + it("emits target_ref as empty string and warns when JOB_WORKFLOW_REF ends with a commit SHA", async () => { |
| 158 | + // workflow_call can pin to a SHA; createWorkflowDispatch cannot accept a SHA as ref. |
| 159 | + const sha = "abc123def456abc123def456abc123def456abc1"; |
| 160 | + process.env.JOB_WORKFLOW_REPOSITORY = "owner/platform-repo"; |
| 161 | + process.env.JOB_WORKFLOW_SHA = sha; |
| 162 | + process.env.JOB_WORKFLOW_REF = `owner/platform-repo/.github/workflows/file.yml@${sha}`; |
| 163 | + process.env.GITHUB_REPOSITORY = "owner/platform-repo"; |
| 164 | + |
| 165 | + await main(); |
| 166 | + |
| 167 | + expect(outputs["target_ref"]).toBe(""); |
| 168 | + expect(warnings.length).toBeGreaterThan(0); |
| 169 | + expect(outputs["target_checkout_ref"]).toBe(sha); |
| 170 | + }); |
| 171 | + |
| 172 | + it("emits target_repo and target_repo_name correctly", async () => { |
| 173 | + process.env.JOB_WORKFLOW_REPOSITORY = "my-org/my-platform"; |
| 174 | + process.env.JOB_WORKFLOW_SHA = "abc123def456abc123def456abc123def456abc1"; |
| 175 | + process.env.JOB_WORKFLOW_REF = "my-org/my-platform/.github/workflows/file.yml@refs/heads/main"; |
| 176 | + process.env.GITHUB_REPOSITORY = "my-org/my-caller"; |
| 177 | + |
| 178 | + await main(); |
| 179 | + |
| 180 | + expect(outputs["target_repo"]).toBe("my-org/my-platform"); |
| 181 | + expect(outputs["target_repo_name"]).toBe("my-platform"); |
| 182 | + }); |
| 183 | +}); |
0 commit comments