You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HandleUnanalyzedLanguagesStep.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.language;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import java.util.SortedMap;
  24. import java.util.TreeMap;
  25. import java.util.stream.Collectors;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.ce.task.log.CeTaskMessages;
  28. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  29. import org.sonar.ce.task.projectanalysis.component.Component;
  30. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  31. import org.sonar.ce.task.projectanalysis.measure.Measure;
  32. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  33. import org.sonar.ce.task.projectanalysis.metric.Metric;
  34. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  35. import org.sonar.ce.task.step.ComputationStep;
  36. import org.sonar.core.platform.EditionProvider;
  37. import org.sonar.core.platform.PlatformEditionProvider;
  38. import org.sonar.db.dismissmessage.MessageType;
  39. import static java.lang.String.format;
  40. import static org.sonar.core.language.UnanalyzedLanguages.C;
  41. import static org.sonar.core.language.UnanalyzedLanguages.CPP;
  42. import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
  43. import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
  44. /**
  45. * Check if there are files that could be analyzed with a higher SQ edition.
  46. */
  47. public class HandleUnanalyzedLanguagesStep implements ComputationStep {
  48. static final String DESCRIPTION = "Check upgrade possibility for not analyzed code files.";
  49. private static final String LANGUAGE_UPGRADE_MESSAGE = "%s detected in this project during the last analysis. %s cannot be analyzed with your" +
  50. " current SonarQube edition. Please consider <a target=\"_blank\" href=\"https://www.sonarsource.com/plans-and-pricing/developer/?referrer=sonarqube-cpp\">upgrading to" +
  51. " Developer Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in %s.";
  52. private final BatchReportReader reportReader;
  53. private final CeTaskMessages ceTaskMessages;
  54. private final PlatformEditionProvider editionProvider;
  55. private final System2 system;
  56. private final TreeRootHolder treeRootHolder;
  57. private final MeasureRepository measureRepository;
  58. private final Metric unanalyzedCMetric;
  59. private final Metric unanalyzedCppMetric;
  60. public HandleUnanalyzedLanguagesStep(BatchReportReader reportReader, CeTaskMessages ceTaskMessages, PlatformEditionProvider editionProvider,
  61. System2 system, TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
  62. this.reportReader = reportReader;
  63. this.ceTaskMessages = ceTaskMessages;
  64. this.editionProvider = editionProvider;
  65. this.system = system;
  66. this.treeRootHolder = treeRootHolder;
  67. this.measureRepository = measureRepository;
  68. this.unanalyzedCMetric = metricRepository.getByKey(UNANALYZED_C_KEY);
  69. this.unanalyzedCppMetric = metricRepository.getByKey(UNANALYZED_CPP_KEY);
  70. }
  71. @Override
  72. public void execute(Context context) {
  73. editionProvider.get().ifPresent(edition -> {
  74. if (!edition.equals(EditionProvider.Edition.COMMUNITY)) {
  75. return;
  76. }
  77. Map<String, Integer> filesPerLanguage = reportReader.readMetadata().getNotAnalyzedFilesByLanguageMap()
  78. .entrySet()
  79. .stream()
  80. .filter(entry -> entry.getValue() > 0)
  81. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  82. if (filesPerLanguage.isEmpty()) {
  83. return;
  84. }
  85. ceTaskMessages.add(constructMessage(filesPerLanguage));
  86. computeMeasures(filesPerLanguage);
  87. });
  88. }
  89. private CeTaskMessages.Message constructMessage(Map<String, Integer> filesPerLanguage) {
  90. SortedMap<String, Integer> sortedLanguageMap = new TreeMap<>(filesPerLanguage);
  91. Iterator<Map.Entry<String, Integer>> iterator = sortedLanguageMap.entrySet().iterator();
  92. Map.Entry<String, Integer> firstLanguage = iterator.next();
  93. StringBuilder languageLabel = new StringBuilder(firstLanguage.getKey());
  94. StringBuilder fileCountLabel = new StringBuilder(format("%s unanalyzed %s", firstLanguage.getValue(), firstLanguage.getKey()));
  95. while (iterator.hasNext()) {
  96. Map.Entry<String, Integer> nextLanguage = iterator.next();
  97. if (iterator.hasNext()) {
  98. languageLabel.append(", ");
  99. fileCountLabel.append(", ");
  100. } else {
  101. languageLabel.append(" and ");
  102. fileCountLabel.append(" and ");
  103. }
  104. languageLabel.append(nextLanguage.getKey());
  105. fileCountLabel.append(format("%s unanalyzed %s", nextLanguage.getValue(), nextLanguage.getKey()));
  106. }
  107. if (sortedLanguageMap.size() == 1 && sortedLanguageMap.entrySet().iterator().next().getValue() == 1) {
  108. fileCountLabel.append(" file was");
  109. } else {
  110. fileCountLabel.append(" files were");
  111. }
  112. String message = format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel, sortedLanguageMap.size() == 1 ? "this file" : "these files");
  113. return new CeTaskMessages.Message(message, system.now(), MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
  114. }
  115. private void computeMeasures(Map<String, Integer> filesPerLanguage) {
  116. Component project = treeRootHolder.getRoot();
  117. Integer unanalyzedCFiles = filesPerLanguage.getOrDefault(C.toString(), 0);
  118. if (unanalyzedCFiles > 0) {
  119. measureRepository.add(project, unanalyzedCMetric, Measure.newMeasureBuilder().create(unanalyzedCFiles));
  120. }
  121. Integer unanalyzedCppFiles = filesPerLanguage.getOrDefault(CPP.toString(), 0);
  122. if (unanalyzedCppFiles > 0) {
  123. measureRepository.add(project, unanalyzedCppMetric, Measure.newMeasureBuilder().create(unanalyzedCppFiles));
  124. }
  125. }
  126. @Override
  127. public String getDescription() {
  128. return DESCRIPTION;
  129. }
  130. }