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

applied 5monkeys#74 from https://github.com/kevinvalk/cobertura-action/tree/prefix
  • Loading branch information
kevinvalk authored and Vadgyik committed Jan 19, 2024
1 parent 18d911b commit 43ef8a4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 Expand Up @@ -80,11 +84,6 @@ Default is no crop.
Link missing line numbers. This only has an effect when `show_missing` is set to `true`.
Defaults to `false`.

### `link_missing_lines_source_dir`

Allows specifying a source directory for `link_missing_lines`, that will be inserted
into the resulting URLs, in-between the commit hash and the file path.

### `only_changed_files`

Only show coverage for changed files.
Expand Down
5 changes: 5 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 @@ -48,6 +52,7 @@ inputs:
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
20 changes: 8 additions & 12 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ async function action(payload) {
}

const path = core.getInput("path", { required: true });
const prefixPath =
core.getInput("prefix_path", { required: false }) ||
core.getInput("link_missing_lines_source_dir", { required: false }) ||
undefined;
const skipCovered = JSON.parse(
core.getInput("skip_covered", { required: true })
);
Expand Down Expand Up @@ -44,8 +48,6 @@ async function action(payload) {
const linkMissingLines = JSON.parse(
core.getInput("link_missing_lines", { required: false }) || "false"
);
const linkMissingLinesSourceDir =
core.getInput("link_missing_lines_source_dir", { required: false }) || null;
const onlyChangedFiles = JSON.parse(
core.getInput("only_changed_files", { required: true })
);
Expand All @@ -55,7 +57,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 All @@ -64,7 +66,6 @@ async function action(payload) {
showMissing,
showMissingMaxLength,
linkMissingLines,
linkMissingLinesSourceDir,
filteredFiles: changedFiles,
reportName,
});
Expand All @@ -88,13 +89,9 @@ async function action(payload) {
}
}

function formatFileUrl(sourceDir, fileName, commit) {
function formatFileUrl(fileName, commit) {
const repo = github.context.repo;
sourceDir = sourceDir ? sourceDir : "";
// Strip leading and trailing slashes.
sourceDir = sourceDir.replace(/\/$/, "").replace(/^\//, "");
const path = (sourceDir ? `${sourceDir}/` : "") + fileName;
return `https://github.com/${repo.owner}/${repo.repo}/blob/${commit}/${path}`;
return `https://github.com/${repo.owner}/${repo.repo}/blob/${commit}/${fileName}`;
}

function formatRangeText([start, end]) {
Expand Down Expand Up @@ -157,7 +154,6 @@ function markdownReport(reports, commit, options) {
showMissing = false,
showMissingMaxLength = -1,
linkMissingLines = false,
linkMissingLinesSourceDir = null,
filteredFiles = null,
reportName = "Coverage Report",
} = options || {};
Expand All @@ -182,7 +178,7 @@ function markdownReport(reports, commit, options) {
status(fileTotal),
showMissing && file.missing
? formatMissingLines(
formatFileUrl(linkMissingLinesSourceDir, file.filename, commit),
formatFileUrl(file.filename, commit),
file.missing,
showMissingMaxLength,
linkMissingLines
Expand Down
20 changes: 0 additions & 20 deletions src/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,26 +566,6 @@ _Minimum allowed coverage is \`100%\`_
_Minimum allowed coverage is \`100%\`_
<p align="right">Generated by :monkey: cobertura-action against deadbeef </p>`);

expect(
markdownReport([dummyReport], commit, {
showMissing: true,
linkMissingLines: true,
linkMissingLinesSourceDir: "path/to/src/",
showMissingMaxLength: 200,
})
).toBe(`<strong>${defaultReportName}</strong>
| File | Coverage | | Missing |
| - | :-: | :-: | :-: |
| **All files** | \`77%\` | :x: | |
| \\_\\_init\\_\\_.py | \`80%\` | :x: | [\`24-26\`](https://github.com/someowner/somerepo/blob/deadbeef/path/to/src/__init__.py?plain=1#L24-L26) |
| bar.py | \`75%\` | :x: | [\`23-24\`](https://github.com/someowner/somerepo/blob/deadbeef/path/to/src/bar.py?plain=1#L23-L24) [\`39-40\`](https://github.com/someowner/somerepo/blob/deadbeef/path/to/src/bar.py?plain=1#L39-L40) [\`50\`](https://github.com/someowner/somerepo/blob/deadbeef/path/to/src/bar.py?plain=1#L50) |
| foo.py | \`75%\` | :x: | |
_Minimum allowed coverage is \`100%\`_
<p align="right">Generated by :monkey: cobertura-action against deadbeef </p>`);

expect(
Expand Down
6 changes: 5 additions & 1 deletion src/cobertura.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs").promises;
const xml2js = require("xml2js");
const util = require("util");
const glob = require("glob-promise");
const pathLib = require("path");
const parseString = util.promisify(xml2js.parseString);

/**
Expand All @@ -24,7 +25,10 @@ async function readCoverageFromFile(path, options) {
.map((klass) => {
return {
...calculateRates(klass),
filename: klass["filename"],
filename:
options.prefixPath != null
? pathLib.join(options.prefixPath, klass["filename"])
: 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 43ef8a4

Please sign in to comment.