3 * Copyright (C) 2009-2024 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.issue;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.Optional;
25 import java.util.function.Supplier;
26 import javax.annotation.Nullable;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.utils.DateUtils;
29 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
30 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.ce.task.projectanalysis.analysis.ScannerPlugin;
32 import org.sonar.ce.task.projectanalysis.component.Component;
33 import org.sonar.ce.task.projectanalysis.filemove.AddedFileRepository;
34 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
35 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
36 import org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository;
37 import org.sonar.ce.task.projectanalysis.scm.Changeset;
38 import org.sonar.ce.task.projectanalysis.scm.ScmInfo;
39 import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepository;
40 import org.sonar.core.issue.DefaultIssue;
41 import org.sonar.core.issue.IssueChangeContext;
42 import org.sonar.server.issue.IssueFieldsSetter;
44 import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.UNCHANGED;
45 import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByScanBuilder;
48 * Calculates the creation date of an issue. Takes into account, that the issue
49 * might be raised by adding a rule to a quality profile.
51 public class IssueCreationDateCalculator extends IssueVisitor {
53 private final ScmInfoRepository scmInfoRepository;
54 private final IssueFieldsSetter issueUpdater;
55 private final AnalysisMetadataHolder analysisMetadataHolder;
56 private final IssueChangeContext changeContext;
57 private final ActiveRulesHolder activeRulesHolder;
58 private final RuleRepository ruleRepository;
59 private final AddedFileRepository addedFileRepository;
60 private QProfileStatusRepository qProfileStatusRepository;
62 public IssueCreationDateCalculator(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository,
63 IssueFieldsSetter issueUpdater, ActiveRulesHolder activeRulesHolder, RuleRepository ruleRepository,
64 AddedFileRepository addedFileRepository, QProfileStatusRepository qProfileStatusRepository) {
65 this.scmInfoRepository = scmInfoRepository;
66 this.issueUpdater = issueUpdater;
67 this.analysisMetadataHolder = analysisMetadataHolder;
68 this.ruleRepository = ruleRepository;
69 this.changeContext = issueChangeContextByScanBuilder(new Date(analysisMetadataHolder.getAnalysisDate())).build();
70 this.activeRulesHolder = activeRulesHolder;
71 this.addedFileRepository = addedFileRepository;
72 this.qProfileStatusRepository = qProfileStatusRepository;
76 public void onIssue(Component component, DefaultIssue issue) {
81 Optional<Long> lastAnalysisOptional = lastAnalysis();
82 boolean firstAnalysis = !lastAnalysisOptional.isPresent();
83 if (firstAnalysis || isNewFile(component)) {
84 backdateIssue(component, issue);
88 Rule rule = ruleRepository.findByKey(issue.getRuleKey())
89 .orElseThrow(illegalStateException("The rule with key '%s' raised an issue, but no rule with that key was found", issue.getRuleKey()));
90 if (rule.isExternal()) {
91 backdateIssue(component, issue);
93 // Rule can't be inactive (see contract of IssueVisitor)
94 ActiveRule activeRule = activeRulesHolder.get(issue.getRuleKey()).get();
95 if (activeRuleIsNewOrChanged(activeRule, lastAnalysisOptional.get())
96 || ruleImplementationChanged(activeRule.getRuleKey(), activeRule.getPluginKey(), lastAnalysisOptional.get())
97 || qualityProfileChanged(activeRule.getQProfileKey())) {
98 backdateIssue(component, issue);
103 private boolean qualityProfileChanged(String qpKey) {
104 return qProfileStatusRepository.get(qpKey).filter(s -> !s.equals(UNCHANGED)).isPresent();
107 private boolean isNewFile(Component component) {
108 return component.getType() == Component.Type.FILE && addedFileRepository.isAdded(component);
111 private void backdateIssue(Component component, DefaultIssue issue) {
112 getDateOfLatestChange(component, issue).ifPresent(changeDate -> updateDate(issue, changeDate));
115 private boolean ruleImplementationChanged(RuleKey ruleKey, @Nullable String pluginKey, long lastAnalysisDate) {
116 if (pluginKey == null) {
120 ScannerPlugin scannerPlugin = Optional.ofNullable(analysisMetadataHolder.getScannerPluginsByKey().get(pluginKey))
121 .orElseThrow(illegalStateException("The rule %s is declared to come from plugin %s, but this plugin was not used by scanner.", ruleKey, pluginKey));
122 return pluginIsNew(scannerPlugin, lastAnalysisDate)
123 || basePluginIsNew(scannerPlugin, lastAnalysisDate);
126 private boolean basePluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
127 String basePluginKey = scannerPlugin.getBasePluginKey();
128 if (basePluginKey == null) {
131 ScannerPlugin basePlugin = analysisMetadataHolder.getScannerPluginsByKey().get(basePluginKey);
132 return lastAnalysisDate < basePlugin.getUpdatedAt();
135 private static boolean pluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
136 return lastAnalysisDate < scannerPlugin.getUpdatedAt();
139 private static boolean activeRuleIsNewOrChanged(ActiveRule activeRule, Long lastAnalysisDate) {
140 return lastAnalysisDate < activeRule.getUpdatedAt();
143 private Optional<Date> getDateOfLatestChange(Component component, DefaultIssue issue) {
144 return getScmInfo(component)
145 .flatMap(scmInfo -> getLatestChangeset(component, scmInfo, issue))
146 .map(IssueCreationDateCalculator::getChangeDate);
149 private Optional<Long> lastAnalysis() {
150 return Optional.ofNullable(analysisMetadataHolder.getBaseAnalysis()).map(Analysis::getCreatedAt);
153 private Optional<ScmInfo> getScmInfo(Component component) {
154 return scmInfoRepository.getScmInfo(component);
157 private static Optional<Changeset> getLatestChangeset(Component component, ScmInfo scmInfo, DefaultIssue issue) {
158 Optional<Changeset> mostRecentChangeset = IssueLocations.allLinesFor(issue, component.getUuid())
159 .filter(scmInfo::hasChangesetForLine)
160 .mapToObj(scmInfo::getChangesetForLine)
161 .max(Comparator.comparingLong(Changeset::getDate));
162 if (mostRecentChangeset.isPresent()) {
163 return mostRecentChangeset;
165 return Optional.of(scmInfo.getLatestChangeset());
168 private static Date getChangeDate(Changeset changesetForLine) {
169 return DateUtils.longToDate(changesetForLine.getDate());
172 private void updateDate(DefaultIssue issue, Date scmDate) {
173 issueUpdater.setCreationDate(issue, scmDate, changeContext);
176 private static Supplier<? extends IllegalStateException> illegalStateException(String str, Object... args) {
177 return () -> new IllegalStateException(String.format(str, args));