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.Optional;
28 import java.util.function.Supplier;
29 import java.util.stream.IntStream;
30 import org.sonar.api.utils.DateUtils;
31 import org.sonar.api.utils.log.Logger;
32 import org.sonar.api.utils.log.Loggers;
33 import org.sonar.core.issue.DefaultIssue;
34 import org.sonar.core.issue.IssueChangeContext;
35 import org.sonar.db.protobuf.DbCommons.TextRange;
36 import org.sonar.db.protobuf.DbIssues;
37 import org.sonar.db.protobuf.DbIssues.Flow;
38 import org.sonar.db.protobuf.DbIssues.Location;
39 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
40 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
41 import org.sonar.server.computation.task.projectanalysis.analysis.ScannerPlugin;
42 import org.sonar.server.computation.task.projectanalysis.component.Component;
43 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRule;
44 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRulesHolder;
45 import org.sonar.server.computation.task.projectanalysis.scm.Changeset;
46 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfo;
47 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepository;
48 import org.sonar.server.issue.IssueFieldsSetter;
50 import static org.sonar.core.issue.IssueChangeContext.createScan;
53 * Calculates the creation date of an issue. Takes into account, that the issue
54 * might be raised by adding a rule to a quality profile.
56 public class IssueCreationDateCalculator extends IssueVisitor {
58 private static final Logger LOGGER = Loggers.get(IssueCreationDateCalculator.class);
60 private final ScmInfoRepository scmInfoRepository;
61 private final IssueFieldsSetter issueUpdater;
62 private final AnalysisMetadataHolder analysisMetadataHolder;
63 private final IssueChangeContext changeContext;
64 private final ActiveRulesHolder activeRulesHolder;
66 public IssueCreationDateCalculator(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository,
67 IssueFieldsSetter issueUpdater, ActiveRulesHolder activeRulesHolder) {
68 this.scmInfoRepository = scmInfoRepository;
69 this.issueUpdater = issueUpdater;
70 this.analysisMetadataHolder = analysisMetadataHolder;
71 this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate()));
72 this.activeRulesHolder = activeRulesHolder;
76 public void onIssue(Component component, DefaultIssue issue) {
80 Optional<Long> lastAnalysisOptional = lastAnalysis();
81 boolean firstAnalysis = !lastAnalysisOptional.isPresent();
82 ActiveRule activeRule = toJavaUtilOptional(activeRulesHolder.get(issue.getRuleKey()))
83 .orElseThrow(illegalStateException("The rule %s raised an issue, but is not one of the active rules.", issue.getRuleKey()));
85 || activeRuleIsNew(activeRule, lastAnalysisOptional.get())
86 || ruleImplementationChanged(activeRule, lastAnalysisOptional.get())) {
87 getScmChangeDate(component, issue)
88 .ifPresent(changeDate -> updateDate(issue, changeDate));
92 private boolean ruleImplementationChanged(ActiveRule activeRule, long lastAnalysisDate) {
93 String pluginKey = activeRule.getPluginKey();
94 if (pluginKey == null) {
98 ScannerPlugin scannerPlugin = Optional.ofNullable(analysisMetadataHolder.getScannerPluginsByKey().get(pluginKey))
99 .orElseThrow(illegalStateException("The rule %s is declared to come from plugin %s, but this plugin was not used by scanner.", activeRule.getRuleKey(), pluginKey));
100 return pluginIsNew(scannerPlugin, lastAnalysisDate)
101 || basePluginIsNew(scannerPlugin, lastAnalysisDate);
104 private boolean basePluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
105 String basePluginKey = scannerPlugin.getBasePluginKey();
106 if (basePluginKey == null) {
109 ScannerPlugin basePlugin = analysisMetadataHolder.getScannerPluginsByKey().get(basePluginKey);
110 return lastAnalysisDate < basePlugin.getUpdatedAt();
113 private static boolean pluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
114 return lastAnalysisDate < scannerPlugin.getUpdatedAt();
117 private static boolean activeRuleIsNew(ActiveRule activeRule, Long lastAnalysisDate) {
118 long ruleCreationDate = activeRule.getCreatedAt();
119 return lastAnalysisDate < ruleCreationDate;
122 private Optional<Date> getScmChangeDate(Component component, DefaultIssue issue) {
123 return getScmInfo(component)
124 .flatMap(scmInfo -> getChangeset(scmInfo, issue))
125 .map(IssueCreationDateCalculator::getChangeDate);
128 private Optional<Long> lastAnalysis() {
129 return Optional.ofNullable(analysisMetadataHolder.getBaseAnalysis()).map(Analysis::getCreatedAt);
132 private Optional<ScmInfo> getScmInfo(Component component) {
133 return toJavaUtilOptional(scmInfoRepository.getScmInfo(component));
136 private static Optional<Changeset> getChangeset(ScmInfo scmInfo, DefaultIssue issue) {
137 Set<Integer> involvedLines = new HashSet<>();
138 DbIssues.Locations locations = issue.getLocations();
139 if (locations != null) {
140 if (locations.hasTextRange()) {
141 addLines(involvedLines, locations.getTextRange());
143 for (Flow f : locations.getFlowList()) {
144 for (Location l : f.getLocationList()) {
145 addLines(involvedLines, l.getTextRange());
148 if (!involvedLines.isEmpty()) {
149 return involvedLines.stream()
150 .map(scmInfo::getChangesetForLine)
151 .max(Comparator.comparingLong(Changeset::getDate));
155 Changeset latestChangeset = scmInfo.getLatestChangeset();
156 if (latestChangeset != null) {
157 return Optional.of(latestChangeset);
160 return Optional.empty();
163 private static void addLines(Set<Integer> involvedLines, TextRange range) {
164 IntStream.rangeClosed(range.getStartLine(), range.getEndLine()).forEach(involvedLines::add);
167 private static Date getChangeDate(Changeset changesetForLine) {
168 return DateUtils.longToDate(changesetForLine.getDate());
171 private void updateDate(DefaultIssue issue, Date scmDate) {
172 LOGGER.debug("Issue {} seems to be raised in consequence of a modification of the quality profile. Backdating the issue to {}.", issue,
173 DateTimeFormatter.ISO_INSTANT.format(scmDate.toInstant()));
174 issueUpdater.setCreationDate(issue, scmDate, changeContext);
177 private static <T> Optional<T> toJavaUtilOptional(com.google.common.base.Optional<T> scmInfo) {
178 return scmInfo.transform(Optional::of).or(Optional::empty);
181 private static Supplier<? extends IllegalStateException> illegalStateException(String str, Object... args) {
182 return () -> new IllegalStateException(String.format(str, args));