Skip to content

Commit b7104b3

Browse files
committed
Merge pull request #266 from adangel:issue-249-relative-paths
Relativize paths if Sarif report already contains URIs #266
2 parents 821eeac + 409c051 commit b7104b3

File tree

5 files changed

+165
-4
lines changed

5 files changed

+165
-4
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,27 @@ const relativizeReport = function (reportFile) {
2929
}
3030

3131
const prefix = path.normalize(`${process.env['GITHUB_WORKSPACE']}/`);
32-
const prefixUri = new URL(`file:///${prefix}`).href;
32+
const prefixUri = new URL(`file://${prefix}`).href;
3333
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+
38+
let uri = artifactLocation.uri;
39+
if (uri.startsWith('file://')) {
40+
// sarif report already contains a file uri, remove the prefix "file://".
41+
// this is true for PMD 7.0.0-rc3 and later
42+
uri = uri.substring('file://'.length);
43+
}
3744
// note: this also converts any backslashes from Windows paths into forward slashes
3845
// forward slashes are needed in the sarif report for GitHub annotations and codeql upload
39-
const uri = new URL(`file:///${artifactLocation.uri}`).href;
46+
uri = new URL(`file://${uri}`).href;
4047
if (uri.startsWith(prefixUri)) {
4148
artifactLocation.uri = uri.substring(prefixUri.length);
4249
} else {
4350
// report contains already relative paths
4451
// still use the uri, in order to have forward slashes
45-
artifactLocation.uri = uri.substring('file:///'.length);
52+
artifactLocation.uri = uri.substring('file://'.length);
4653
}
4754
})
4855
});

tests/data/pmd-report-uris.sarif

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.40.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": "file:///home/andreas/PMD/source/pmd-github-action-test/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+
}
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.40.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": "file:///D:/a/pmd-github-action-test/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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ describe('pmd-github-action-sarif', function () {
7575
.toBe('src/classes/UnusedLocalVariableSample.cls');
7676
})
7777

78+
test('can properly relativize report which contains already uris', async () => {
79+
const isWindows = os.platform() === 'win32';
80+
81+
const reportPath = path.join(tempPath, 'pmd-report-uris.sarif');
82+
await io.cp(path.join(__dirname, 'data', isWindows ? 'pmd-report-win-uris.sarif' : 'pmd-report-uris.sarif'), reportPath);
83+
84+
const reportBefore = sarif.loadReport(reportPath);
85+
const fullPath = isWindows ? 'file:///D:/a/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls' : 'file:///home/andreas/PMD/source/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls';
86+
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe(fullPath);
87+
88+
process.env['GITHUB_WORKSPACE'] = isWindows ? 'D:\\a\\pmd-github-action-test' : '/home/andreas/PMD/source/pmd-github-action-test';
89+
sarif.relativizeReport(reportPath);
90+
const reportAfter = sarif.loadReport(reportPath);
91+
// note: not normalizing the paths to platform dependent paths - it must be a valid URI
92+
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri)
93+
.toBe('src/classes/UnusedLocalVariableSample.cls');
94+
})
95+
7896
test('can properly relativize report - windows paths - issue #51', async () => {
7997
const reportPath = path.join(tempPath, 'pmd-report.sarif');
8098
await io.cp(path.join(__dirname, 'data', 'pmd-report-win.sarif'), reportPath);

0 commit comments

Comments
 (0)