Skip to content

Commit

Permalink
Fix relative windows paths in sarif report
Browse files Browse the repository at this point in the history
Fixes #177
  • Loading branch information
adangel committed Mar 18, 2023
1 parent a552efd commit 5a119f4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion lib/sarif.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ 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}`);
core.debug(`Relativizing sarif report '${reportFile}' against '${prefix}'`);
report.runs[0].results.forEach(rule => {
rule.locations.forEach(location => {
const artifactLocation = location.physicalLocation.artifactLocation;
// note: this also converts any backslashes from Windows paths into forward slashes
// forward slashes are needed in the sarif report for GitHub annotations and codeql upload
const uri = new URL(`file:///${artifactLocation.uri}`).href;
if (uri.startsWith(prefixUri)) {
artifactLocation.uri = uri.substring(prefixUri.length);
} else {
// report contains already relative paths
// still use the uri, in order to have forward slashes
artifactLocation.uri = uri.substring('file:///'.length);
}
})
});
Expand Down
68 changes: 68 additions & 0 deletions tests/data/pmd-report-win-relativized.sarif
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "PMD",
"version": "6.54.0",
"informationUri": "https://pmd.github.io/pmd/",
"rules": [
{
"id": "UnusedLocalVariable",
"shortDescription": {
"text": "Variable 'x' defined but not used"
},
"fullDescription": {
"text": "\n Detects when a local variable is declared and/or assigned but not used.\n Second line.\n Third line with additional indentation.\n Fourth line with less indentation.\n "
},
"helpUri": "https://pmd.github.io/pmd-6.40.0/pmd_rules_apex_bestpractices.html#unusedlocalvariable",
"help": {
"text": "\nDetects when a local variable is declared and/or assigned but not used.\n "
},
"properties": {
"ruleset": "Best Practices",
"priority": 5,
"tags": [
"Best Practices"
]
}
}
]
}
},
"results": [
{
"ruleId": "UnusedLocalVariable",
"ruleIndex": 0,
"message": {
"text": "Variable 'x' defined but not used"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "src\\classes\\UnusedLocalVariableSample.cls"
},
"region": {
"startLine": 3,
"startColumn": 16,
"endLine": 3,
"endColumn": 16
}
}
}
]
}
],
"invocations": [
{
"executionSuccessful": true,
"toolConfigurationNotifications": [],
"toolExecutionNotifications": []
}
]
}
]
}
16 changes: 16 additions & 0 deletions tests/sarif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ describe('pmd-github-action-sarif', function () {
.toBe('src/classes/UnusedLocalVariableSample.cls');
})

test('convert backslash to forward slash for already relativized report - windows paths - issue #177', async () => {
const reportPath = path.join(tempPath, 'pmd-report.sarif');
await io.cp(path.join(__dirname, 'data', 'pmd-report-win-relativized.sarif'), reportPath);

const reportBefore = sarif.loadReport(reportPath);
const windowsPath = 'src\\classes\\UnusedLocalVariableSample.cls';
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe(windowsPath);

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 with forward slashes
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri)
.toBe('src/classes/UnusedLocalVariableSample.cls');
})

test('can properly relativize already relativized report', async () => {
const reportPath = path.join(tempPath, 'pmd-report.sarif');
await io.cp(path.join(__dirname, 'data', 'pmd-report-relativized.sarif'), reportPath);
Expand Down

0 comments on commit 5a119f4

Please sign in to comment.