Skip to content

Commit

Permalink
Merge PR romeovs#53 add fail-drop-percent-threshold, more
Browse files Browse the repository at this point in the history
* max-uncovered-lines
* dont-post-if-no-changed-files-in-report
  • Loading branch information
klausbadelt committed Mar 5, 2023
2 parents 2c4956b + b240b9f commit 9b6012a
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 48 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ If set to true, old comments will be deleted before a new comment is posted
##### `title` (**Optional**)
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

##### `fail-drop-percent-threshold` (**Optional**)
If included, will fail if coverage drops more than the given percentage

## Example usage

```yml
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ inputs:
title:
description: Title to add to the comment
required: false
max-uncovered-lines:
description: Max number of uncovered lines to display in uncovered lines column (integer)
required: false
dont-post-if-no-changed-files-in-report:
description: Set to true to prevent posting when no files in the report have been changed
required: false
default: false
fail-drop-percent-threshold:
description: If included, will fail if coverage drops more than the given percentage
required: false
runs:
using: node16
main: dist/main.js
128 changes: 104 additions & 24 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22834,7 +22834,9 @@ function tabulate(lcov, options) {
],
[],
);

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

Expand Down Expand Up @@ -22921,8 +22923,14 @@ function uncovered(file, options) {

const all = ranges([...branches, ...lines]);

return all
.map(function (range) {
var numNotIncluded = 0;
if (options.maxUncoveredLines) {
const notIncluded = all.splice(options.maxUncoveredLines);
numNotIncluded = notIncluded.length;
}

const result = all
.map(function(range) {
const fragment =
range.start === range.end
? `L${range.start}`
Expand All @@ -22935,7 +22943,13 @@ function uncovered(file, options) {

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

if (numNotIncluded > 0) {
return result + ` and ${numNotIncluded} more...`
} else {
return result
}
}

function ranges(linenos) {
Expand Down Expand Up @@ -22966,6 +22980,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 @@ -22975,14 +22993,16 @@ function comment(lcov, options) {
: `Coverage for this commit`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
"\n\n",
details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
),
reportTable
? details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
reportTable,
)
: "",
)
}

Expand All @@ -22997,6 +23017,16 @@ function diff(lcov, before, options) {
const plus = pdiff > 0 ? "+" : "";
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";

const thresholdWarning =
options.failDropThreshold && pdiff < -options.failDropThreshold
? `Failing due to coverage dropping more than ${options.failDropThreshold}%!`
: "";

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

return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -23006,21 +23036,25 @@ function diff(lcov, before, options) {
: `Coverage for this commit`,
table(
tbody(
tr(th("Coverage"), th("Diff")),
tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
td(pafter.toFixed(2), "%"),
td(arrow, " ", plus, pdiff.toFixed(2), "%"),
),
),
),
thresholdWarning ? b(thresholdWarning) : "",
"\n\n",
details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
),
reportTable
? details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
reportTable,
)
: "",
)
}

Expand Down Expand Up @@ -23106,7 +23140,25 @@ async function main$1() {
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");
const failDropThreshold = core$1.getInput("fail-drop-percent-threshold");

if (maxUncoveredLines && isNaN(parseInt(maxUncoveredLines))) {
console.log(
`Invalid parameter for max-uncovered-lines '${maxUncoveredLines}'. Must be an integer. Exiting...`,
);
return
}
if (failDropThreshold && isNaN(parseFloat(failDropThreshold))) {
console.log(
`Invalid parameter for fail-drop-threshold '${failDropThreshold}'. Must be a number. Exiting...`,
);
return
}

const raw = await fs.promises.readFile(lcovFile, "utf-8").catch(err => null);
if (!raw) {
Expand Down Expand Up @@ -23138,15 +23190,26 @@ async function main$1() {
}

options.shouldFilterChangedFiles = shouldFilterChangedFiles;
options.dontPostIfNoChangedFilesInReport = dontPostIfNoChangedFilesInReport;
options.title = title;
options.failDropThreshold = failDropThreshold;
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);
let body = diff(lcov, baselcov, options);
if (!body) {
console.log(`No changed files in report, exiting...`);
return
} else {
body = body.substring(0, MAX_COMMENT_CHARS);
}

if (github_1.eventName === "pull_request") {
if (shouldDeleteOldComments) {
Expand All @@ -23166,6 +23229,23 @@ async function main$1() {
body: body,
});
}

if (failDropThreshold) {
if (!baselcov) {
console.warn(
"Cannot check coverage drop threshold with no base coverage file. Skipping this step.",
);
} else {
const pbefore = percentage(baselcov);
const pafter = percentage(lcov);
const pdiff = pafter - pbefore;
if (pdiff < -failDropThreshold) {
core$1.setFailed(
`Coverage dropped more than ${failDropThreshold}%. Failing coverage check.`,
);
}
}
}
}

main$1().catch(function(err) {
Expand Down
69 changes: 50 additions & 19 deletions src/comment.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { details, summary, b, fragment, table, tbody, tr, th, h2 } from "./html"
import {
details,
summary,
b,
fragment,
table,
tbody,
tr,
th,
td,
h2,
} from "./html"

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 @@ -13,14 +28,16 @@ export function comment(lcov, options) {
: `Coverage for this commit`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
"\n\n",
details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
),
reportTable
? details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
reportTable,
)
: "",
)
}

Expand All @@ -35,6 +52,16 @@ export function diff(lcov, before, options) {
const plus = pdiff > 0 ? "+" : ""
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴"

const thresholdWarning =
options.failDropThreshold && pdiff < -options.failDropThreshold
? `Failing due to coverage dropping more than ${options.failDropThreshold}%!`
: ""

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

return fragment(
options.title ? h2(options.title) : "",
options.base
Expand All @@ -44,20 +71,24 @@ export function diff(lcov, before, options) {
: `Coverage for this commit`,
table(
tbody(
tr(th("Coverage"), th("Diff")),
tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
td(pafter.toFixed(2), "%"),
td(arrow, " ", plus, pdiff.toFixed(2), "%"),
),
),
),
thresholdWarning ? b(thresholdWarning) : "",
"\n\n",
details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
tabulate(lcov, options),
),
reportTable
? details(
summary(
options.shouldFilterChangedFiles
? "Coverage Report for Changed Files"
: "Coverage Report",
),
reportTable,
)
: "",
)
}
Loading

0 comments on commit 9b6012a

Please sign in to comment.