diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2018-09-11 18:01:57 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2018-10-10 09:23:04 +0200 |
commit | 9d3bcaf129a89e1fd11e47ef8ce2a357ef7e81d6 (patch) | |
tree | dd37a9e066c1d8c688176829cdd78a929aa64a27 | |
parent | 8b06219ef58e8eb5382ff1310747632e2c2d366e (diff) | |
download | sonarqube-9d3bcaf129a89e1fd11e47ef8ce2a357ef7e81d6.tar.gz sonarqube-9d3bcaf129a89e1fd11e47ef8ce2a357ef7e81d6.zip |
SONAR-11241 Persist analysis warnings from scanner report
9 files changed, 186 insertions, 10 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReader.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReader.java index a8e82f3d65f..7f1d0313a3d 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReader.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReader.java @@ -68,4 +68,6 @@ public interface BatchReportReader { Optional<CloseableIterator<ScannerReport.LineSgnificantCode>> readComponentSignificantCode(int fileRef); Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef); + + CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImpl.java index 3c324d7a8ed..819e44f8d3f 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImpl.java @@ -274,7 +274,15 @@ public class BatchReportReaderImpl implements BatchReportReader { return Optional.ofNullable(delegate.readComponentSignificantCode(fileRef)); } - @Override public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) { + @Override + public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) { + ensureInitialized(); return Optional.ofNullable(delegate.readComponentChangedLines(fileRef)); } + + @Override + public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() { + ensureInitialized(); + return delegate.readAnalysisWarnings(); + } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStep.java new file mode 100644 index 00000000000..96228e42de6 --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStep.java @@ -0,0 +1,56 @@ +/* + * 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.ce.task.projectanalysis.step; + +import java.util.ArrayList; +import java.util.Collection; +import org.sonar.ce.task.log.CeTaskMessages; +import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; +import org.sonar.ce.task.step.ComputationStep; + +/** + * Propagate analysis warnings from scanner report. + */ +public class PersistAnalysisWarningsStep implements ComputationStep { + + static final String DESCRIPTION = "Propagate analysis warnings from scanner report"; + + private final BatchReportReader reportReader; + private final CeTaskMessages ceTaskMessages; + + public PersistAnalysisWarningsStep(BatchReportReader reportReader, CeTaskMessages ceTaskMessages) { + this.reportReader = reportReader; + this.ceTaskMessages = ceTaskMessages; + } + + @Override + public void execute(Context context) { + Collection<CeTaskMessages.Message> warnings = new ArrayList<>(); + reportReader.readAnalysisWarnings().forEachRemaining(w -> warnings.add(new CeTaskMessages.Message(w.getText(), w.getTimestamp()))); + if (!warnings.isEmpty()) { + ceTaskMessages.addAll(warnings); + } + } + + @Override + public String getDescription() { + return DESCRIPTION; + } +} diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java index 36785903a8a..dff4aabc133 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java @@ -37,6 +37,7 @@ public class ReportComputationSteps extends AbstractComputationSteps { private static final List<Class<? extends ComputationStep>> STEPS = Arrays.asList( ExtractReportStep.class, PersistScannerContextStep.class, + PersistAnalysisWarningsStep.class, DbMigrationsStep.class, GenerateAnalysisUuid.class, diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImplTest.java index 38c103d23da..5bde3b46ffd 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderImplTest.java @@ -19,6 +19,7 @@ */ package org.sonar.ce.task.projectanalysis.batch; +import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; @@ -314,4 +315,16 @@ public class BatchReportReaderImplTest { assertThat(res).containsExactly(COVERAGE_DETAIL_1, COVERAGE_DETAIL_2); res.close(); } + + @Test + public void verify_readAnalysisWarnings() { + ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build(); + ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build(); + ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2); + writer.writeAnalysisWarnings(warnings); + + CloseableIterator<ScannerReport.AnalysisWarning> res = underTest.readAnalysisWarnings(); + assertThat(res).containsExactlyElementsOf(warnings); + res.close(); + } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderRule.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderRule.java index 06a0b1e1594..e1ba42e0aae 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderRule.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/batch/BatchReportReaderRule.java @@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.batch; import com.google.common.base.Preconditions; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -57,6 +58,7 @@ public class BatchReportReaderRule implements TestRule, BatchReportReader { private Map<Integer, List<ScannerReport.CoverageDetail>> coverageDetails = new HashMap<>(); private Map<Integer, List<ScannerReport.LineSgnificantCode>> significantCode = new HashMap<>(); private Map<Integer, ScannerReport.ChangedLines> changedLines = new HashMap<>(); + private List<ScannerReport.AnalysisWarning> analysisWarnings = Collections.emptyList(); @Override public Statement apply(final Statement statement, Description description) { @@ -250,11 +252,22 @@ public class BatchReportReaderRule implements TestRule, BatchReportReader { return this; } - @Override public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) { + @Override + public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) { return Optional.ofNullable(changedLines.get(fileRef)); } @Override + public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() { + return closeableIterator(analysisWarnings); + } + + public BatchReportReaderRule setAnalysisWarnings(List<ScannerReport.AnalysisWarning> analysisWarnings) { + this.analysisWarnings = new ArrayList<>(analysisWarnings); + return this; + } + + @Override public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) { return closeableIterator(this.syntaxHighlightings.get(fileRef)); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java new file mode 100644 index 00000000000..29868466c16 --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java @@ -0,0 +1,75 @@ +/* + * 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.ce.task.projectanalysis.step; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.ce.task.log.CeTaskMessages; +import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule; +import org.sonar.ce.task.step.TestComputationStepContext; +import org.sonar.scanner.protocol.output.ScannerReport; + +import static com.google.common.collect.ImmutableList.of; +import static java.util.Collections.emptyList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +public class PersistAnalysisWarningsStepTest { + + @Rule + public BatchReportReaderRule reportReader = new BatchReportReaderRule(); + + private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class); + private final PersistAnalysisWarningsStep underTest = new PersistAnalysisWarningsStep(reportReader, ceTaskMessages); + + @Test + public void getDescription() { + assertThat(underTest.getDescription()).isEqualTo(PersistAnalysisWarningsStep.DESCRIPTION); + } + + @Test + public void execute_persists_warnings_from_reportReader() { + ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build(); + ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build(); + ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2); + reportReader.setAnalysisWarnings(warnings); + + underTest.execute(new TestComputationStepContext()); + + List<CeTaskMessages.Message> messages = warnings.stream() + .map(w -> new CeTaskMessages.Message(w.getText(), w.getTimestamp())) + .collect(Collectors.toList()); + verify(ceTaskMessages).addAll(messages); + } + + @Test + public void execute_does_not_persist_warnings_from_reportReader_when_empty() { + reportReader.setScannerLogs(emptyList()); + + underTest.execute(new TestComputationStepContext()); + + verifyZeroInteractions(ceTaskMessages); + } +} 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 index 4d3d0f895dd..897f17b0716 100644 --- 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 @@ -64,11 +64,11 @@ public class AnalysisWarningsPublisherTest { 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); + List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings()); + + assertThat(warnings) + .extracting(ScannerReport.AnalysisWarning::getText) + .containsExactly(warning1, warning2); } @Test @@ -81,8 +81,8 @@ public class AnalysisWarningsPublisherTest { assertThat(writer.getFileStructure().analysisWarnings()).doesNotExist(); ScannerReportReader reader = new ScannerReportReader(outputDir); -// List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings()); -// -// assertThat(warnings).isEmpty(); + List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings()); + + assertThat(warnings).isEmpty(); } } 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 c52726e48c1..fa590ae9085 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 @@ -209,6 +209,14 @@ public class ScannerReportReader { return Protobuf.readStream(file, ScannerReport.ContextProperty.parser()); } + public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() { + File file = fileStructure.analysisWarnings(); + if (!fileExists(file)) { + return emptyCloseableIterator(); + } + return Protobuf.readStream(file, ScannerReport.AnalysisWarning.parser()); + } + private static boolean fileExists(File file) { return file.exists() && file.isFile(); } |