Skip to content

Commit

Permalink
Merge pull request #5 from andybelltree/add-skip-empty-post-option
Browse files Browse the repository at this point in the history
Add an option dont-post-if-no-changed-files-in-report. If this is set…
  • Loading branch information
andybelltree authored Aug 29, 2022
2 parents 1ebf368 + 03f4ee4 commit 4985dc8
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ If included, will be added as a title for the comment produced.
##### `max-uncovered-lines` (**Optional**)
If included, will limit the number of uncovered lines displayed in the Uncovered Lines column.

##### `dont-post-if-no-changed-files-in-report` (**Optional**)
If included, will skip posting a coverage report if no changed files would be included in the report

## Example usage

```yml
Expand Down
33 changes: 27 additions & 6 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22823,7 +22823,9 @@ function tabulate(lcov, options) {
],
[],
);

if (rows.length === 0) {
return ""
}
return table(tbody(head, ...rows))
}

Expand Down Expand Up @@ -22971,6 +22973,10 @@ function ranges(linenos) {
}

function comment(lcov, options) {
const reportTable = tabulate(lcov, options);
if (options.dontPostIfNoChangedFilesInReport && !reportTable) {
return
}
return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -22986,7 +22992,7 @@ function comment(lcov, options) {
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
reportTable,
),
)
}
Expand All @@ -23002,6 +23008,11 @@ function diff(lcov, before, options) {
const plus = pdiff > 0 ? "+" : "";
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";

const reportTable = tabulate(lcov, options);
if (options.dontPostIfNoChangedFilesInReport && !reportTable) {
return
}

return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -23024,7 +23035,7 @@ function diff(lcov, before, options) {
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
reportTable,
),
)
}
Expand Down Expand Up @@ -23106,8 +23117,13 @@ async function main$1() {
const githubClient = new github_2(token);
const lcovFile = 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';
const shouldDeleteOldComments = core$1.getInput("delete-old-comments").toLowerCase() === 'true';
const shouldFilterChangedFiles =
core$1.getInput("filter-changed-files").toLowerCase() === "true";
const shouldDeleteOldComments =
core$1.getInput("delete-old-comments").toLowerCase() === "true";
const dontPostIfNoChangedFilesInReport =
core$1.getInput("dont-post-if-no-changed-files-in-report").toLowerCase() ===
"true";
const title = core$1.getInput("title");
const maxUncoveredLines = core$1.getInput("max-uncovered-lines");
if (maxUncoveredLines && isNaN(parseInt(maxUncoveredLines))) {
Expand Down Expand Up @@ -23146,18 +23162,23 @@ async function main$1() {
}

options.shouldFilterChangedFiles = shouldFilterChangedFiles;
options.dontPostIfNoChangedFilesInReport = dontPostIfNoChangedFilesInReport;
options.title = title;
if (maxUncoveredLines) {
options.maxUncoveredLines = parseInt(maxUncoveredLines);
}

if (shouldFilterChangedFiles) {
if (shouldFilterChangedFiles || dontPostIfNoChangedFilesInReport) {
options.changedFiles = await getChangedFiles(githubClient, options, github_1);
}

const lcov = await parse$2(raw);
const baselcov = baseRaw && (await parse$2(baseRaw));
const body = diff(lcov, baselcov, options).substring(0, MAX_COMMENT_CHARS);
if (!body) {
console.log(`No changed files in report, exiting...`);
return
}

if (shouldDeleteOldComments) {
await deleteOldComments(githubClient, options, github_1);
Expand Down
13 changes: 11 additions & 2 deletions src/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { percentage } from "./lcov"
import { tabulate } from "./tabulate"

export function comment(lcov, options) {
const reportTable = tabulate(lcov, options)
if (options.dontPostIfNoChangedFilesInReport && !reportTable) {
return
}
return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -19,7 +23,7 @@ export function comment(lcov, options) {
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
reportTable,
),
)
}
Expand All @@ -35,6 +39,11 @@ export function diff(lcov, before, options) {
const plus = pdiff > 0 ? "+" : ""
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴"

const reportTable = tabulate(lcov, options)
if (options.dontPostIfNoChangedFilesInReport && !reportTable) {
return
}

return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -57,7 +66,7 @@ export function diff(lcov, before, options) {
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
reportTable,
),
)
}
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ async function main() {
core.getInput("filter-changed-files").toLowerCase() === "true"
const shouldDeleteOldComments =
core.getInput("delete-old-comments").toLowerCase() === "true"
const dontPostIfNoChangedFilesInReport =
core.getInput("dont-post-if-no-changed-files-in-report").toLowerCase() ===
"true"
const title = core.getInput("title")
const maxUncoveredLines = core.getInput("max-uncovered-lines")
if (maxUncoveredLines && isNaN(parseInt(maxUncoveredLines))) {
Expand Down Expand Up @@ -57,18 +60,23 @@ async function main() {
}

options.shouldFilterChangedFiles = shouldFilterChangedFiles
options.dontPostIfNoChangedFilesInReport = dontPostIfNoChangedFilesInReport
options.title = title
if (maxUncoveredLines) {
options.maxUncoveredLines = parseInt(maxUncoveredLines)
}

if (shouldFilterChangedFiles) {
if (shouldFilterChangedFiles || dontPostIfNoChangedFilesInReport) {
options.changedFiles = await getChangedFiles(githubClient, options, context)
}

const lcov = await parse(raw)
const baselcov = baseRaw && (await parse(baseRaw))
const body = diff(lcov, baselcov, options).substring(0, MAX_COMMENT_CHARS)
if (!body) {
console.log(`No changed files in report, exiting...`)
return
}

if (shouldDeleteOldComments) {
await deleteOldComments(githubClient, options, context)
Expand Down
4 changes: 3 additions & 1 deletion src/tabulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function tabulate(lcov, options) {
],
[],
)

if (rows.length === 0) {
return ""
}
return table(tbody(head, ...rows))
}

Expand Down
125 changes: 125 additions & 0 deletions src/tabulate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,131 @@ test("filtered tabulate should generate a correct table with only changed files"
expect(tabulate(data, options)).toBe(html)
})

test("filtered tabulate should generate an empty result when no matching changed files and dontPostIfNoChangedFilesInReport set", function() {
const data = [
{
file: "/files/project/index.js",
functions: {
found: 0,
hit: 0,
details: [],
},
},
{
file: "/files/project/src/foo.js",
lines: {
found: 23,
hit: 21,
details: [
{
line: 20,
hit: 3,
},
{
line: 21,
hit: 3,
},
{
line: 22,
hit: 3,
},
],
},
functions: {
hit: 2,
found: 3,
details: [
{
name: "foo",
line: 19,
},
{
name: "bar",
line: 33,
},
{
name: "baz",
line: 54,
},
],
},
branches: {
hit: 3,
found: 3,
details: [
{
line: 21,
block: 0,
branch: 0,
taken: 1,
},
{
line: 21,
block: 0,
branch: 1,
taken: 2,
},
{
line: 37,
block: 1,
branch: 0,
taken: 0,
},
],
},
},
{
file: "/files/project/src/bar/baz.js",
lines: {
found: 10,
hit: 5,
details: [
{
line: 20,
hit: 0,
},
{
line: 21,
hit: 0,
},
{
line: 27,
hit: 0,
},
],
},
functions: {
hit: 2,
found: 3,
details: [
{
name: "foo",
line: 19,
},
{
name: "bar",
line: 33,
},
{
name: "baz",
line: 54,
},
],
},
},
]

const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
shouldFilterChangedFiles: true,
changedFiles: ["src/not-in-this-report.js"],
}

expect(tabulate(data, options)).toBe("")
})

test("filtered tabulate should fix backwards slashes in filenames", function() {
const data = [
{
Expand Down

0 comments on commit 4985dc8

Please sign in to comment.