3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.language;
22 import java.util.Iterator;
24 import java.util.SortedMap;
25 import java.util.TreeMap;
26 import java.util.stream.Collectors;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.task.log.CeTaskMessages;
29 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.ce.task.projectanalysis.measure.Measure;
33 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
34 import org.sonar.ce.task.projectanalysis.metric.Metric;
35 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
36 import org.sonar.ce.task.step.ComputationStep;
37 import org.sonar.core.platform.EditionProvider;
38 import org.sonar.core.platform.PlatformEditionProvider;
39 import org.sonar.db.ce.CeTaskMessageType;
41 import static java.lang.String.format;
42 import static org.sonar.core.language.UnanalyzedLanguages.C;
43 import static org.sonar.core.language.UnanalyzedLanguages.CPP;
44 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
45 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
48 * Check if there are files that could be analyzed with a higher SQ edition.
50 public class HandleUnanalyzedLanguagesStep implements ComputationStep {
52 static final String DESCRIPTION = "Check upgrade possibility for not analyzed code files.";
54 private static final String LANGUAGE_UPGRADE_MESSAGE = "%s detected in this project during the last analysis. %s cannot be analyzed with your" +
55 " current SonarQube edition. Please consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to" +
56 " Developer Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in %s.";
58 private final BatchReportReader reportReader;
59 private final CeTaskMessages ceTaskMessages;
60 private final PlatformEditionProvider editionProvider;
61 private final System2 system;
62 private final TreeRootHolder treeRootHolder;
63 private final MeasureRepository measureRepository;
64 private final Metric unanalyzedCMetric;
65 private final Metric unanalyzedCppMetric;
67 public HandleUnanalyzedLanguagesStep(BatchReportReader reportReader, CeTaskMessages ceTaskMessages, PlatformEditionProvider editionProvider,
68 System2 system, TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
69 this.reportReader = reportReader;
70 this.ceTaskMessages = ceTaskMessages;
71 this.editionProvider = editionProvider;
73 this.treeRootHolder = treeRootHolder;
74 this.measureRepository = measureRepository;
75 this.unanalyzedCMetric = metricRepository.getByKey(UNANALYZED_C_KEY);
76 this.unanalyzedCppMetric = metricRepository.getByKey(UNANALYZED_CPP_KEY);
80 public void execute(Context context) {
81 editionProvider.get().ifPresent(edition -> {
82 if (!edition.equals(EditionProvider.Edition.COMMUNITY)) {
86 Map<String, Integer> filesPerLanguage = reportReader.readMetadata().getNotAnalyzedFilesByLanguageMap()
89 .filter(entry -> entry.getValue() > 0)
90 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
92 if (filesPerLanguage.isEmpty()) {
96 ceTaskMessages.add(constructMessage(filesPerLanguage));
97 computeMeasures(filesPerLanguage);
101 private CeTaskMessages.Message constructMessage(Map<String, Integer> filesPerLanguage) {
102 SortedMap<String, Integer> sortedLanguageMap = new TreeMap<>(filesPerLanguage);
103 Iterator<Map.Entry<String, Integer>> iterator = sortedLanguageMap.entrySet().iterator();
104 Map.Entry<String, Integer> firstLanguage = iterator.next();
105 StringBuilder languageLabel = new StringBuilder(firstLanguage.getKey());
106 StringBuilder fileCountLabel = new StringBuilder(format("%s unanalyzed %s", firstLanguage.getValue(), firstLanguage.getKey()));
107 while (iterator.hasNext()) {
108 Map.Entry<String, Integer> nextLanguage = iterator.next();
109 if (iterator.hasNext()) {
110 languageLabel.append(", ");
111 fileCountLabel.append(", ");
113 languageLabel.append(" and ");
114 fileCountLabel.append(" and ");
116 languageLabel.append(nextLanguage.getKey());
117 fileCountLabel.append(format("%s unanalyzed %s", nextLanguage.getValue(), nextLanguage.getKey()));
120 if (sortedLanguageMap.size() == 1 && sortedLanguageMap.entrySet().iterator().next().getValue() == 1) {
121 fileCountLabel.append(" file was");
123 fileCountLabel.append(" files were");
126 String message = format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel, sortedLanguageMap.size() == 1 ? "this file" : "these files");
127 return new CeTaskMessages.Message(message, system.now(), CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
130 private void computeMeasures(Map<String, Integer> filesPerLanguage) {
131 Component project = treeRootHolder.getRoot();
132 Integer unanalyzedCFiles = filesPerLanguage.getOrDefault(C.toString(), 0);
133 if (unanalyzedCFiles > 0) {
134 measureRepository.add(project, unanalyzedCMetric, Measure.newMeasureBuilder().create(unanalyzedCFiles));
136 Integer unanalyzedCppFiles = filesPerLanguage.getOrDefault(CPP.toString(), 0);
137 if (unanalyzedCppFiles > 0) {
138 measureRepository.add(project, unanalyzedCppMetric, Measure.newMeasureBuilder().create(unanalyzedCppFiles));
143 public String getDescription() {