Skip to content

Commit

Permalink
Relativize paths in SARIF report
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
adangel committed Dec 20, 2021
1 parent 838492e commit ecd6f0c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async function main() {

core.info(`PMD exited with ${execOutput.exitCode}`);

sarif.relativizeReport(reportFile);

violations = sarif.countViolations(reportFile);
core.setOutput('violations', violations);
core.info(`PMD detected ${violations} violations.`);
Expand Down
23 changes: 23 additions & 0 deletions lib/sarif.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');

const countViolations = function (reportFile) {
let count = 0;
Expand All @@ -22,5 +24,26 @@ const loadReport = function (reportFile) {
return JSON.parse(fs.readFileSync(reportFile));
}

const relativizeReport = function (reportFile) {
const report = loadReport(reportFile);
if (report === null) {
return;
}

const prefix = path.normalize(`${process.env['GITHUB_WORKSPACE']}/`);
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);
}
})
});
fs.writeFileSync(reportFile, JSON.stringify(report));
}

module.exports.countViolations = countViolations;
module.exports.loadReport = loadReport;
module.exports.relativizeReport = relativizeReport;
68 changes: 68 additions & 0 deletions tests/data/pmd-report-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.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": "src/classes/UnusedLocalVariableSample.cls"
},
"region": {
"startLine": 3,
"startColumn": 16,
"endLine": 3,
"endColumn": 16
}
}
}
]
}
],
"invocations": [
{
"executionSuccessful": true,
"toolConfigurationNotifications": [],
"toolExecutionNotifications": []
}
]
}
]
}
47 changes: 47 additions & 0 deletions tests/sarif.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const path = require('path');
const sarif = require('../lib/sarif');
const io = require('@actions/io');

const tempPath = path.join(__dirname, 'TEMP');

describe('pmd-github-action-sarif', function () {

beforeEach(async function () {
await io.rmRF(tempPath);
await io.mkdirP(tempPath);
})

afterEach(function () {
delete process.env['GITHUB_WORKSPACE'];
})

afterAll(async function () {
await io.rmRF(tempPath);
})

it('can count violations', () => {
const count = sarif.countViolations(path.join(__dirname, 'data', 'pmd-report.sarif'));
expect(count).toBe(1);
Expand All @@ -28,4 +44,35 @@ describe('pmd-github-action-sarif', function () {
const report = sarif.loadReport(path.join(__dirname, 'data', 'pmd-report-not-existing.sarif'));
expect(report).toBe(null);
})

test('relativize can deal with missing report', () => {
const reportPath = path.join(__dirname, 'data', 'pmd-report-not-existing.sarif');
sarif.relativizeReport(reportPath);
})

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

const reportBefore = sarif.loadReport(reportPath);
expect(reportBefore.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe('/home/andreas/PMD/source/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls');

process.env['GITHUB_WORKSPACE'] = '/home/andreas/PMD/source/pmd-github-action-test';
sarif.relativizeReport(reportPath);
const reportAfter = sarif.loadReport(reportPath);
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);

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

process.env['GITHUB_WORKSPACE'] = '/home/andreas/PMD/source/pmd-github-action-test';
sarif.relativizeReport(reportPath);
const reportAfter = sarif.loadReport(reportPath);
expect(reportAfter.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri).toBe('src/classes/UnusedLocalVariableSample.cls');
})
});

0 comments on commit ecd6f0c

Please sign in to comment.