diff --git a/dist/main.js b/dist/main.js index e20b9568..030a9ba9 100644 --- a/dist/main.js +++ b/dist/main.js @@ -22798,7 +22798,13 @@ function createHref(options, file) { const relative = file.file.replace(options.prefix, ""); const parts = relative.split("/"); const filename = parts[parts.length - 1]; - const url = path.join(options.repository, 'blob', options.commit, options.workingDir || './', relative); + const url = path.join( + options.repository, + "blob", + options.commit || options.defaultBranch, + options.workingDir || "./", + relative, + ); return { href: `https://github.com/${url}`, filename @@ -23047,9 +23053,13 @@ async function getChangedFiles(githubClient, options, context) { ); } + // Normalize the working directory path from './' to '' or './src' to 'src' + const workingDir = options.workingDir.replace(/^.\//, ""); + + // Return the list of changed files, removing the working directory prefix and starting slash. return response.data.files .filter(file => file.status == "modified" || file.status == "added") - .map(file => file.filename) + .map(file => file.filename.replace(workingDir, "").replace(/^\//, "")) } const REQUESTED_COMMENTS_PER_PAGE = 20; @@ -23106,6 +23116,7 @@ async function main$1() { core$1.getInput("filter-changed-files").toLowerCase() === "true"; const shouldDeleteOldComments = core$1.getInput("delete-old-comments").toLowerCase() === "true"; + const defaultBranch = core$1.getInput("default-branch") || "main"; const title = core$1.getInput("title"); const raw = await fs.promises.readFile(lcovFile, "utf-8").catch(err => null); @@ -23124,6 +23135,7 @@ async function main$1() { repository: github_1.payload.repository.full_name, prefix: normalisePath(`${process.env.GITHUB_WORKSPACE}/`), workingDir, + defaultBranch, }; if (github_1.eventName === "pull_request") { diff --git a/src/get_changes.js b/src/get_changes.js index d8f322cb..435d9129 100644 --- a/src/get_changes.js +++ b/src/get_changes.js @@ -23,7 +23,11 @@ export async function getChangedFiles(githubClient, options, context) { ) } + // Normalize the working directory path from './' to '' or './src' to 'src' + const workingDir = options.workingDir.replace(/^.\//, "") + + // Return the list of changed files, removing the working directory prefix and starting slash. return response.data.files .filter(file => file.status == "modified" || file.status == "added") - .map(file => file.filename) + .map(file => file.filename.replace(workingDir, "").replace(/^\//, "")) } diff --git a/src/get_changes_test.js b/src/get_changes_test.js new file mode 100644 index 00000000..54979373 --- /dev/null +++ b/src/get_changes_test.js @@ -0,0 +1,116 @@ +import { getChangedFiles } from "./get_changes" +import * as core from "@actions/core" + +jest.mock("@actions/core") + +describe("getChangedFiles", () => { + let mockGithubClient + + beforeEach(() => { + mockGithubClient = { + repos: { + compareCommits: jest.fn(), + }, + } + core.setFailed = jest.fn() + }) + + it("should fail if commit or baseCommit is missing", async () => { + const options = { workingDir: "./" } + const context = { + eventName: "push", + repo: { owner: "owner", repo: "repo" }, + } + + mockGithubClient.repos.compareCommits.mockResolvedValue({ + status: 500, + data: { + files: [], + }, + }) + + await getChangedFiles(mockGithubClient, options, context) + + expect(core.setFailed).toHaveBeenCalledWith( + "The base and head commits are missing from the payload for this push event.", + ) + }) + + it("should fail on non-200 response from GitHub API", async () => { + const options = { + commit: "headSha", + baseCommit: "baseSha", + workingDir: "./", + } + const context = { + eventName: "push", + repo: { owner: "owner", repo: "repo" }, + } + + mockGithubClient.repos.compareCommits.mockResolvedValue({ + status: 404, + data: { + files: [], + }, + }) + + await getChangedFiles(mockGithubClient, options, context) + + expect(core.setFailed).toHaveBeenCalledWith( + "The GitHub API for comparing the base and head commits for this push event returned 404, expected 200.", + ) + }) + + it("should return a list of modified and added files", async () => { + const options = { + commit: "headSha", + baseCommit: "baseSha", + workingDir: "./src", + } + const context = { + eventName: "push", + repo: { owner: "owner", repo: "repo" }, + } + + mockGithubClient.repos.compareCommits.mockResolvedValue({ + status: 200, + data: { + files: [ + { status: "modified", filename: "src/file1.js" }, + { status: "added", filename: "src/file2.js" }, + { status: "removed", filename: "src/file3.js" }, + ], + }, + }) + + const result = await getChangedFiles(mockGithubClient, options, context) + + expect(result).toEqual(["file1.js", "file2.js"]) + }) + + it("should correctly normalize working directory", async () => { + const options = { + commit: "headSha", + baseCommit: "baseSha", + workingDir: "./", + } + const context = { + eventName: "push", + repo: { owner: "owner", repo: "repo" }, + } + + mockGithubClient.repos.compareCommits.mockResolvedValue({ + status: 200, + data: { + files: [ + { status: "modified", filename: "/file1.js" }, + { status: "added", filename: "/file2.js" }, + ], + }, + }) + + const result = await getChangedFiles(mockGithubClient, options, context) + + expect(result).toEqual(["file1.js", "file2.js"]) + }) +}) diff --git a/src/index.js b/src/index.js index 2efc5fb0..bdd018f5 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,7 @@ async function main() { core.getInput("filter-changed-files").toLowerCase() === "true" const shouldDeleteOldComments = core.getInput("delete-old-comments").toLowerCase() === "true" + const defaultBranch = core.getInput("default-branch") || "main" const title = core.getInput("title") const raw = await fs.readFile(lcovFile, "utf-8").catch(err => null) @@ -39,6 +40,7 @@ async function main() { repository: context.payload.repository.full_name, prefix: normalisePath(`${process.env.GITHUB_WORKSPACE}/`), workingDir, + defaultBranch, } if (context.eventName === "pull_request") { diff --git a/src/util.js b/src/util.js index 71a319f6..1f05fd62 100644 --- a/src/util.js +++ b/src/util.js @@ -8,7 +8,13 @@ export function createHref(options, file) { const relative = file.file.replace(options.prefix, "") const parts = relative.split("/") const filename = parts[parts.length - 1] - const url = path.join(options.repository, 'blob', options.commit, options.workingDir || './', relative) + const url = path.join( + options.repository, + "blob", + options.commit || options.defaultBranch, + options.workingDir || "./", + relative, + ) return { href: `https://github.com/${url}`, filename