Skip to content

Commit

Permalink
Merge branch 'romeovs-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
andybelltree committed Nov 27, 2022
2 parents 1733fef + d98b0ef commit b240b9f
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 31 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ Github token used for posting the comment. Defaults to `${{ github.token }}`.

For alternative `github-token` values see: [Creating Personal Access Tokens](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token)

##### `working-directory` (**Default: ""**)
Path to working directory the same as [default shell property](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun)

##### `lcov-file` (**Optional**)
The location of the lcov file to read the coverage report from. Defaults to
`./coverage/lcov.info`.
`./coverage/lcov.info`. Path is relative to **working-directory** input

##### `lcov-base` (**Optional**)
The location of the lcov file resulting from running the tests in the base
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
description: Set to true to delete old Coverage Report comments
required: false
default: false
working-directory:
description: Set working directory if project is not in root folder
required: false
default: "./"
title:
description: Title to add to the comment
required: false
Expand Down
31 changes: 20 additions & 11 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22794,6 +22794,17 @@ function normalisePath(file) {
return file.replace(/\\/g, "/")
}

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);
return {
href: `https://github.com/${url}`,
filename
};
}

// Tabulate the lcov data in a HTML table.
function tabulate(lcov, options) {
const head = tr(
Expand Down Expand Up @@ -22857,7 +22868,7 @@ function getStatement(file) {
const { branches, functions, lines } = file;

return [branches, functions, lines].reduce(
function(acc, curr) {
function (acc, curr) {
if (!curr) {
return acc
}
Expand All @@ -22883,12 +22894,9 @@ function toRow(file, indent, options) {
}

function filename(file, indent, options) {
const relative = file.file.replace(options.prefix, "");
const href = `https://github.com/${options.repository}/blob/${options.commit}/${relative}`;
const parts = relative.split("/");
const last = parts[parts.length - 1];
const {href, filename} = createHref(options, file);
const space = indent ? "   " : "";
return fragment(space, a({ href }, last))
return fragment(space, a({ href }, filename))
}

function percentage$1(item) {
Expand Down Expand Up @@ -22927,14 +22935,13 @@ function uncovered(file, options) {
range.start === range.end
? `L${range.start}`
: `L${range.start}-L${range.end}`;
const relative = file.file.replace(options.prefix, "");
const href = `https://github.com/${options.repository}/blob/${options.commit}/${relative}#${fragment}`;
const { href } = createHref(options, file);
const text =
range.start === range.end
? range.start
: `${range.start}–${range.end}`;

return a({ href }, text)
return a({ href: `${href}#${fragment}` }, text)
})
.join(", ");

Expand All @@ -22950,7 +22957,7 @@ function ranges(linenos) {

let last = null;

linenos.sort().forEach(function(lineno) {
linenos.sort().forEach(function (lineno) {
if (last === null) {
last = { start: lineno, end: lineno };
return
Expand Down Expand Up @@ -23126,7 +23133,8 @@ const MAX_COMMENT_CHARS = 65536;
async function main$1() {
const token = core$1.getInput("github-token");
const githubClient = new github_2(token);
const lcovFile = core$1.getInput("lcov-file") || "./coverage/lcov.info";
const workingDir = core$1.getInput('working-directory') || './';
const lcovFile = path.join(workingDir, core$1.getInput("lcov-file") || "./coverage/lcov.info");
const baseFile = core$1.getInput("lcov-base");
const shouldFilterChangedFiles =
core$1.getInput("filter-changed-files").toLowerCase() === "true";
Expand Down Expand Up @@ -23167,6 +23175,7 @@ async function main$1() {
const options = {
repository: github_1.payload.repository.full_name,
prefix: normalisePath(`${process.env.GITHUB_WORKSPACE}/`),
workingDir,
};

if (github_1.eventName === "pull_request") {
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fs } from "fs"
import core from "@actions/core"
import { GitHub, context } from "@actions/github"
import path from "path"

import { parse } from "./lcov"
import { diff } from "./comment"
Expand All @@ -14,7 +15,8 @@ const MAX_COMMENT_CHARS = 65536
async function main() {
const token = core.getInput("github-token")
const githubClient = new GitHub(token)
const lcovFile = core.getInput("lcov-file") || "./coverage/lcov.info"
const workingDir = core.getInput('working-directory') || './';
const lcovFile = path.join(workingDir, core.getInput("lcov-file") || "./coverage/lcov.info")
const baseFile = core.getInput("lcov-base")
const shouldFilterChangedFiles =
core.getInput("filter-changed-files").toLowerCase() === "true"
Expand Down Expand Up @@ -55,6 +57,7 @@ async function main() {
const options = {
repository: context.payload.repository.full_name,
prefix: normalisePath(`${process.env.GITHUB_WORKSPACE}/`),
workingDir,
}

if (context.eventName === "pull_request") {
Expand Down
18 changes: 7 additions & 11 deletions src/tabulate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { th, tr, td, table, tbody, a, b, span, fragment } from "./html"
import { normalisePath } from "./util"
import { createHref, normalisePath } from "./util"

// Tabulate the lcov data in a HTML table.
export function tabulate(lcov, options) {
Expand Down Expand Up @@ -64,7 +64,7 @@ function getStatement(file) {
const { branches, functions, lines } = file

return [branches, functions, lines].reduce(
function(acc, curr) {
function (acc, curr) {
if (!curr) {
return acc
}
Expand All @@ -90,12 +90,9 @@ function toRow(file, indent, options) {
}

function filename(file, indent, options) {
const relative = file.file.replace(options.prefix, "")
const href = `https://github.com/${options.repository}/blob/${options.commit}/${relative}`
const parts = relative.split("/")
const last = parts[parts.length - 1]
const {href, filename} = createHref(options, file);
const space = indent ? "   " : ""
return fragment(space, a({ href }, last))
return fragment(space, a({ href }, filename))
}

function percentage(item) {
Expand Down Expand Up @@ -134,14 +131,13 @@ function uncovered(file, options) {
range.start === range.end
? `L${range.start}`
: `L${range.start}-L${range.end}`
const relative = file.file.replace(options.prefix, "")
const href = `https://github.com/${options.repository}/blob/${options.commit}/${relative}#${fragment}`
const { href } = createHref(options, file)
const text =
range.start === range.end
? range.start
: `${range.start}–${range.end}`

return a({ href }, text)
return a({ href: `${href}#${fragment}` }, text)
})
.join(", ")

Expand All @@ -157,7 +153,7 @@ function ranges(linenos) {

let last = null

linenos.sort().forEach(function(lineno) {
linenos.sort().forEach(function (lineno) {
if (last === null) {
last = { start: lineno, end: lineno }
return
Expand Down
15 changes: 8 additions & 7 deletions src/tabulate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ test("tabulate should generate a correct table", function() {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
workingDir: 'frontend'
}

const html = table(
Expand All @@ -135,7 +136,7 @@ test("tabulate should generate a correct table", function() {
td(
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/index.js`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/index.js`,
},
"index.js",
),
Expand All @@ -152,7 +153,7 @@ test("tabulate should generate a correct table", function() {
"   ",
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/foo.js`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/foo.js`,
},
"foo.js",
),
Expand All @@ -164,7 +165,7 @@ test("tabulate should generate a correct table", function() {
td(
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/foo.js#L37`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/foo.js#L37`,
},
37,
),
Expand All @@ -176,7 +177,7 @@ test("tabulate should generate a correct table", function() {
"   ",
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/bar/baz.js`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/bar/baz.js`,
},
"baz.js",
),
Expand All @@ -188,14 +189,14 @@ test("tabulate should generate a correct table", function() {
td(
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/bar/baz.js#L20-L21`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/bar/baz.js#L20-L21`,
},
"20–21",
),
", ",
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/bar/baz.js#L27`,
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/bar/baz.js#L27`,
},
"27",
),
Expand Down Expand Up @@ -613,7 +614,7 @@ test("filtered tabulate should fix backwards slashes in filenames", function() {
shouldFilterChangedFiles: true,
changedFiles: ["src/foo.js"],
}

const html = table(
tbody(
tr(
Expand Down
13 changes: 13 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import path from 'path'

export function normalisePath(file) {
return file.replace(/\\/g, "/")
}

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)
return {
href: `https://github.com/${url}`,
filename
};
}
87 changes: 87 additions & 0 deletions src/util_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { createHref } from "./util"

test('create simple url to file', () => {
const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
}
const file = {
file: "/files/project/index.js",
functions: {
found: 0,
hit: 0,
details: [],
},
}
expect(createHref(options, file)).toEqual({
href: `https://github.com/${options.repository}/blob/${options.commit}/index.js`,
filename: 'index.js'
})
})

describe('has working directory', () => {
test('create url to file with simple working dir path', () => {
const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
workingDir: 'frontend'
}
const file = {
file: "/files/project/index.js",
functions: {
found: 0,
hit: 0,
details: [],
},
}
expect(createHref(options, file)).toEqual({
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/index.js`,
filename: 'index.js'
})
})

test('working dir relative path', () => {
const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
workingDir: './frontend/'
}
const file = {
file: "/files/project/index.js",
functions: {
found: 0,
hit: 0,
details: [],
},
}
expect(createHref(options, file)).toEqual({
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/index.js`,
filename: 'index.js'
})
})

test('working dir path with leading and trailing slashed', () => {
const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
workingDir: '/frontend/'
}
const file = {
file: "/files/project/src/foo.js",
functions: {
found: 0,
hit: 0,
details: [],
},
}
expect(createHref(options, file)).toEqual({
href: `https://github.com/${options.repository}/blob/${options.commit}/frontend/src/foo.js`,
filename: 'foo.js'
})
})

})

0 comments on commit b240b9f

Please sign in to comment.