From ac05960a9df46da99c3973b87a262348bf348686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Er=C5=BEen?= Date: Thu, 22 Aug 2024 14:15:44 +0200 Subject: [PATCH] fix: Pass PR number to the getPullChanges --- src/index.ts | 5 +- src/inputs/getPullChanges.test.ts | 82 +++++++++++++++++++++++++++++++ src/inputs/getPullChanges.ts | 16 +++--- 3 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/inputs/getPullChanges.test.ts diff --git a/src/index.ts b/src/index.ts index 855685a..efe62a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,7 +43,10 @@ const run = async () => { .addRaw(tableData); if (fileCoverageMode !== FileCoverageMode.None) { - const pullChanges = await getPullChanges(fileCoverageMode); + const pullChanges = await getPullChanges({ + fileCoverageMode, + prNumber: processedPrNumber, + }); const jsonFinal = await parseVitestJsonFinal(jsonFinalPath); const fileTable = generateFileCoverageHtml({ jsonSummary, diff --git a/src/inputs/getPullChanges.test.ts b/src/inputs/getPullChanges.test.ts new file mode 100644 index 0000000..ad3a223 --- /dev/null +++ b/src/inputs/getPullChanges.test.ts @@ -0,0 +1,82 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { FileCoverageMode } from "./FileCoverageMode"; +import { getPullChanges } from "./getPullChanges"; + +const mockGetInput = vi.hoisted(() => vi.fn()); +vi.mock("@actions/core", () => ({ + getInput: mockGetInput, + endGroup: vi.fn(), + startGroup: vi.fn(), + info: vi.fn(), + debug: vi.fn(), +})); + +const mockContext = vi.hoisted(() => ({ + repo: { + owner: "owner", + repo: "repo", + }, + payload: {}, +})); +const mockGetOctokit = vi.hoisted(() => vi.fn()); +vi.mock("@actions/github", () => ({ + context: mockContext, + getOctokit: mockGetOctokit, +})); + +describe("getPullChanges", () => { + beforeEach(() => { + vi.clearAllMocks(); + mockGetInput.mockReturnValue("fake-token"); + const mockOctokit = { + paginate: { + iterator: vi.fn().mockReturnValue([ + { + data: [ + { status: "added", filename: "file1.ts" }, + { status: "modified", filename: "file2.ts" }, + ], + }, + ]), + }, + rest: { + pulls: { + listFiles: vi.fn(), + }, + }, + }; + mockGetOctokit.mockReturnValue(mockOctokit); + }); + + it("should return an empty array if fileCoverageMode is None", async () => { + const result = await getPullChanges({ + fileCoverageMode: FileCoverageMode.None, + }); + expect(result).toEqual([]); + }); + + it("should return an empty array if prNumber is not provided and context payload has no pull request number", async () => { + mockContext.payload = {}; + const result = await getPullChanges({ + fileCoverageMode: FileCoverageMode.All, + }); + expect(result).toEqual([]); + }); + + it("should fetch and return changed files when prNumber is provided but not in the context", async () => { + mockContext.payload = {}; + const result = await getPullChanges({ + fileCoverageMode: FileCoverageMode.All, + prNumber: 123, + }); + expect(result).toEqual(["file1.ts", "file2.ts"]); + }); + + it("should fetch and return changed files when prNumber is in the context but not provided", async () => { + mockContext.payload = { pull_request: { number: 123 } }; + const result = await getPullChanges({ + fileCoverageMode: FileCoverageMode.All, + }); + expect(result).toEqual(["file1.ts", "file2.ts"]); + }); +}); diff --git a/src/inputs/getPullChanges.ts b/src/inputs/getPullChanges.ts index 8e3b701..09f25d2 100644 --- a/src/inputs/getPullChanges.ts +++ b/src/inputs/getPullChanges.ts @@ -5,21 +5,25 @@ import { FileCoverageMode } from "./FileCoverageMode"; type Octokit = ReturnType; -export async function getPullChanges( - fileCoverageMode: FileCoverageMode, -): Promise { +interface Params { + fileCoverageMode: FileCoverageMode; + prNumber?: number; +} + +export async function getPullChanges({ + fileCoverageMode, + prNumber = github.context.payload.pull_request?.number, +}: Params): Promise { // Skip Changes collection if we don't need it if (fileCoverageMode === FileCoverageMode.None) { return []; } - // Skip Changes collection if we can't do it - if (!github.context.payload?.pull_request) { + if (!prNumber) { return []; } const gitHubToken = core.getInput("github-token").trim(); - const prNumber = github.context.payload.pull_request.number; try { const octokit: Octokit = github.getOctokit(gitHubToken); const paths: string[] = [];