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.

QualityProfileEventsStep.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.step;
  21. import com.google.common.collect.ImmutableSortedMap;
  22. import java.util.Collections;
  23. import java.util.Date;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import javax.annotation.Nullable;
  27. import org.apache.commons.lang3.time.DateUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.api.measures.CoreMetrics;
  31. import org.sonar.api.resources.Language;
  32. import org.sonar.api.utils.KeyValueFormat;
  33. import org.sonar.ce.task.projectanalysis.component.Component;
  34. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  35. import org.sonar.ce.task.projectanalysis.event.Event;
  36. import org.sonar.ce.task.projectanalysis.event.EventRepository;
  37. import org.sonar.ce.task.projectanalysis.language.LanguageRepository;
  38. import org.sonar.ce.task.projectanalysis.measure.Measure;
  39. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  40. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  41. import org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository;
  42. import org.sonar.ce.task.projectanalysis.qualityprofile.QualityProfileRuleChangeResolver;
  43. import org.sonar.ce.task.projectanalysis.qualityprofile.QualityProfileTextGenerator;
  44. import org.sonar.ce.task.step.ComputationStep;
  45. import org.sonar.core.util.UtcDateUtils;
  46. import org.sonar.server.qualityprofile.ActiveRuleChange;
  47. import org.sonar.server.qualityprofile.QPMeasureData;
  48. import org.sonar.server.qualityprofile.QualityProfile;
  49. import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.ADDED;
  50. import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.REMOVED;
  51. import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.UPDATED;
  52. /**
  53. * Computation of quality profile events
  54. * As it depends upon {@link CoreMetrics#QUALITY_PROFILES_KEY}, it must be executed after {@link ComputeQProfileMeasureStep}
  55. */
  56. public class QualityProfileEventsStep implements ComputationStep {
  57. private static final Logger LOG = LoggerFactory.getLogger(QualityProfileEventsStep.class);
  58. private final TreeRootHolder treeRootHolder;
  59. private final MetricRepository metricRepository;
  60. private final MeasureRepository measureRepository;
  61. private final EventRepository eventRepository;
  62. private final LanguageRepository languageRepository;
  63. private final QProfileStatusRepository qProfileStatusRepository;
  64. private final QualityProfileRuleChangeResolver qualityProfileRuleChangeTextResolver;
  65. public QualityProfileEventsStep(TreeRootHolder treeRootHolder,
  66. MetricRepository metricRepository, MeasureRepository measureRepository, LanguageRepository languageRepository,
  67. EventRepository eventRepository, QProfileStatusRepository qProfileStatusRepository, QualityProfileRuleChangeResolver qualityProfileRuleChangeTextResolver) {
  68. this.treeRootHolder = treeRootHolder;
  69. this.metricRepository = metricRepository;
  70. this.measureRepository = measureRepository;
  71. this.eventRepository = eventRepository;
  72. this.languageRepository = languageRepository;
  73. this.qProfileStatusRepository = qProfileStatusRepository;
  74. this.qualityProfileRuleChangeTextResolver = qualityProfileRuleChangeTextResolver;
  75. }
  76. @Override
  77. public void execute(ComputationStep.Context context) {
  78. executeForBranch(treeRootHolder.getRoot());
  79. }
  80. private void executeForBranch(Component branchComponent) {
  81. Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
  82. if (baseMeasure.isEmpty()) {
  83. // first analysis -> do not generate events
  84. return;
  85. }
  86. // Load profiles used in current analysis for which at least one file of the corresponding language exists
  87. Optional<Measure> rawMeasure = measureRepository.getRawMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
  88. if (rawMeasure.isEmpty()) {
  89. // No qualify profile computed on the project
  90. return;
  91. }
  92. Map<String, QualityProfile> rawProfiles = QPMeasureData.fromJson(rawMeasure.get().getStringValue()).getProfilesByKey();
  93. Map<String, QualityProfile> baseProfiles = parseJsonData(baseMeasure.get());
  94. detectNewOrUpdatedProfiles(baseProfiles, rawProfiles, branchComponent.getUuid());
  95. detectNoMoreUsedProfiles(baseProfiles);
  96. }
  97. private static Map<String, QualityProfile> parseJsonData(Measure measure) {
  98. String data = measure.getStringValue();
  99. if (data == null) {
  100. return Collections.emptyMap();
  101. }
  102. return QPMeasureData.fromJson(data).getProfilesByKey();
  103. }
  104. private void detectNoMoreUsedProfiles(Map<String, QualityProfile> baseProfiles) {
  105. for (QualityProfile baseProfile : baseProfiles.values()) {
  106. if (qProfileStatusRepository.get(baseProfile.getQpKey()).filter(REMOVED::equals).isPresent()) {
  107. markAsRemoved(baseProfile);
  108. }
  109. }
  110. }
  111. private void detectNewOrUpdatedProfiles(Map<String, QualityProfile> baseProfiles, Map<String, QualityProfile> rawProfiles, String componentUuid) {
  112. for (QualityProfile profile : rawProfiles.values()) {
  113. qProfileStatusRepository.get(profile.getQpKey()).ifPresent(status -> {
  114. if (status.equals(ADDED)) {
  115. markAsAdded(profile);
  116. } else if (status.equals(UPDATED)) {
  117. markAsChanged(baseProfiles.get(profile.getQpKey()), profile, componentUuid);
  118. }
  119. });
  120. }
  121. }
  122. private void markAsChanged(QualityProfile baseProfile, QualityProfile profile, String componentUuid) {
  123. try {
  124. Map<ActiveRuleChange.Type, Long> changesMappedToNumberOfRules = qualityProfileRuleChangeTextResolver.mapChangeToNumberOfRules(baseProfile, componentUuid);
  125. if (changesMappedToNumberOfRules.isEmpty()) {
  126. LOG.debug("No changes found for Quality Profile {}. Quality Profile event skipped.", profile.getQpKey());
  127. return;
  128. }
  129. String data = KeyValueFormat.format(ImmutableSortedMap.of(
  130. "key", profile.getQpKey(),
  131. "from", UtcDateUtils.formatDateTime(fixDate(baseProfile.getRulesUpdatedAt())),
  132. "to", UtcDateUtils.formatDateTime(fixDate(profile.getRulesUpdatedAt())),
  133. "name", profile.getQpName(),
  134. "languageKey", profile.getLanguageKey()));
  135. String ruleChangeText = QualityProfileTextGenerator.generateRuleChangeText(changesMappedToNumberOfRules);
  136. eventRepository.add(createQProfileEvent(profile, "%s updated with " + ruleChangeText, data, ruleChangeText));
  137. } catch (Exception e) {
  138. LOG.error("Failed to generate 'change' event for Quality Profile " + profile.getQpKey(), e);
  139. }
  140. }
  141. private void markAsRemoved(QualityProfile profile) {
  142. eventRepository.add(createQProfileEvent(profile, "Stop using %s"));
  143. }
  144. private void markAsAdded(QualityProfile profile) {
  145. eventRepository.add(createQProfileEvent(profile, "Use %s"));
  146. }
  147. private Event createQProfileEvent(QualityProfile profile, String namePattern) {
  148. return createQProfileEvent(profile, namePattern, null);
  149. }
  150. private Event createQProfileEvent(QualityProfile profile, String namePattern, @Nullable String data) {
  151. return Event.createProfile(String.format(namePattern, profileLabel(profile)), data, null);
  152. }
  153. private Event createQProfileEvent(QualityProfile profile, String namePattern, @Nullable String data, @Nullable String description) {
  154. return Event.createProfile(String.format(namePattern, profileLabel(profile)), data, description);
  155. }
  156. private String profileLabel(QualityProfile profile) {
  157. Optional<Language> language = languageRepository.find(profile.getLanguageKey());
  158. String languageName = language.isPresent() ? language.get().getName() : profile.getLanguageKey();
  159. return String.format("\"%s\" (%s)", profile.getQpName(), languageName);
  160. }
  161. /**
  162. * This hack must be done because date precision is millisecond in db/es and date format is select only
  163. */
  164. private static Date fixDate(Date date) {
  165. return DateUtils.addSeconds(date, 1);
  166. }
  167. @Override
  168. public String getDescription() {
  169. return "Generate Quality profile events";
  170. }
  171. }