Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sonar issue report formats >= v7.5 #81

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/SonarParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SonarParser implements ViolationsParser {
static class SonarReportIssue {
String component;
int line;
SonarIssueTextRange textRange;
Integer startLine;
Integer endLine;
String message;
Expand Down Expand Up @@ -73,6 +74,13 @@ public String toString() {
}
}

static class SonarIssueTextRange {
int startLine;
int endLine;
int startOffset;
int endOffset;
}

static class SonarReport {
String version;
private List<SonarReportIssue> issues;
Expand All @@ -94,11 +102,24 @@ public List<Violation> parseReportOutput(final String string) throws Exception {
final SonarReport sonarReport = new Gson().fromJson(string, SonarReport.class);

final List<Violation> violations = new ArrayList<>();

for (final SonarReportIssue issue : sonarReport.getIssues()) {

if (issue.textRange != null) {
// Issue reports from the SonarQube API versions >= 7.5 use a textRange field for the startLine/endLine fields.
issue.startLine = issue.textRange.startLine;
issue.endLine = issue.textRange.endLine;
}

if (issue.startLine == null || issue.getSeverity() == null) {
LOG.log(Level.FINE, "Ignoring issue: " + issue);
continue;
}

if (issue.message == null) {
issue.message= "N/A";
}

violations.add(
violationBuilder() //
.setFile(issue.getFile()) //
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/se/bjurr/violations/lib/SonarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,32 @@ public void testThatViolationsCanBeParsed2() {
assertThat(actual) //
.hasSize(88);
}

@Test
public void testThatViolationsCanBeParsedWithIssuesReportVersion7_5() {
final String rootFolder = getRootFolder();

final List<Violation> actual =
violationsApi() //
.withPattern(".*/sonar/sonar-report-3\\.json$") //
.inFolder(rootFolder) //
.findAll(SONAR) //
.violations();

assertThat(actual) //
.hasSize(5);

final Violation actualViolationZero = actual.get(0);
assertThat(actualViolationZero.getFile()) //
.isEqualTo("src/main/java/com/example/component/application/providers/WebProvider.java");
assertThat(actualViolationZero.getStartLine()) //
.isEqualTo(56);
assertThat(actualViolationZero.getMessage()) //
.isEqualTo("Complete the task associated to this TODO comment.");

final Violation actualViolationFour = actual.get(4);
assertThat(actualViolationFour.getMessage()) //
.isEqualTo("N/A");

}
}
202 changes: 202 additions & 0 deletions src/test/resources/sonar/sonar-report-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"version": "7.5",
"total": 324,
"p": 1,
"ps": 100,
"paging": {
"pageIndex": 1,
"pageSize": 100,
"total": 324
},
"effortTotal": 10545,
"debtTotal": 10545,
"issues": [
{
"key": "AW9ZELWqZbXQApVMssTY",
"rule": "squid:S1135",
"severity": "INFO",
"component": "com.example.example:exampleapplication:microservices:component:src/main/java/com/example/component/application/providers/WebProvider.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:component",
"line": 56,
"hash": "32d0b2527b9a7178c1c08532020e1630",
"textRange": {
"startLine": 56,
"endLine": 56,
"startOffset": 0,
"endOffset": 83
},
"flows": [],
"status": "OPEN",
"message": "Complete the task associated to this TODO comment.",
"author": "",
"tags": [
"cwe"
],
"creationDate": "2019-12-30T18:06:54-0500",
"updateDate": "2019-12-30T18:06:54-0500",
"type": "CODE_SMELL",
"organization": "default-organization",
"fromHotspot": false
},
{
"key": "AW9ZELWqZbXQApVMssTX",
"rule": "squid:S106",
"severity": "MAJOR",
"component": "com.example.example:exampleapplication:microservices:component:src/main/java/com/example/component/application/providers/WebProvider.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:component",
"line": 57,
"hash": "9f33d47a150e7dca46f2b5cbd37c0c7f",
"textRange": {
"startLine": 57,
"endLine": 57,
"startOffset": 8,
"endOffset": 18
},
"flows": [],
"status": "OPEN",
"message": "Replace this use of System.out or System.err by a logger.",
"effort": "10min",
"debt": "10min",
"author": "",
"tags": [
"bad-practice",
"cert"
],
"creationDate": "2019-12-30T18:06:54-0500",
"updateDate": "2019-12-30T18:06:54-0500",
"type": "CODE_SMELL",
"organization": "default-organization",
"fromHotspot": false
},
{
"key": "AW9Y1CHKZbXQApVMsrp0",
"rule": "squid:S2068",
"severity": "BLOCKER",
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/ApiUrlConstants.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:app",
"line": 10,
"hash": "448e6d1ff0af5f6a7d923d2363926d69",
"textRange": {
"startLine": 10,
"endLine": 10,
"startOffset": 31,
"endOffset": 54
},
"flows": [],
"status": "OPEN",
"message": "'PASSWORD' detected in this expression, review this potentially hard-coded credential.",
"effort": "30min",
"debt": "30min",
"author": "",
"tags": [
"cert",
"cwe",
"owasp-a2",
"sans-top25-porous"
],
"creationDate": "2019-12-30T17:00:40-0500",
"updateDate": "2019-12-30T17:00:40-0500",
"type": "VULNERABILITY",
"organization": "default-organization",
"fromHotspot": false
},
{
"key": "AW9Y1CFWZbXQApVMsrpx",
"rule": "squid:S2259",
"severity": "MAJOR",
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/registration/Delegate.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:app",
"line": 280,
"hash": "3d4091d7f0220b9bf66d784f0c5eed57",
"textRange": {
"startLine": 280,
"endLine": 280,
"startOffset": 50,
"endOffset": 71
},
"flows": [
{
"locations": [
{
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/registration/Delegate.java",
"textRange": {
"startLine": 280,
"endLine": 280,
"startOffset": 50,
"endOffset": 71
},
"msg": "'apiContext' is dereferenced."
},
{
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/registration/Delegate.java",
"textRange": {
"startLine": 258,
"endLine": 258,
"startOffset": 8,
"endOffset": 37
},
"msg": "Implies 'apiContext' is null."
}
]
}
],
"status": "OPEN",
"message": "A \"NullPointerException\" could be thrown; \"apiContext\" is nullable here.",
"effort": "10min",
"debt": "10min",
"author": "",
"tags": [
"cert",
"cwe"
],
"creationDate": "2019-12-30T17:00:40-0500",
"updateDate": "2019-12-30T17:00:40-0500",
"type": "BUG",
"organization": "default-organization",
"fromHotspot": false
},
{
"key": "AW9Y1CGXZbXQApVMsrpy",
"rule": "common-java:DuplicatedBlocks",
"severity": "MAJOR",
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/registration/requests/Request.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:app",
"flows": [],
"status": "OPEN",
"message": "1 duplicated blocks of code must be removed.",
"effort": "20min",
"debt": "20min",
"author": "",
"tags": [
"pitfall"
],
"creationDate": "2019-12-30T17:00:40-0500",
"updateDate": "2019-12-30T17:00:40-0500",
"type": "CODE_SMELL",
"organization": "default-organization",
"fromHotspot": false
},
{
"key": "AW9Y1CHfZbXQApVMsrp1",
"rule": "squid:UselessImportCheck",
"severity": "MINOR",
"component": "com.example.example:exampleapplication:microservices:app:src/main/java/com/example/app/application/controllers/api/updatepassword/Controller.java",
"project": "com.example.example:exampleapplication",
"subProject": "com.example.example:exampleapplication:microservices:app",
"line": 14,
"hash": "91de8c40a73977c152e46e55f5c37c13",
"textRange": {
"startLine": 14,
"endLine": 14,
"startOffset": 0,
"endOffset": 43
}
}
],
"facets": []
}