diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2020-09-23 12:12:44 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-09-29 20:07:42 +0000 |
commit | bc413ee3db73258a22059816bd4766bcea66a9ca (patch) | |
tree | ecddd838b51eb476390852e62d50ddd16fcf562b /server/sonar-ce-task-projectanalysis | |
parent | 27282ea04940d30eb33c20a02989a79d88dec4a0 (diff) | |
download | sonarqube-bc413ee3db73258a22059816bd4766bcea66a9ca.tar.gz sonarqube-bc413ee3db73258a22059816bd4766bcea66a9ca.zip |
SONAR-13869 Add a flag in telemetry to know if there's some c/c++ unanalyzed code
* SONAR-13869 Compute number of unanalyzed C and C++ files measures
* SONAR-13869 Send unanalyzed C and C++ flags in Telemetry
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.java (renamed from server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStep.java) | 45 | ||||
-rw-r--r-- | server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java | 4 | ||||
-rw-r--r-- | server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java (renamed from server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStepTest.java) | 54 |
3 files changed, 84 insertions, 19 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java index 67154fc4c74..d5c78acab54 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.ce.task.projectanalysis.language; import java.util.Iterator; import java.util.Map; @@ -27,18 +27,27 @@ import java.util.stream.Collectors; import org.sonar.api.utils.System2; import org.sonar.ce.task.log.CeTaskMessages; import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; +import org.sonar.ce.task.projectanalysis.component.Component; +import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; +import org.sonar.ce.task.projectanalysis.measure.Measure; +import org.sonar.ce.task.projectanalysis.measure.MeasureRepository; +import org.sonar.ce.task.projectanalysis.metric.Metric; +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 static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static java.lang.String.format; +import static org.sonar.core.language.UnanalyzedLanguages.C; +import static org.sonar.core.language.UnanalyzedLanguages.CPP; +import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY; +import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY; /** * Check if there are files that could be analyzed with a higher SQ edition. */ -public class PerformNotAnalyzedFilesCheckStep implements ComputationStep { +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 " + @@ -49,13 +58,21 @@ public class PerformNotAnalyzedFilesCheckStep implements ComputationStep { private final CeTaskMessages ceTaskMessages; private final PlatformEditionProvider editionProvider; private final System2 system; + private final TreeRootHolder treeRootHolder; + private final MeasureRepository measureRepository; + private final Metric unanalyzedCMetric; + private final Metric unanalyzedCppMetric; - public PerformNotAnalyzedFilesCheckStep(BatchReportReader reportReader, CeTaskMessages ceTaskMessages, PlatformEditionProvider editionProvider, - System2 system) { + public HandleUnanalyzedLanguagesStep(BatchReportReader reportReader, CeTaskMessages ceTaskMessages, PlatformEditionProvider editionProvider, + System2 system, TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { this.reportReader = reportReader; this.ceTaskMessages = ceTaskMessages; this.editionProvider = editionProvider; this.system = system; + this.treeRootHolder = treeRootHolder; + this.measureRepository = measureRepository; + this.unanalyzedCMetric = metricRepository.getByKey(UNANALYZED_C_KEY); + this.unanalyzedCppMetric = metricRepository.getByKey(UNANALYZED_CPP_KEY); } @Override @@ -76,13 +93,11 @@ public class PerformNotAnalyzedFilesCheckStep implements ComputationStep { } ceTaskMessages.add(constructMessage(filesPerLanguage)); + computeMeasures(filesPerLanguage); }); } private CeTaskMessages.Message constructMessage(Map<String, Integer> filesPerLanguage) { - checkNotNull(filesPerLanguage); - checkArgument(filesPerLanguage.size() > 0); - SortedMap<String, Integer> sortedLanguageMap = new TreeMap<>(filesPerLanguage); Iterator<Map.Entry<String, Integer>> iterator = sortedLanguageMap.entrySet().iterator(); Map.Entry<String, Integer> firstLanguage = iterator.next(); @@ -104,6 +119,18 @@ public class PerformNotAnalyzedFilesCheckStep implements ComputationStep { return new CeTaskMessages.Message(format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel), system.now(), true); } + private void computeMeasures(Map<String, Integer> filesPerLanguage) { + Component project = treeRootHolder.getRoot(); + Integer unanalyzedCFiles = filesPerLanguage.getOrDefault(C.toString(), 0); + if (unanalyzedCFiles > 0) { + measureRepository.add(project, unanalyzedCMetric, Measure.newMeasureBuilder().create(unanalyzedCFiles)); + } + Integer unanalyzedCppFiles = filesPerLanguage.getOrDefault(CPP.toString(), 0); + if (unanalyzedCppFiles > 0) { + measureRepository.add(project, unanalyzedCppMetric, Measure.newMeasureBuilder().create(unanalyzedCppFiles)); + } + } + @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 cd708269303..1860cec78f4 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 @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.List; import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.projectanalysis.filemove.FileMoveDetectionStep; +import org.sonar.ce.task.projectanalysis.language.HandleUnanalyzedLanguagesStep; import org.sonar.ce.task.projectanalysis.measure.PostMeasuresComputationChecksStep; import org.sonar.ce.task.projectanalysis.purge.PurgeDatastoresStep; import org.sonar.ce.task.projectanalysis.qualityprofile.RegisterQualityProfileStatusStep; @@ -38,7 +39,6 @@ public class ReportComputationSteps extends AbstractComputationSteps { private static final List<Class<? extends ComputationStep>> STEPS = Arrays.asList( ExtractReportStep.class, PersistScannerContextStep.class, - PerformNotAnalyzedFilesCheckStep.class, PersistAnalysisWarningsStep.class, GenerateAnalysisUuid.class, @@ -87,6 +87,8 @@ public class ReportComputationSteps extends AbstractComputationSteps { // Must be executed after computation of quality gate measure QualityGateEventsStep.class, + HandleUnanalyzedLanguagesStep.class, + // Persist data PersistComponentsStep.class, PersistAnalysisStep.class, diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java index ef05b274ed6..6aba65c5494 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PerformNotAnalyzedFilesCheckStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.ce.task.projectanalysis.language; import com.google.common.collect.ImmutableList; import java.util.List; @@ -29,6 +29,11 @@ import org.sonar.api.utils.System2; import org.sonar.ce.task.log.CeTaskMessages; import org.sonar.ce.task.log.CeTaskMessages.Message; import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule; +import org.sonar.ce.task.projectanalysis.component.Component; +import org.sonar.ce.task.projectanalysis.component.ReportComponent; +import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule; +import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule; +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; @@ -43,23 +48,41 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; +import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C; +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; -public class PerformNotAnalyzedFilesCheckStepTest { +public class HandleUnanalyzedLanguagesStepTest { + + private static final int PROJECT_REF = 1; + private static final Component ROOT_PROJECT = ReportComponent.builder(PROJECT, PROJECT_REF).build(); @Rule public BatchReportReaderRule reportReader = new BatchReportReaderRule(); + @Rule + public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(ROOT_PROJECT); + @Rule + public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + .add(UNANALYZED_C) + .add(UNANALYZED_CPP); + @Rule + public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class); - private final PerformNotAnalyzedFilesCheckStep underTest = new PerformNotAnalyzedFilesCheckStep(reportReader, ceTaskMessages, editionProvider, System2.INSTANCE); + + private final HandleUnanalyzedLanguagesStep underTest = new HandleUnanalyzedLanguagesStep(reportReader, ceTaskMessages, editionProvider, System2.INSTANCE, treeRootHolder, + metricRepository, measureRepository); @Test public void getDescription() { - assertThat(underTest.getDescription()).isEqualTo(PerformNotAnalyzedFilesCheckStep.DESCRIPTION); + assertThat(underTest.getDescription()).isEqualTo(HandleUnanalyzedLanguagesStep.DESCRIPTION); } @Test - public void execute_adds_warning_in_SQ_community_edition_if_there_are_c_or_cpp_files() { + public void add_warning_and_measures_in_SQ_community_edition_if_there_are_c_or_cpp_files() { when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build(); ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build(); @@ -82,10 +105,12 @@ public class PerformNotAnalyzedFilesCheckStepTest { "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)); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(20); } @Test - public void execute_adds_warning_in_SQ_community_edition_if_there_are_c_files() { + public void adds_warning_and_measures_in_SQ_community_edition_if_there_are_c_files() { when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); reportReader.setMetadata(ScannerReport.Metadata.newBuilder() .putNotAnalyzedFilesByLanguage("C", 10) @@ -100,10 +125,12 @@ public class PerformNotAnalyzedFilesCheckStepTest { "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."); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty(); } @Test - public void execute_adds_warning_in_SQ_community_edition_if_there_are_cpp_files() { + 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) @@ -118,10 +145,12 @@ public class PerformNotAnalyzedFilesCheckStepTest { "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); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty(); } @Test - public void execute_does_not_add_a_warning_in_SQ_community_edition_if_cpp_files_in_report_is_zero() { + public void do_nothing_SQ_community_edition_if_cpp_files_in_report_is_zero() { when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build(); ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build(); @@ -132,10 +161,13 @@ public class PerformNotAnalyzedFilesCheckStepTest { underTest.execute(new TestComputationStepContext()); verify(ceTaskMessages, never()).add(any()); + + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty(); } @Test - public void execute_does_not_add_a_warning_in_SQ_community_edition_if_no_c_or_cpp_files_2() { + public void execute_does_not_add_a_warning_in_SQ_community_edition_if_no_c_or_cpp_files() { when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build(); ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build(); @@ -146,6 +178,8 @@ public class PerformNotAnalyzedFilesCheckStepTest { underTest.execute(new TestComputationStepContext()); verify(ceTaskMessages, never()).add(any()); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty(); } @Test @@ -160,5 +194,7 @@ public class PerformNotAnalyzedFilesCheckStepTest { underTest.execute(new TestComputationStepContext()); verify(ceTaskMessages, never()).add(any()); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty(); } } |