diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2018-04-06 11:33:23 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-04-26 20:20:49 +0200 |
commit | 7bd31bc52d296c558803ee633c49851afe13e9ff (patch) | |
tree | 00bf40713bda81ae39861a9bf24fe843f43945ec /sonar-scanner-protocol | |
parent | 95b338ed1b91e2fd4684ac0fe0b4f7f9ac736bb0 (diff) | |
download | sonarqube-7bd31bc52d296c558803ee633c49851afe13e9ff.tar.gz sonarqube-7bd31bc52d296c558803ee633c49851afe13e9ff.zip |
SONAR-10543 Sensor Java API should allow to add external rule engine issues
Diffstat (limited to 'sonar-scanner-protocol')
7 files changed, 83 insertions, 1 deletions
diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java index bf750bd0722..019e64d169d 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java @@ -31,6 +31,7 @@ public class FileStructure { public enum Domain { ISSUES("issues-", Domain.PB), + EXTERNAL_ISSUES("external-issues-", Domain.PB), COMPONENT("component-", Domain.PB), MEASURES("measures-", Domain.PB), DUPLICATIONS("duplications-", Domain.PB), diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java index c6e3c94fa84..2fb837b2558 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java @@ -83,6 +83,14 @@ public class ScannerReportReader { return emptyCloseableIterator(); } + public CloseableIterator<ScannerReport.ExternalIssue> readComponentExternalIssues(int componentRef) { + File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); + if (fileExists(file)) { + return Protobuf.readStream(file, ScannerReport.ExternalIssue.parser()); + } + return emptyCloseableIterator(); + } + public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef); if (fileExists(file)) { diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java index 8b3b9c8202b..ebb6fe925e2 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java @@ -82,6 +82,21 @@ public class ScannerReportWriter { } } + public File writeComponentExternalIssues(int componentRef, Iterable<ScannerReport.ExternalIssue> issues) { + File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); + Protobuf.writeStream(issues, file, false); + return file; + } + + public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) { + File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { + issue.writeDelimitedTo(out); + } catch (Exception e) { + throw ContextException.of("Unable to write external issue", e).addContext("file", file); + } + } + public File writeComponentMeasures(int componentRef, Iterable<ScannerReport.Measure> measures) { File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef); Protobuf.writeStream(measures, file, false); diff --git a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto index c99b9c9882e..9bdb00bd4cf 100644 --- a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto +++ b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto @@ -186,6 +186,30 @@ message Issue { repeated Flow flow = 7; } +message ExternalIssue { + string rule_repository = 1; + string rule_key = 2; + // Only when issue component is a file. Can also be empty for a file if this is an issue global to the file. + string msg = 3; + Severity severity = 4; + int64 effort = 5; + // Only when issue component is a file. Can also be empty for a file if this is an issue global to the file. + // Will be identical to the first location of the first flow + TextRange text_range = 6; + repeated Flow flow = 7; + // Can be empty as the field is optional + string descriptionUrl = 8; + IssueType type = 9; + string rule_title = 10; + string rule_name = 11; +} + +enum IssueType { + CODE_SMELL = 0; + BUG = 1; + VULNERABILITY = 2; +} + message IssueLocation { int32 component_ref = 1; // Only when component is a file. Can be empty for a file if this is an issue global to the file. diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java index 07b8d46e1f5..9a46a5ba1d6 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java @@ -61,7 +61,8 @@ public class FileStructureTest { public void locate_files() throws Exception { File dir = temp.newFolder(); FileUtils.write(new File(dir, "metadata.pb"), "metadata content"); - FileUtils.write(new File(dir, "issues-3.pb"), "issues of component 3"); + FileUtils.write(new File(dir, "issues-3.pb"), "external issues of component 3"); + FileUtils.write(new File(dir, "external-issues-3.pb"), "issues of component 3"); FileUtils.write(new File(dir, "component-42.pb"), "details of component 42"); FileStructure structure = new FileStructure(dir); @@ -69,6 +70,8 @@ public class FileStructureTest { assertThat(structure.fileFor(FileStructure.Domain.COMPONENT, 42)).exists().isFile(); assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 3)).exists().isFile(); assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 42)).doesNotExist(); + assertThat(structure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, 3)).exists().isFile(); + assertThat(structure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, 42)).doesNotExist(); } @Test diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java index 6430f8866e9..6791f83a5f4 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java @@ -105,6 +105,17 @@ public class ScannerReportReaderTest { } @Test + public void read_external_issues() { + ScannerReportWriter writer = new ScannerReportWriter(dir); + ScannerReport.ExternalIssue issue = ScannerReport.ExternalIssue.newBuilder() + .build(); + writer.writeComponentExternalIssues(1, asList(issue)); + + assertThat(underTest.readComponentExternalIssues(1)).hasSize(1); + assertThat(underTest.readComponentExternalIssues(200)).isEmpty(); + } + + @Test public void empty_list_if_no_issue_found() { assertThat(underTest.readComponentIssues(UNKNOWN_COMPONENT_REF)).isEmpty(); } diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java index 647ed49a40e..f4ee4dc3ed3 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java @@ -117,6 +117,26 @@ public class ScannerReportWriterTest { } @Test + public void write_external_issues() { + // no data yet + assertThat(underTest.hasComponentData(FileStructure.Domain.EXTERNAL_ISSUES, 1)).isFalse(); + + // write data + ScannerReport.ExternalIssue issue = ScannerReport.ExternalIssue.newBuilder() + .setMsg("the message") + .build(); + + underTest.writeComponentExternalIssues(1, asList(issue)); + + assertThat(underTest.hasComponentData(FileStructure.Domain.EXTERNAL_ISSUES, 1)).isTrue(); + File file = underTest.getFileStructure().fileFor(FileStructure.Domain.EXTERNAL_ISSUES, 1); + assertThat(file).exists().isFile(); + try (CloseableIterator<ScannerReport.ExternalIssue> read = Protobuf.readStream(file, ScannerReport.ExternalIssue.parser())) { + assertThat(Iterators.size(read)).isEqualTo(1); + } + } + + @Test public void write_measures() { assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isFalse(); |