Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect workingDirectory on extracting git changed files #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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") {
Expand Down
6 changes: 5 additions & 1 deletion src/get_changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^\//, ""))
}
116 changes: 116 additions & 0 deletions src/get_changes_test.js
Original file line number Diff line number Diff line change
@@ -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"])
})
})
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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") {
Expand Down
8 changes: 7 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down