Skip to content

Commit cef68b1

Browse files
feat: normalize severity values (#268)
1 parent faea957 commit cef68b1

File tree

7 files changed

+67402
-66278
lines changed

7 files changed

+67402
-66278
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules/
22
sonar-report.html
3-
coverage.lcov
3+
coverage.lcov

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ xdg-open samples/sonar-report_sonar-report.html
6666
6767
Summary of the Detected Vulnerabilities
6868
69-
Severity: BLOCKER
69+
Severity: HIGH
7070
Number of Issues: 0
7171
72-
Severity: CRITICAL
72+
Severity: MEDIUM
7373
Number of Issues: 0
7474
75-
Severity: MAJOR
76-
Number of Issues: 0
77-
78-
Severity: MINOR
75+
Severity: LOW
7976
Number of Issues: 0
8077
```
8178

index.ejs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
3535
<dt>Project Name/URL</dt>
3636
<dd>
37-
<a href="<%= sonarBaseURL %>/dashboard?id=<%= sonarComponent %>" target="_blank"><%= projectName %></a>
37+
<a href="<%= sonarBaseURL %>/dashboard?id=<%= sonarComponent %>" target="_blank">
38+
<%= projectName %>
39+
</a>
3840
</dd>
3941
4042
<dt>Application</dt>
@@ -77,7 +79,8 @@
7779
</dl>
7880
7981
<%if (qualityGateStatus) { %>
80-
<h2>Quality Gate Status: <%= qualityGateStatus.projectStatus.status %> since <%= qualityGateStatusPeriodDate %></h2>
82+
<h2>Quality Gate Status: <%= qualityGateStatus.projectStatus.status %> since <%= qualityGateStatusPeriodDate %>
83+
</h2>
8184
8285
<table>
8386
<thead>
@@ -119,31 +122,24 @@
119122
</thead>
120123
<tbody>
121124
<tr>
122-
<td class="sevBLOCKER"></td>
123-
<td>BLOCKER</td>
124-
<td>
125-
<%= summary.blocker %>
126-
</td>
127-
</tr>
128-
<tr>
129-
<td class="sevCRITICAL"></td>
130-
<td>CRITICAL</td>
125+
<td class="sevHIGH"></td>
126+
<td>HIGH</td>
131127
<td>
132-
<%= summary.critical %>
128+
<%= summary.high %>
133129
</td>
134130
</tr>
135131
<tr>
136-
<td class="sevMAJOR"></td>
137-
<td>MAJOR</td>
132+
<td class="sevMEDIUM"></td>
133+
<td>MEDIUM</td>
138134
<td>
139-
<%= summary.major %>
135+
<%= summary.medium %>
140136
</td>
141137
</tr>
142138
<tr>
143-
<td class="sevMINOR"></td>
144-
<td>MINOR</td>
139+
<td class="sevLOW"></td>
140+
<td>LOW</td>
145141
<td>
146-
<%= summary.minor %>
142+
<%= summary.low %>
147143
</td>
148144
</tr>
149145
</tbody>
@@ -178,7 +174,11 @@
178174
<%= issues[i].rule %>
179175
</a></td>
180176
<td>
177+
<% if(issues[i].severity == "BLOCKER"){ %>
178+
HIGH
179+
<% } else{ %>
181180
<%= issues[i].severity %>
181+
<% } %>
182182
</td>
183183
<td class="component">
184184
<%= issues[i].component %>
@@ -238,15 +238,14 @@
238238
var ctx = canvas.getContext("2d");
239239
240240
var data = [
241-
<%= summary.blocker %>,
242-
<%= summary.critical %>,
243-
<%= summary.major %>,
244-
<%= summary.minor %>
241+
<%= summary.high %>,
242+
<%= summary.medium %>,
243+
<%= summary.low %>
245244
];
246245
var total = data.reduce(function(sum, n) {
247246
return sum + n;
248247
})
249-
var colors = ['#2c3e50', '#d43223', '#f39c12', '#319ddb'];
248+
var colors = ['#d43223', '#f39c12', '#319ddb'];
250249
251250
for (var i = 0, lastend = 0; i < data.length; i++) {
252251
ctx.fillStyle = colors[i];

index.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,35 +163,33 @@ const generateReport = async (options) => {
163163

164164
const issueLink = options.linkIssues
165165
? (data, issue) => (c) =>
166-
`<a href="${data.sonarBaseURL}/project/issues?${
167-
data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
168-
}id=${encodeURIComponent(
169-
data.sonarComponent
170-
)}&issues=${encodeURIComponent(issue.key)}&open=${encodeURIComponent(
171-
issue.key
172-
)}">${c}</a>`
166+
`<a href="${data.sonarBaseURL}/project/issues?${data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
167+
}id=${encodeURIComponent(
168+
data.sonarComponent
169+
)}&issues=${encodeURIComponent(issue.key)}&open=${encodeURIComponent(
170+
issue.key
171+
)}">${c}</a>`
173172
: (data, issue) => (c) => c;
174173

