diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2018-09-11 16:28:09 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2018-10-10 09:23:04 +0200 |
commit | 8b06219ef58e8eb5382ff1310747632e2c2d366e (patch) | |
tree | b17d57a4dc2a1a2bcc96bfff92f25b8423901016 /sonar-scanner-engine | |
parent | 9c38ed460e4b4cb01e7ed977f52424cdfd4a747e (diff) | |
download | sonarqube-8b06219ef58e8eb5382ff1310747632e2c2d366e.tar.gz sonarqube-8b06219ef58e8eb5382ff1310747632e2c2d366e.zip |
SONAR-11241 Write analysis warnings to scanner report
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 144 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisWarningsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisWarningsPublisher.java new file mode 100644 index 00000000000..b8c4284b96b --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisWarningsPublisher.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.report; + +import java.util.List; +import java.util.stream.Collectors; +import org.sonar.scanner.notifications.DefaultAnalysisWarnings; +import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.scanner.protocol.output.ScannerReportWriter; + +public class AnalysisWarningsPublisher implements ReportPublisherStep { + + private final DefaultAnalysisWarnings defaultAnalysisWarnings; + + public AnalysisWarningsPublisher(DefaultAnalysisWarnings defaultAnalysisWarnings) { + this.defaultAnalysisWarnings = defaultAnalysisWarnings; + } + + @Override + public void publish(ScannerReportWriter writer) { + List<DefaultAnalysisWarnings.Message> warnings = defaultAnalysisWarnings.warnings(); + if (warnings.isEmpty()) { + return; + } + writer.writeAnalysisWarnings(warnings.stream() + .map(AnalysisWarningsPublisher::toProtobufAnalysisWarning) + .collect(Collectors.toList())); + } + + private static ScannerReport.AnalysisWarning toProtobufAnalysisWarning(DefaultAnalysisWarnings.Message message) { + return ScannerReport.AnalysisWarning.newBuilder() + .setText(message.getText()) + .setTimestamp(message.getTimestamp()) + .build(); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 3e47377c768..e3e313974fc 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -58,6 +58,7 @@ import org.sonar.scanner.mediumtest.ScanTaskObservers; import org.sonar.scanner.notifications.DefaultAnalysisWarnings; import org.sonar.scanner.report.ActiveRulesPublisher; import org.sonar.scanner.report.AnalysisContextReportPublisher; +import org.sonar.scanner.report.AnalysisWarningsPublisher; import org.sonar.scanner.report.ChangedLinesPublisher; import org.sonar.scanner.report.ComponentsPublisher; import org.sonar.scanner.report.ContextPropertiesPublisher; @@ -204,6 +205,7 @@ public class ProjectScanContainer extends ComponentContainer { AnalysisContextReportPublisher.class, MetadataPublisher.class, ActiveRulesPublisher.class, + AnalysisWarningsPublisher.class, // Cpd CpdExecutor.class, @@ -226,8 +228,7 @@ public class ProjectScanContainer extends ComponentContainer { CoveragePublisher.class, SourcePublisher.class, ChangedLinesPublisher.class, - TestExecutionAndCoveragePublisher.class - ); + TestExecutionAndCoveragePublisher.class); } private void addIssueTrackingComponents() { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisWarningsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisWarningsPublisherTest.java new file mode 100644 index 00000000000..4d3d0f895dd --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisWarningsPublisherTest.java @@ -0,0 +1,88 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.report; + +import com.google.common.collect.Lists; +import java.io.File; +import java.io.IOException; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.notifications.AnalysisWarnings; +import org.sonar.api.utils.System2; +import org.sonar.scanner.notifications.DefaultAnalysisWarnings; +import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.scanner.protocol.output.ScannerReportReader; +import org.sonar.scanner.protocol.output.ScannerReportWriter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class AnalysisWarningsPublisherTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private final AnalysisWarnings analysisWarnings; + private final AnalysisWarningsPublisher underTest; + + public AnalysisWarningsPublisherTest() { + DefaultAnalysisWarnings defaultAnalysisWarnings = new DefaultAnalysisWarnings(mock(System2.class)); + this.analysisWarnings = defaultAnalysisWarnings; + this.underTest = new AnalysisWarningsPublisher(defaultAnalysisWarnings); + } + + @Test + public void publish_warnings() throws IOException { + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + + String warning1 = "warning 1"; + String warning2 = "warning 2"; + analysisWarnings.addUnique(warning1); + analysisWarnings.addUnique(warning1); + analysisWarnings.addUnique(warning2); + + underTest.publish(writer); + + ScannerReportReader reader = new ScannerReportReader(outputDir); +// List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings()); +// +// assertThat(warnings) +// .extracting(ScannerReport.AnalysisWarning::getText) +// .containsExactly(warning1, warning2); + } + + @Test + public void do_not_write_warnings_report_when_empty() throws IOException { + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + + underTest.publish(writer); + + assertThat(writer.getFileStructure().analysisWarnings()).doesNotExist(); + + ScannerReportReader reader = new ScannerReportReader(outputDir); +// List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings()); +// +// assertThat(warnings).isEmpty(); + } +} |