3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import java.time.format.DateTimeFormatter;
23 import java.util.Comparator;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.Objects;
27 import java.util.Optional;
29 import java.util.function.Supplier;
30 import java.util.stream.IntStream;
31 import org.sonar.api.utils.DateUtils;
32 import org.sonar.api.utils.log.Logger;
33 import org.sonar.api.utils.log.Loggers;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.core.issue.IssueChangeContext;
36 import org.sonar.db.protobuf.DbCommons.TextRange;
37 import org.sonar.db.protobuf.DbIssues;
38 import org.sonar.db.protobuf.DbIssues.Flow;
39 import org.sonar.db.protobuf.DbIssues.Location;
40 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
41 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
42 import org.sonar.server.computation.task.projectanalysis.analysis.ScannerPlugin;
43 import org.sonar.server.computation.task.projectanalysis.component.Component;
44 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRule;
45 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRulesHolder;
46 import org.sonar.server.computation.task.projectanalysis.scm.Changeset;
47 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfo;
48 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepository;
49 import org.sonar.server.issue.IssueFieldsSetter;
51 import static org.sonar.core.issue.IssueChangeContext.createScan;
54 * Calculates the creation date of an issue. Takes into account, that the issue
55 * might be raised by adding a rule to a quality profile.
57 public class IssueCreationDateCalculator extends IssueVisitor {
59 private static final Logger LOGGER = Loggers.get(IssueCreationDateCalculator.class);
61 private final ScmInfoRepository scmInfoRepository;
62 private final IssueFieldsSetter issueUpdater;
63 private final AnalysisMetadataHolder analysisMetadataHolder;
64 private final IssueChangeContext changeContext;
65 private final ActiveRulesHolder activeRulesHolder;
67 public IssueCreationDateCalculator(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository,
68 IssueFieldsSetter issueUpdater, ActiveRulesHolder activeRulesHolder) {
69 this.scmInfoRepository = scmInfoRepository;
70 this.issueUpdater = issueUpdater;
71 this.analysisMetadataHolder = analysisMetadataHolder;
72 this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate()));
73 this.activeRulesHolder = activeRulesHolder;
77 public void onIssue(Component component, DefaultIssue issue) {
81 Optional<Long> lastAnalysisOptional = lastAnalysis();
82 boolean firstAnalysis = !lastAnalysisOptional.isPresent();
83 ActiveRule activeRule = toJavaUtilOptional(activeRulesHolder.get(issue.getRuleKey()))
84 .orElseThrow(illegalStateException("The rule %s raised an issue, but is not one of the active rules.", issue.getRuleKey()));
86 || activeRuleIsNew(activeRule, lastAnalysisOptional.get())
87 || ruleImplementationChanged(activeRule, lastAnalysisOptional.get())) {
88 getScmChangeDate(component, issue)
89 .ifPresent(changeDate -> updateDate(issue, changeDate));
93 private boolean ruleImplementationChanged(ActiveRule activeRule, long lastAnalysisDate) {
94 String pluginKey = activeRule.getPluginKey();
95 if (pluginKey == null) {
99 ScannerPlugin scannerPlugin = Optional.ofNullable(analysisMetadataHolder.getScannerPluginsByKey().get(pluginKey))
100 .orElseThrow(illegalStateException("The rule %s is declared to come from plugin %s, but this plugin was not used by scanner.", activeRule.getRuleKey(), pluginKey));
101 return pluginIsNew(scannerPlugin, lastAnalysisDate)
102 || basePluginIsNew(scannerPlugin, lastAnalysisDate);
105 private boolean basePluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
106 String basePluginKey = scannerPlugin.getBasePluginKey();
107 if (basePluginKey == null) {
110 ScannerPlugin basePlugin = analysisMetadataHolder.getScannerPluginsByKey().get(basePluginKey);
111 return lastAnalysisDate < basePlugin.getUpdatedAt();
114 private static boolean pluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
115 return lastAnalysisDate < scannerPlugin.getUpdatedAt();
118 private static boolean activeRuleIsNew(ActiveRule activeRule, Long lastAnalysisDate) {
119 long ruleCreationDate = activeRule.getCreatedAt();
120 return lastAnalysisDate < ruleCreationDate;
123 private Optional<Date> getScmChangeDate(Component component, DefaultIssue issue) {
124 return getScmInfo(component)
125 .flatMap(scmInfo -> getChangeset(component, scmInfo, issue))
126 .map(IssueCreationDateCalculator::getChangeDate);
129 private Optional<Long> lastAnalysis() {
130 return Optional.ofNullable(analysisMetadataHolder.getBaseAnalysis()).map(Analysis::getCreatedAt);
133 private Optional<ScmInfo> getScmInfo(Component component) {
134 return toJavaUtilOptional(scmInfoRepository.getScmInfo(component));
137 private static Optional<Changeset> getChangeset(Component component, ScmInfo scmInfo, DefaultIssue issue) {
138 Set<Integer> involvedLines = new HashSet<>();
139 DbIssues.Locations locations = issue.getLocations();
140 if (locations != null) {
141 if (locations.hasTextRange()) {
142 addLines(involvedLines, locations.getTextRange());
144 for (Flow f : locations.getFlowList()) {
145 for (Location l : f.getLocationList()) {
146 if (Objects.equals(l.getComponentId(), component.getUuid())) {
147 // Ignore locations in other files, since it is currently not very common, and this is hard to load SCM by component UUID.
148 addLines(involvedLines, l.getTextRange());
152 if (!involvedLines.isEmpty()) {
153 return involvedLines.stream()
154 .map(scmInfo::getChangesetForLine)
155 .max(Comparator.comparingLong(Changeset::getDate));
159 Changeset latestChangeset = scmInfo.getLatestChangeset();
160 if (latestChangeset != null) {
161 return Optional.of(latestChangeset);
164 return Optional.empty();
167 private static void addLines(Set<Integer> involvedLines, TextRange range) {
168 IntStream.rangeClosed(range.getStartLine(), range.getEndLine()).forEach(involvedLines::add);
171 private static Date getChangeDate(Changeset changesetForLine) {
172 return DateUtils.longToDate(changesetForLine.getDate());
175 private void updateDate(DefaultIssue issue, Date scmDate) {
176 LOGGER.debug("Issue {} seems to be raised in consequence of a modification of the quality profile. Backdating the issue to {}.", issue,
177 DateTimeFormatter.ISO_INSTANT.format(scmDate.toInstant()));
178 issueUpdater.setCreationDate(issue, scmDate, changeContext);
181 private static <T> Optional<T> toJavaUtilOptional(com.google.common.base.Optional<T> scmInfo) {
182 return scmInfo.transform(Optional::of).or(Optional::empty);
185 private static Supplier<? extends IllegalStateException> illegalStateException(String str, Object... args) {
186 return () -> new IllegalStateException(String.format(str, args));