175174
const hotspotLink = options.linkIssues
176175
? (data, hotspot) => (c) =>
177-
`<a href="${data.sonarBaseURL}/security_hotspots?${
178-
data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
179-
}id=${encodeURIComponent(
180-
data.sonarComponent
181-
)}&hotspots=${encodeURIComponent(hotspot.key)}">${c}</a>`
176+
`<a href="${data.sonarBaseURL}/security_hotspots?${data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
177+
}id=${encodeURIComponent(
178+
data.sonarComponent
179+
)}&hotspots=${encodeURIComponent(hotspot.key)}">${c}</a>`
182180
: () => (c) => c;
183181

184182
let severity = new Map();
185183
severity.set("MINOR", 0);
186184
severity.set("MAJOR", 1);
187185
severity.set("CRITICAL", 2);
188186
severity.set("BLOCKER", 3);
189-
let hotspotSeverities = { HIGH: "CRITICAL", MEDIUM: "MAJOR", LOW: "MINOR" };
187+
let hotspotSeverities = { HIGH: "HIGH", MEDIUM: "MEDIUM", LOW: "LOW" };
190188

191189
let properties = [];
192190
try {
193191
properties = getProperties(readFileSync(options.sonarPropertiesFile));
194-
} catch (e) {}
192+
} catch (e) { }
195193

196194
const data = {
197195
date: new Date().toLocaleDateString("en-us", {
@@ -513,6 +511,7 @@ const generateReport = async (options) => {
513511
};
514512
})
515513
);
514+
516515
} catch (error) {
517516
logError("getting issues", error);
518517
return null;
@@ -558,13 +557,15 @@ const generateReport = async (options) => {
558557
);
559558
const hotspot = JSON.parse(response.body);
560559
hSeverity = hotspotSeverities[hotspot.rule.vulnerabilityProbability];
560+
561561
if (hSeverity === undefined) {
562-
hSeverity = "MAJOR";
562+
hSeverity = "MEDIUM";
563563
console.error(
564564
"Unknown hotspot severity: %s",
565565
hotspot.vulnerabilityProbability
566566
);
567567
}
568+
568569
data.issues.push({
569570
rule: hotspot.rule.key,
570571
severity: hSeverity,
@@ -589,12 +590,10 @@ const generateReport = async (options) => {
589590
});
590591

591592
data.summary = {
592-
blocker: data.issues.filter((issue) => issue.severity === "BLOCKER")
593-
.length,
594-
critical: data.issues.filter((issue) => issue.severity === "CRITICAL")
593+
high: data.issues.filter((issue) => (issue.severity === "HIGH" || issue.severity === "BLOCKER"))
595594
.length,
596-
major: data.issues.filter((issue) => issue.severity === "MAJOR").length,
597-
minor: data.issues.filter((issue) => issue.severity === "MINOR").length,
595+
medium: data.issues.filter((issue) => issue.severity === "MEDIUM").length,
596+
low: data.issues.filter((issue) => issue.severity === "LOW").length,
598597
};
599598
}
600599

@@ -615,9 +614,9 @@ const generateReport = async (options) => {
615614
// https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map
616615
if (key === "rules") {
617616
return Array.from(value).reduce((obj, [key, value]) => {
618-
obj[key] = value;
619-
return obj;
620-
}, {});
617+
obj[key] = value;
618+
return obj;
619+
}, {});
621620
} else {
622621
return value
623622
}

0 commit comments

Comments
 (0)