Skip to content

Commit

Permalink
Merge pull request #59 from adangel:issue-51-annotations-windows
Browse files Browse the repository at this point in the history
Fixes #51 Convert Windows paths #59
  • Loading branch information
adangel committed Feb 19, 2022
2 parents 1039c3f + 22808ce commit c087fa6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions lib/sarif.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ const relativizeReport = function (reportFile) {
}

const prefix = path.normalize(`${process.env['GITHUB_WORKSPACE']}/`);
const prefixUri = new URL(`file:///${prefix}`).href;
core.debug(`Relativizing sarif ${reportFile} report against ${prefix}`);
report.runs[0].results.forEach(rule => {
rule.locations.forEach(location => {
const artifactLocation = location.physicalLocation.artifactLocation;
const uri = artifactLocation.uri;
if (uri.startsWith(prefix)) {
artifactLocation.uri = uri.substr(prefix.length);
const uri = new URL(`file:///${artifactLocation.uri}`).href;
if (uri.startsWith(prefixUri)) {
artifactLocation.uri = uri.substring(prefixUri.length);
}
})
});
Expand Down
21 changes: 19 additions & 2 deletions tests/sarif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,25 @@ describe('pmd-github-action-sarif', function () {
process.env['GITHUB_WORKSPACE'] = isWindows ? 'D:\\a\\pmd-github-action-test' : '/home/andreas/PMD/source/pmd-github-action-test';
sarif.relativizeReport(reportPath);
const reportAfter = sarif.loadReport(reportPath);
expect(path.normalize(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri))
.toBe(path.normalize('src/classes/UnusedLocalVariableSample.cls'));
// note: not normalizing the paths to platform dependent paths - it must be a valid URI
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri)
.toBe('src/classes/UnusedLocalVariableSample.cls');
})

test('can properly relativize report - windows paths - issue #51', async () => {
const reportPath = path.join(tempPath, 'pmd-report.sarif');
await io.cp(path.join(__dirname, 'data', 'pmd-report-win.sarif'), reportPath);

const reportBefore = sarif.loadReport(reportPath);
const fullPath = 'D:\\a\\pmd-github-action-test\\src\\classes\\UnusedLocalVariableSample.cls';
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe(fullPath);

process.env['GITHUB_WORKSPACE'] = 'D:\\a\\pmd-github-action-test';
sarif.relativizeReport(reportPath);
const reportAfter = sarif.loadReport(reportPath);
// note: not normalizing the paths to platform dependent paths - it must be a valid URI
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri)
.toBe('src/classes/UnusedLocalVariableSample.cls');
})

test('can properly relativize already relativized report', async () => {
Expand Down

0 comments on commit c087fa6

Please sign in to comment.