-
Notifications
You must be signed in to change notification settings - Fork 26
/
annotations.js
83 lines (71 loc) · 2.57 KB
/
annotations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const core = require('@actions/core');
const processSarifReport = function (report) {
if (!report) {
return;
}
const rules = report.runs[0].tool.driver.rules;
const results = report.runs[0].results;
core.startGroup('PMD Results');
core.debug(`processing sarif report`);
core.debug(`rules: ${rules.length}`);
core.debug(`results: ${results.length}`);
results.forEach(violation => {
const rule = rules[violation.ruleIndex];
const logFunction = mapPriority(rule.properties.priority);
violation.locations.forEach(location => {
const annotation = createAnnotation(location.physicalLocation, violation.message.text);
core.info(`\n${annotation.file}:${annotation.startLine}:${rule.id} (Priority: ${rule.properties.priority}):${violation.message.text}`);
logFunction(createDescription(rule), annotation);
});
});
core.endGroup();
}
function mapPriority(pmdPriority) {
switch (pmdPriority) {
case 1: // net.sourceforge.pmd.RulePriority.HIGH
case 2: // net.sourceforge.pmd.RulePriority.MEDIUM_HIGH
return core.error;
case 3: // net.sourceforge.pmd.RulePriority.MEDIUM
case 4: // net.sourceforge.pmd.RulePriority.MEDIUM_LOW
return core.warning;
default: // net.sourceforge.pmd.RulePriority.LOW (5)
return core.notice;
}
}
// AnnotationProperties from @actions/core
function createAnnotation(location, title) {
return {
title: title,
file: location.artifactLocation.uri,
startLine: location.region.startLine,
endLine: location.region.endLine
};
}
function createDescription(rule) {
const lines = rule.fullDescription.text.split(/\n|\r\n/);
// remove empty first line
if (lines.length > 1 && lines[0] === '') {
lines.splice(0, 1);
}
let indentation = '';
const matchResult = lines[0].match(/^([ \t]+).*$/);
if (matchResult !== null) {
indentation = matchResult[1];
}
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith(indentation)) {
lines[i] = lines[i].substr(indentation.length);
}
}
// remove empty last line
if (lines.length > 0 && lines[lines.length - 1].trim() === '') {
lines.splice(lines.length - 1, 1);
}
const description = lines.join('\n');
const desc =
`${description}
${rule.id} (Priority: ${rule.properties.priority}, Ruleset: ${rule.properties.ruleset})
${rule.helpUri.trim()}`;
return desc;
}
module.exports.processSarifReport = processSarifReport;