Skip to content

Commit

Permalink
fix: Pass PR number to the getPullChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaerzen authored Aug 22, 2024
1 parent 534197e commit ac05960
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
82 changes: 82 additions & 0 deletions src/inputs/getPullChanges.test.ts
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
16 changes: 10 additions & 6 deletions src/inputs/getPullChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import { FileCoverageMode } from "./FileCoverageMode";

type Octokit = ReturnType<typeof github.getOctokit>;

export async function getPullChanges(
fileCoverageMode: FileCoverageMode,
): Promise<string[]> {
interface Params {
fileCoverageMode: FileCoverageMode;
prNumber?: number;
}

export async function getPullChanges({
fileCoverageMode,
prNumber = github.context.payload.pull_request?.number,
}: Params): Promise<string[]> {
// 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[] = [];
Expand Down

0 comments on commit ac05960

Please sign in to comment.