|
| 1 | +import { CiType, type CiDescriptor, GitProvider } from "@allurereport/core-api"; |
| 2 | +import { type Mock, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { resolveGithubGitHints } from "../src/gitHints/github.js"; |
| 5 | +import { getEnv } from "../src/utils.js"; |
| 6 | + |
| 7 | +vi.mock("../src/utils.js", () => ({ |
| 8 | + getEnv: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +const githubCi = { |
| 12 | + type: CiType.Github, |
| 13 | + jobRunBranch: "feature/foo", |
| 14 | + pullRequestUrl: "https://github.com/myorg/myrepo/pull/42", |
| 15 | + pullRequestName: "Pull request #42", |
| 16 | +} as CiDescriptor; |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("resolveGithubGitHints", () => { |
| 23 | + it("maps repository, branches, and pull request for PR workflow", () => { |
| 24 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 25 | + const env: Record<string, string> = { |
| 26 | + GITHUB_REPOSITORY: "myorg/myrepo", |
| 27 | + GITHUB_SERVER_URL: "https://github.com", |
| 28 | + GITHUB_HEAD_REF: "feature/foo", |
| 29 | + GITHUB_BASE_REF: "main", |
| 30 | + GITHUB_REF_NAME: "42/merge", |
| 31 | + GITHUB_REF: "refs/heads/feature/foo", |
| 32 | + }; |
| 33 | + |
| 34 | + return env[key] ?? ""; |
| 35 | + }); |
| 36 | + |
| 37 | + expect(resolveGithubGitHints(githubCi)).toEqual({ |
| 38 | + provider: GitProvider.Github, |
| 39 | + repository: { |
| 40 | + slug: "myorg/myrepo", |
| 41 | + url: "https://github.com/myorg/myrepo", |
| 42 | + }, |
| 43 | + sourceBranch: "feature/foo", |
| 44 | + targetBranch: "main", |
| 45 | + pullRequest: { |
| 46 | + id: "42", |
| 47 | + url: "https://github.com/myorg/myrepo/pull/42", |
| 48 | + title: "Pull request #42", |
| 49 | + }, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("uses GITHUB_REF branch when HEAD_REF is empty", () => { |
| 54 | + (getEnv as Mock).mockImplementation((key: string) => { |
| 55 | + if (key === "GITHUB_REPOSITORY") { |
| 56 | + return "myorg/myrepo"; |
| 57 | + } |
| 58 | + |
| 59 | + if (key === "GITHUB_REF") { |
| 60 | + return "refs/heads/main"; |
| 61 | + } |
| 62 | + |
| 63 | + return ""; |
| 64 | + }); |
| 65 | + |
| 66 | + expect(resolveGithubGitHints(githubCi).sourceBranch).toBe("main"); |
| 67 | + expect(resolveGithubGitHints(githubCi).pullRequest).toBeUndefined(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments