|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | + |
| 3 | +import { type Mock, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { parsePullRequestNumberFromEventJson, resolveGithubPullRequestNumber } from "../src/githubPullRequest.js"; |
| 6 | +import { getEnv } from "../src/utils.js"; |
| 7 | + |
| 8 | +vi.mock("../src/utils.js", () => ({ |
| 9 | + getEnv: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("node:fs", () => ({ |
| 13 | + readFileSync: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | +}); |
| 19 | + |
| 20 | +describe("parsePullRequestNumberFromEventJson", () => { |
| 21 | + it("returns pull request number from event payload", () => { |
| 22 | + expect( |
| 23 | + parsePullRequestNumberFromEventJson(JSON.stringify({ pull_request: { number: 77, title: "Fix things" } })), |
| 24 | + ).toBe("77"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns empty string for invalid JSON", () => { |
| 28 | + expect(parsePullRequestNumberFromEventJson("{not-json")).toBe(""); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns empty string when pull_request is missing", () => { |
| 32 | + expect(parsePullRequestNumberFromEventJson(JSON.stringify({ action: "opened" }))).toBe(""); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns empty string when pull_request.number is missing", () => { |
| 36 | + expect(parsePullRequestNumberFromEventJson(JSON.stringify({ pull_request: { title: "No number" } }))).toBe(""); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("resolveGithubPullRequestNumber", () => { |
| 41 | + it("resolves pull request id from GITHUB_REF_NAME merge suffix", () => { |
| 42 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 43 | + if (key === "GITHUB_REF_NAME") { |
| 44 | + return "42/merge"; |
| 45 | + } |
| 46 | + |
| 47 | + return ""; |
| 48 | + }); |
| 49 | + |
| 50 | + expect(resolveGithubPullRequestNumber()).toBe("42"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("resolves pull request id from GITHUB_REF merge ref", () => { |
| 54 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 55 | + const env: Record<string, string> = { |
| 56 | + GITHUB_REF: "refs/pull/99/merge", |
| 57 | + GITHUB_REF_NAME: "99", |
| 58 | + }; |
| 59 | + |
| 60 | + return env[key] ?? ""; |
| 61 | + }); |
| 62 | + |
| 63 | + expect(resolveGithubPullRequestNumber()).toBe("99"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("resolves pull request id from GITHUB_EVENT_PATH for pull_request_target workflows", () => { |
| 67 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 68 | + const env: Record<string, string> = { |
| 69 | + GITHUB_HEAD_REF: "feature/foo", |
| 70 | + GITHUB_BASE_REF: "main", |
| 71 | + GITHUB_REF: "refs/heads/main", |
| 72 | + GITHUB_REF_NAME: "main", |
| 73 | + GITHUB_EVENT_PATH: "/tmp/event.json", |
| 74 | + }; |
| 75 | + |
| 76 | + return env[key] ?? ""; |
| 77 | + }); |
| 78 | + (readFileSync as Mock).mockReturnValue(JSON.stringify({ pull_request: { number: 77 } })); |
| 79 | + |
| 80 | + expect(resolveGithubPullRequestNumber()).toBe("77"); |
| 81 | + expect(readFileSync).toHaveBeenCalledWith("/tmp/event.json", "utf-8"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not read event file when ref-based detection succeeds", () => { |
| 85 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 86 | + const env: Record<string, string> = { |
| 87 | + GITHUB_HEAD_REF: "feature/foo", |
| 88 | + GITHUB_BASE_REF: "main", |
| 89 | + GITHUB_REF_NAME: "55/merge", |
| 90 | + GITHUB_EVENT_PATH: "/tmp/event.json", |
| 91 | + }; |
| 92 | + |
| 93 | + return env[key] ?? ""; |
| 94 | + }); |
| 95 | + |
| 96 | + expect(resolveGithubPullRequestNumber()).toBe("55"); |
| 97 | + expect(readFileSync).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns empty string when event file is missing", () => { |
| 101 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 102 | + const env: Record<string, string> = { |
| 103 | + GITHUB_HEAD_REF: "feature/foo", |
| 104 | + GITHUB_BASE_REF: "main", |
| 105 | + GITHUB_EVENT_PATH: "/tmp/missing-event.json", |
| 106 | + }; |
| 107 | + |
| 108 | + return env[key] ?? ""; |
| 109 | + }); |
| 110 | + (readFileSync as Mock).mockImplementation(() => { |
| 111 | + throw new Error("ENOENT"); |
| 112 | + }); |
| 113 | + |
| 114 | + expect(resolveGithubPullRequestNumber()).toBe(""); |
| 115 | + }); |
| 116 | + |
| 117 | + it("returns empty string when event JSON has no pull request number", () => { |
| 118 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 119 | + const env: Record<string, string> = { |
| 120 | + GITHUB_HEAD_REF: "feature/foo", |
| 121 | + GITHUB_BASE_REF: "main", |
| 122 | + GITHUB_EVENT_PATH: "/tmp/event.json", |
| 123 | + }; |
| 124 | + |
| 125 | + return env[key] ?? ""; |
| 126 | + }); |
| 127 | + (readFileSync as Mock).mockReturnValue(JSON.stringify({ action: "push" })); |
| 128 | + |
| 129 | + expect(resolveGithubPullRequestNumber()).toBe(""); |
| 130 | + }); |
| 131 | + |
| 132 | + it("returns empty string when only one of head/base refs is set", () => { |
| 133 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 134 | + if (key === "GITHUB_HEAD_REF") { |
| 135 | + return "feature/foo"; |
| 136 | + } |
| 137 | + |
| 138 | + if (key === "GITHUB_EVENT_PATH") { |
| 139 | + return "/tmp/event.json"; |
| 140 | + } |
| 141 | + |
| 142 | + return ""; |
| 143 | + }); |
| 144 | + (readFileSync as Mock).mockReturnValue(JSON.stringify({ pull_request: { number: 77 } })); |
| 145 | + |
| 146 | + expect(resolveGithubPullRequestNumber()).toBe(""); |
| 147 | + expect(readFileSync).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments