Skip to content

Commit

Permalink
Merge pull request #266 from adangel:issue-249-relative-paths
Browse files Browse the repository at this point in the history
Relativize paths if Sarif report already contains URIs #266
  • Loading branch information
adangel committed Mar 17, 2024
2 parents 821eeac + 409c051 commit b7104b3
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

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

const prefix = path.normalize(`${process.env['GITHUB_WORKSPACE']}/`);
const prefixUri = new URL(`file:///${prefix}`).href;
const prefixUri = new URL(`file://${prefix}`).href;
core.debug(`Relativizing sarif report '${reportFile}' against '${prefix}'`);
report.runs[0].results.forEach(rule => {
rule.locations.forEach(location => {
const artifactLocation = location.physicalLocation.artifactLocation;

let uri = artifactLocation.uri;
if (uri.startsWith('file://')) {
// sarif report already contains a file uri, remove the prefix "file://".
// this is true for PMD 7.0.0-rc3 and later
uri = uri.substring('file://'.length);
}
// 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;
uri = new URL(`file://${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);
artifactLocation.uri = uri.substring('file://'.length);
}
})
});
Expand Down
68 changes: 68 additions & 0 deletions tests/data/pmd-report-uris.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.40.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": "file:///home/andreas/PMD/source/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls"
},
"region": {
"startLine": 3,
"startColumn": 16,
"endLine": 3,
"endColumn": 16
}
}
}
]
}
],
"invocations": [
{
"executionSuccessful": true,
"toolConfigurationNotifications": [],
"toolExecutionNotifications": []
}
]
}
]
}
68 changes: 68 additions & 0 deletions tests/data/pmd-report-win-uris.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.40.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": "file:///D:/a/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls"
},
"region": {
"startLine": 3,
"startColumn": 16,
"endLine": 3,
"endColumn": 16
}
}
}
]
}
],
"invocations": [
{
"executionSuccessful": true,
"toolConfigurationNotifications": [],
"toolExecutionNotifications": []
}
]
}
]
}
18 changes: 18 additions & 0 deletions tests/sarif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ describe('pmd-github-action-sarif', function () {
.toBe('src/classes/UnusedLocalVariableSample.cls');
})

test('can properly relativize report which contains already uris', async () => {
const isWindows = os.platform() === 'win32';

const reportPath = path.join(tempPath, 'pmd-report-uris.sarif');
await io.cp(path.join(__dirname, 'data', isWindows ? 'pmd-report-win-uris.sarif' : 'pmd-report-uris.sarif'), reportPath);

const reportBefore = sarif.loadReport(reportPath);
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';
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe(fullPath);

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);
// 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);
Expand Down

0 comments on commit b7104b3

Please sign in to comment.