aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task-projectanalysis
diff options
context:
space:
mode:
authorMichal Duda <michal.duda@sonarsource.com>2020-09-18 17:08:48 +0200
committersonartech <sonartech@sonarsource.com>2020-09-29 20:07:42 +0000
commit0ecd8610552ccd59049308c1be46b03470c48538 (patch)
treed1da42fc9e90627a037696702cb30dd0e7442e6d /server/sonar-ce-task-projectanalysis
parentbc413ee3db73258a22059816bd4766bcea66a9ca (diff)
downloadsonarqube-0ecd8610552ccd59049308c1be46b03470c48538.tar.gz
sonarqube-0ecd8610552ccd59049308c1be46b03470c48538.zip
SONAR-13862 Add a WS for dismissing analysis warning by the current user
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java20
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java38
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java13
3 files changed, 46 insertions, 25 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java
index d5c78acab54..bd29a24839f 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java
@@ -36,6 +36,7 @@ import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.core.platform.EditionProvider;
import org.sonar.core.platform.PlatformEditionProvider;
+import org.sonar.db.ce.CeTaskMessageType;
import static java.lang.String.format;
import static org.sonar.core.language.UnanalyzedLanguages.C;
@@ -50,9 +51,9 @@ public class HandleUnanalyzedLanguagesStep implements ComputationStep {
static final String DESCRIPTION = "Check upgrade possibility for not analyzed code files.";
- private static final String LANGUAGE_UPGRADE_MESSAGE = "%s file(s) detected during the last analysis. %s code cannot be analyzed with SonarQube " +
- "community edition. Please consider <a href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to " +
- "the Developer Edition</a> to analyze this language.";
+ private static final String LANGUAGE_UPGRADE_MESSAGE = "%s detected in this project during the last analysis. %s cannot be analyzed with your" +
+ " current SonarQube edition. Please consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to" +
+ " Developer Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in %s.";
private final BatchReportReader reportReader;
private final CeTaskMessages ceTaskMessages;
@@ -102,7 +103,7 @@ public class HandleUnanalyzedLanguagesStep implements ComputationStep {
Iterator<Map.Entry<String, Integer>> iterator = sortedLanguageMap.entrySet().iterator();
Map.Entry<String, Integer> firstLanguage = iterator.next();
StringBuilder languageLabel = new StringBuilder(firstLanguage.getKey());
- StringBuilder fileCountLabel = new StringBuilder(format("%s %s", firstLanguage.getValue(), firstLanguage.getKey()));
+ StringBuilder fileCountLabel = new StringBuilder(format("%s unanalyzed %s", firstLanguage.getValue(), firstLanguage.getKey()));
while (iterator.hasNext()) {
Map.Entry<String, Integer> nextLanguage = iterator.next();
if (iterator.hasNext()) {
@@ -113,10 +114,17 @@ public class HandleUnanalyzedLanguagesStep implements ComputationStep {
fileCountLabel.append(" and ");
}
languageLabel.append(nextLanguage.getKey());
- fileCountLabel.append(format("%s %s", nextLanguage.getValue(), nextLanguage.getKey()));
+ fileCountLabel.append(format("%s unanalyzed %s", nextLanguage.getValue(), nextLanguage.getKey()));
}
- return new CeTaskMessages.Message(format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel), system.now(), true);
+ if (sortedLanguageMap.size() == 1 && sortedLanguageMap.entrySet().iterator().next().getValue() == 1) {
+ fileCountLabel.append(" file was");
+ } else {
+ fileCountLabel.append(" files were");
+ }
+
+ String message = format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel, sortedLanguageMap.size() == 1 ? "this file" : "these files");
+ return new CeTaskMessages.Message(message, system.now(), CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
}
private void computeMeasures(Map<String, Integer> filesPerLanguage) {
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java
index 6aba65c5494..bc0726aac4b 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java
@@ -24,7 +24,10 @@ import java.util.List;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.junit.MockitoJUnitRunner;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.log.CeTaskMessages;
import org.sonar.ce.task.log.CeTaskMessages.Message;
@@ -37,6 +40,7 @@ import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.core.platform.EditionProvider;
import org.sonar.core.platform.PlatformEditionProvider;
+import org.sonar.db.ce.CeTaskMessageType;
import org.sonar.scanner.protocol.output.ScannerReport;
import static com.google.common.collect.ImmutableList.of;
@@ -54,6 +58,7 @@ import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP;
import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
+@RunWith(MockitoJUnitRunner.class)
public class HandleUnanalyzedLanguagesStepTest {
private static final int PROJECT_REF = 1;
@@ -70,6 +75,9 @@ public class HandleUnanalyzedLanguagesStepTest {
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
+ @Captor
+ private ArgumentCaptor<Message> argumentCaptor;
+
private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
@@ -93,18 +101,18 @@ public class HandleUnanalyzedLanguagesStepTest {
.putNotAnalyzedFilesByLanguage("C", 10)
.putNotAnalyzedFilesByLanguage("SomeLang", 1000)
.build());
- ArgumentCaptor<Message> argumentCaptor = ArgumentCaptor.forClass(Message.class);
underTest.execute(new TestComputationStepContext());
verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
assertThat(argumentCaptor.getAllValues())
- .extracting(Message::getText, Message::isDismissible)
+ .extracting(Message::getText, Message::getType)
.containsExactly(tuple(
- "10 C, 20 C++ and 1000 SomeLang file(s) detected during the last analysis. C, C++ and SomeLang code cannot be analyzed with SonarQube community " +
- "edition. Please consider <a href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to the Developer " +
- "Edition</a> to analyze this language.",
- true));
+ "10 unanalyzed C, 20 unanalyzed C++ and 1000 unanalyzed SomeLang files were detected in this project during the last analysis. C," +
+ " C++ and SomeLang cannot be analyzed with your current SonarQube edition. Please consider" +
+ " <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer Edition</a> to find Bugs," +
+ " Code Smells, Vulnerabilities and Security Hotspots in these files.",
+ CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE));
assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10);
assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(20);
}
@@ -115,16 +123,15 @@ public class HandleUnanalyzedLanguagesStepTest {
reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
.putNotAnalyzedFilesByLanguage("C", 10)
.build());
- ArgumentCaptor<CeTaskMessages.Message> argumentCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
underTest.execute(new TestComputationStepContext());
verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
List<CeTaskMessages.Message> messages = argumentCaptor.getAllValues();
assertThat(messages).extracting(CeTaskMessages.Message::getText).containsExactly(
- "10 C file(s) detected during the last analysis. C code cannot be analyzed with SonarQube community " +
- "edition. Please consider <a href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to the Developer " +
- "Edition</a> to analyze this language.");
+ "10 unanalyzed C files were detected in this project during the last analysis. C cannot be analyzed with your current SonarQube edition. Please" +
+ " consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer" +
+ " Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in this file.");
assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10);
assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty();
}
@@ -133,19 +140,18 @@ public class HandleUnanalyzedLanguagesStepTest {
public void adds_warning_in_SQ_community_edition_if_there_are_cpp_files() {
when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
- .putNotAnalyzedFilesByLanguage("C++", 9)
+ .putNotAnalyzedFilesByLanguage("C++", 1)
.build());
- ArgumentCaptor<CeTaskMessages.Message> argumentCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
underTest.execute(new TestComputationStepContext());
verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
List<CeTaskMessages.Message> messages = argumentCaptor.getAllValues();
assertThat(messages).extracting(CeTaskMessages.Message::getText).containsExactly(
- "9 C++ file(s) detected during the last analysis. C++ code cannot be analyzed with SonarQube community " +
- "edition. Please consider <a href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to the Developer " +
- "Edition</a> to analyze this language.");
- assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(9);
+ "1 unanalyzed C++ file was detected in this project during the last analysis. C++ cannot be analyzed with your current SonarQube edition. Please" +
+ " consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer" +
+ " Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in this file.");
+ assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(1);
assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty();
}
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
index 0024c611644..8f5224a0640 100644
--- 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
@@ -23,10 +23,14 @@ import com.google.common.collect.ImmutableList;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.junit.MockitoJUnitRunner;
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.db.ce.CeTaskMessageType;
import org.sonar.scanner.protocol.output.ScannerReport;
import static com.google.common.collect.ImmutableList.of;
@@ -38,11 +42,15 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
+@RunWith(MockitoJUnitRunner.class)
public class PersistAnalysisWarningsStepTest {
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+ @Captor
+ private ArgumentCaptor<List<CeTaskMessages.Message>> argumentCaptor;
+
private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
private final PersistAnalysisWarningsStep underTest = new PersistAnalysisWarningsStep(reportReader, ceTaskMessages);
@@ -57,14 +65,13 @@ public class PersistAnalysisWarningsStepTest {
ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
reportReader.setAnalysisWarnings(warnings);
- ArgumentCaptor<List<CeTaskMessages.Message>> argumentCaptor = ArgumentCaptor.forClass(List.class);
underTest.execute(new TestComputationStepContext());
verify(ceTaskMessages, times(1)).addAll(argumentCaptor.capture());
assertThat(argumentCaptor.getValue())
- .extracting(CeTaskMessages.Message::getText, CeTaskMessages.Message::isDismissible)
- .containsExactly(tuple("warning 1", false), tuple("warning 2", false));
+ .extracting(CeTaskMessages.Message::getText, CeTaskMessages.Message::getType)
+ .containsExactly(tuple("warning 1", CeTaskMessageType.GENERIC), tuple("warning 2", CeTaskMessageType.GENERIC));
}
@Test