Skip to content

Commit

Permalink
feat: added prefix_path input that can be used to add a prefix to eac…
Browse files Browse the repository at this point in the history
…h filename in the cobertura report
  • Loading branch information
kevinvalk committed Sep 8, 2022
1 parent ed49cc3 commit c3259d7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ artifacts to circumvent this. See the workflows in this project for an implement

A comment is added to the pull request with the coverage report.

![alt text](img/comment.png "Pull request comment with metrics")
![alt text](img/comment.png 'Pull request comment with metrics')

A check is added to the workflow run.

![alt text](img/check.png "Check with metrics")
![alt text](img/check.png 'Check with metrics')

The check will succeed or fail based on your threshold when `fail_below_threshold` is set to `true`, this allows you to mandate coverage checks pass on your [protected branches](https://docs.github.com/en/github/administering-a-repository/defining-the-mergeability-of-pull-requests/about-protected-branches).

Expand All @@ -39,6 +39,10 @@ The GITHUB_TOKEN. Defaults to `${{github.token}}`

The path to the cobertura report. Defaults to `coverage.xml`. Glob pattern is supported, for example `coverage/*.xml`.

## `prefix_path`

This prefix is added to every `filename` within the cobertura report. This can resolve issues with `link_missing_lines` URLs and `only_changed_files`.

### `skip_covered`

If files with 100% coverage should be ignored. Defaults to `true`.
Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: 'Path to the cobertura file.'
required: true
default: 'coverage.xml'
prefix_path:
description: 'Prefix path to add to each filename.'
required: false
default: ''
skip_covered:
description: 'If files with 100% should be skipped from report.'
required: true
Expand Down Expand Up @@ -44,6 +48,11 @@ inputs:
description: 'Link missing line numbers.'
required: false
default: false
link_missing_lines_source_dir:
description: 'Source directory used for link_missing_lines.'
required: false
default: ''
deprecationMessage: 'Please switch to prefix_path instead (this input is forward to prefix_path).'
only_changed_files:
description: 'Only show coverage for changed files. (only if a PR is present)'
required: true
Expand Down
7 changes: 6 additions & 1 deletion src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async function action(payload) {
}

const path = core.getInput("path", { required: true });
const prefixPath = JSON.parse(
core.getInput("prefix_path", { required: false }) ||
core.getInput("link_missing_lines_source_dir", { required: false }) ||
"null"
);
const skipCovered = JSON.parse(
core.getInput("skip_covered", { required: true })
);
Expand Down Expand Up @@ -53,7 +58,7 @@ async function action(payload) {
? await listChangedFiles(pullRequestNumber)
: null;

const reports = await processCoverage(path, { skipCovered });
const reports = await processCoverage(path, { skipCovered, prefixPath });
const comment = markdownReport(reports, commit, {
minimumCoverage,
showLine,
Expand Down
2 changes: 1 addition & 1 deletion src/cobertura.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function readCoverageFromFile(path, options) {
.map((klass) => {
return {
...calculateRates(klass),
filename: klass["filename"],
filename: (options.prefixPath || "") + klass["filename"],
name: klass["name"],
missing: missingLines(klass),
};
Expand Down
10 changes: 10 additions & 0 deletions src/cobertura.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,13 @@ test("longestCommonPrefix", () => {
expect(longestCommonPrefix(null)).toBe(0);
expect(longestCommonPrefix([])).toBe(0);
});

test("processCoverage(test-prefix.xml, {prefixPath: 'somethingrandom/'})", async () => {
const reports = await processCoverage("./src/fixtures/test-istanbul.xml", {
prefixPath: "somethingrandom/",
skipCovered: false,
});

const files = reports[0].files;
expect(files[0].filename).toBe("somethingrandom/src/action.js");
});

0 comments on commit c3259d7

Please sign in to comment.