Skip to content

Commit 34a63d8

Browse files
authored
Fix relative windows paths in sarif report (#178)
Fixes #177
1 parent 697bb43 commit 34a63d8

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/sarif.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ const relativizeReport = function (reportFile) {
3030

3131
const prefix = path.normalize(`${process.env['GITHUB_WORKSPACE']}/`);
3232
const prefixUri = new URL(`file:///${prefix}`).href;
33-
core.debug(`Relativizing sarif ${reportFile} report against ${prefix}`);
33+
core.debug(`Relativizing sarif report '${reportFile}' against '${prefix}'`);
3434
report.runs[0].results.forEach(rule => {
3535
rule.locations.forEach(location => {
3636
const artifactLocation = location.physicalLocation.artifactLocation;
37+
// note: this also converts any backslashes from Windows paths into forward slashes
38+
// forward slashes are needed in the sarif report for GitHub annotations and codeql upload
3739
const uri = new URL(`file:///${artifactLocation.uri}`).href;
3840
if (uri.startsWith(prefixUri)) {
3941
artifactLocation.uri = uri.substring(prefixUri.length);
42+
} else {
43+
// report contains already relative paths
44+
// still use the uri, in order to have forward slashes
45+
artifactLocation.uri = uri.substring('file:///'.length);
4046
}
4147
})
4248
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
3+
"version": "2.1.0",
4+
"runs": [
5+
{
6+
"tool": {
7+
"driver": {
8+
"name": "PMD",
9+
"version": "6.54.0",
10+
"informationUri": "https://pmd.github.io/pmd/",
11+
"rules": [
12+
{
13+
"id": "UnusedLocalVariable",
14+
"shortDescription": {
15+
"text": "Variable 'x' defined but not used"
16+
},
17+
"fullDescription": {
18+
"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 "
19+
},
20+
"helpUri": "https://pmd.github.io/pmd-6.40.0/pmd_rules_apex_bestpractices.html#unusedlocalvariable",
21+
"help": {
22+
"text": "\nDetects when a local variable is declared and/or assigned but not used.\n "
23+
},
24+
"properties": {
25+
"ruleset": "Best Practices",
26+
"priority": 5,
27+
"tags": [
28+
"Best Practices"
29+
]
30+
}
31+
}
32+
]
33+
}
34+
},
35+
"results": [
36+
{
37+
"ruleId": "UnusedLocalVariable",
38+
"ruleIndex": 0,
39+
"message": {
40+
"text": "Variable 'x' defined but not used"
41+
},
42+
"locations": [
43+
{
44+
"physicalLocation": {
45+
"artifactLocation": {
46+
"uri": "src\\classes\\UnusedLocalVariableSample.cls"
47+
},
48+
"region": {
49+
"startLine": 3,
50+
"startColumn": 16,
51+
"endLine": 3,
52+
"endColumn": 16
53+
}
54+
}
55+
}
56+
]
57+
}
58+
],
59+
"invocations": [
60+
{
61+
"executionSuccessful": true,
62+
"toolConfigurationNotifications": [],
63+
"toolExecutionNotifications": []
64+
}
65+
]
66+
}
67+
]
68+
}

tests/sarif.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ describe('pmd-github-action-sarif', function () {
9191
.toBe('src/classes/UnusedLocalVariableSample.cls');
9292
})
9393

94+
test('convert backslash to forward slash for already relativized report - windows paths - issue #177', async () => {
95+
const reportPath = path.join(tempPath, 'pmd-report.sarif');
96+
await io.cp(path.join(__dirname, 'data', 'pmd-report-win-relativized.sarif'), reportPath);
97+
98+
const reportBefore = sarif.loadReport(reportPath);
99+
const windowsPath = 'src\\classes\\UnusedLocalVariableSample.cls';
100+
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe(windowsPath);
101+
102+
process.env['GITHUB_WORKSPACE'] = 'D:\\a\\pmd-github-action-test';
103+
sarif.relativizeReport(reportPath);
104+
const reportAfter = sarif.loadReport(reportPath);
105+
// note: not normalizing the paths to platform dependent paths - it must be a valid URI with forward slashes
106+
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri)
107+
.toBe('src/classes/UnusedLocalVariableSample.cls');
108+
})
109+
94110
test('can properly relativize already relativized report', async () => {
95111
const reportPath = path.join(tempPath, 'pmd-report.sarif');
96112
await io.cp(path.join(__dirname, 'data', 'pmd-report-relativized.sarif'), reportPath);

0 commit comments

Comments
 (0)