Skip to content

Commit

Permalink
feat: normalize severity values (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecampagnards authored Apr 25, 2024
1 parent faea957 commit cef68b1
Show file tree
Hide file tree
Showing 7 changed files with 67,402 additions and 66,278 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
sonar-report.html
coverage.lcov
coverage.lcov
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ xdg-open samples/sonar-report_sonar-report.html
Summary of the Detected Vulnerabilities
Severity: BLOCKER
Severity: HIGH
Number of Issues: 0
Severity: CRITICAL
Severity: MEDIUM
Number of Issues: 0
Severity: MAJOR
Number of Issues: 0
Severity: MINOR
Severity: LOW
Number of Issues: 0
```

Expand Down
45 changes: 22 additions & 23 deletions index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
<dt>Project Name/URL</dt>
<dd>
<a href="<%= sonarBaseURL %>/dashboard?id=<%= sonarComponent %>" target="_blank"><%= projectName %></a>
<a href="<%= sonarBaseURL %>/dashboard?id=<%= sonarComponent %>" target="_blank">
<%= projectName %>
</a>
</dd>
<dt>Application</dt>
Expand Down Expand Up @@ -77,7 +79,8 @@
</dl>
<%if (qualityGateStatus) { %>
<h2>Quality Gate Status: <%= qualityGateStatus.projectStatus.status %> since <%= qualityGateStatusPeriodDate %></h2>
<h2>Quality Gate Status: <%= qualityGateStatus.projectStatus.status %> since <%= qualityGateStatusPeriodDate %>
</h2>
<table>
<thead>
Expand Down Expand Up @@ -119,31 +122,24 @@
</thead>
<tbody>
<tr>
<td class="sevBLOCKER"></td>
<td>BLOCKER</td>
<td>
<%= summary.blocker %>
</td>
</tr>
<tr>
<td class="sevCRITICAL"></td>
<td>CRITICAL</td>
<td class="sevHIGH"></td>
<td>HIGH</td>
<td>
<%= summary.critical %>
<%= summary.high %>
</td>
</tr>
<tr>
<td class="sevMAJOR"></td>
<td>MAJOR</td>
<td class="sevMEDIUM"></td>
<td>MEDIUM</td>
<td>
<%= summary.major %>
<%= summary.medium %>
</td>
</tr>
<tr>
<td class="sevMINOR"></td>
<td>MINOR</td>
<td class="sevLOW"></td>
<td>LOW</td>
<td>
<%= summary.minor %>
<%= summary.low %>
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -178,7 +174,11 @@
<%= issues[i].rule %>
</a></td>
<td>
<% if(issues[i].severity == "BLOCKER"){ %>
HIGH
<% } else{ %>
<%= issues[i].severity %>
<% } %>
</td>
<td class="component">
<%= issues[i].component %>
Expand Down Expand Up @@ -238,15 +238,14 @@
var ctx = canvas.getContext("2d");
var data = [
<%= summary.blocker %>,
<%= summary.critical %>,
<%= summary.major %>,
<%= summary.minor %>
<%= summary.high %>,
<%= summary.medium %>,
<%= summary.low %>
];
var total = data.reduce(function(sum, n) {
return sum + n;
})
var colors = ['#2c3e50', '#d43223', '#f39c12', '#319ddb'];
var colors = ['#d43223', '#f39c12', '#319ddb'];
for (var i = 0, lastend = 0; i < data.length; i++) {
ctx.fillStyle = colors[i];
Expand Down
45 changes: 22 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,35 +163,33 @@ const generateReport = async (options) => {

const issueLink = options.linkIssues
? (data, issue) => (c) =>
`<a href="${data.sonarBaseURL}/project/issues?${
data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
}id=${encodeURIComponent(
data.sonarComponent
)}&issues=${encodeURIComponent(issue.key)}&open=${encodeURIComponent(
issue.key
)}">${c}</a>`
`<a href="${data.sonarBaseURL}/project/issues?${data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
}id=${encodeURIComponent(
data.sonarComponent
)}&issues=${encodeURIComponent(issue.key)}&open=${encodeURIComponent(
issue.key
)}">${c}</a>`
: (data, issue) => (c) => c;

const hotspotLink = options.linkIssues
? (data, hotspot) => (c) =>
`<a href="${data.sonarBaseURL}/security_hotspots?${
data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
}id=${encodeURIComponent(
data.sonarComponent
)}&hotspots=${encodeURIComponent(hotspot.key)}">${c}</a>`
`<a href="${data.sonarBaseURL}/security_hotspots?${data.branch ? "branch=" + encodeURIComponent(data.branch) + "&" : ""
}id=${encodeURIComponent(
data.sonarComponent
)}&hotspots=${encodeURIComponent(hotspot.key)}">${c}</a>`
: () => (c) => c;

let severity = new Map();
severity.set("MINOR", 0);
severity.set("MAJOR", 1);
severity.set("CRITICAL", 2);
severity.set("BLOCKER", 3);
let hotspotSeverities = { HIGH: "CRITICAL", MEDIUM: "MAJOR", LOW: "MINOR" };
let hotspotSeverities = { HIGH: "HIGH", MEDIUM: "MEDIUM", LOW: "LOW" };

let properties = [];
try {
properties = getProperties(readFileSync(options.sonarPropertiesFile));
} catch (e) {}
} catch (e) { }

const data = {
date: new Date().toLocaleDateString("en-us", {
Expand Down Expand Up @@ -513,6 +511,7 @@ const generateReport = async (options) => {
};
})
);

} catch (error) {
logError("getting issues", error);
return null;
Expand Down Expand Up @@ -558,13 +557,15 @@ const generateReport = async (options) => {
);
const hotspot = JSON.parse(response.body);
hSeverity = hotspotSeverities[hotspot.rule.vulnerabilityProbability];

if (hSeverity === undefined) {
hSeverity = "MAJOR";
hSeverity = "MEDIUM";
console.error(
"Unknown hotspot severity: %s",
hotspot.vulnerabilityProbability
);
}

data.issues.push({
rule: hotspot.rule.key,
severity: hSeverity,
Expand All @@ -589,12 +590,10 @@ const generateReport = async (options) => {
});

data.summary = {
blocker: data.issues.filter((issue) => issue.severity === "BLOCKER")
.length,
critical: data.issues.filter((issue) => issue.severity === "CRITICAL")
high: data.issues.filter((issue) => (issue.severity === "HIGH" || issue.severity === "BLOCKER"))
.length,
major: data.issues.filter((issue) => issue.severity === "MAJOR").length,
minor: data.issues.filter((issue) => issue.severity === "MINOR").length,
medium: data.issues.filter((issue) => issue.severity === "MEDIUM").length,
low: data.issues.filter((issue) => issue.severity === "LOW").length,
};
}

Expand All @@ -615,9 +614,9 @@ const generateReport = async (options) => {
// https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map
if (key === "rules") {
return Array.from(value).reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
obj[key] = value;
return obj;
}, {});
} else {
return value
}
Expand Down
Loading

0 comments on commit cef68b1

Please sign in to comment.