From c3259d782836540d3fa769c39739fc02ff5dd971 Mon Sep 17 00:00:00 2001 From: Kevin Valk Date: Thu, 8 Sep 2022 16:01:15 +0200 Subject: [PATCH] feat: added prefix_path input that can be used to add a prefix to each filename in the cobertura report --- README.md | 8 ++++++-- action.yml | 9 +++++++++ src/action.js | 7 ++++++- src/cobertura.js | 2 +- src/cobertura.test.js | 10 ++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0272fc5c..1b4ca4ad 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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`. diff --git a/action.yml b/action.yml index 1e6a5c91..fcacd334 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 diff --git a/src/action.js b/src/action.js index 1c2ac20d..125e6358 100644 --- a/src/action.js +++ b/src/action.js @@ -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 }) ); @@ -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, diff --git a/src/cobertura.js b/src/cobertura.js index 1246831a..74669fc5 100644 --- a/src/cobertura.js +++ b/src/cobertura.js @@ -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), }; diff --git a/src/cobertura.test.js b/src/cobertura.test.js index 34ed7a64..a8214233 100644 --- a/src/cobertura.test.js +++ b/src/cobertura.test.js @@ -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"); +});