-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
IssueCreationDateCalculator.java
170 lines (148 loc) · 7.13 KB
/
IssueCreationDateCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.ce.task.projectanalysis.issue;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
import org.sonar.ce.task.projectanalysis.analysis.Analysis;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.ScannerPlugin;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.filemove.AddedFileRepository;
import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
import org.sonar.ce.task.projectanalysis.scm.Changeset;
import org.sonar.ce.task.projectanalysis.scm.ScmInfo;
import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepository;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.server.issue.IssueFieldsSetter;
import static org.sonar.core.issue.IssueChangeContext.createScan;
/**
* Calculates the creation date of an issue. Takes into account, that the issue
* might be raised by adding a rule to a quality profile.
*/
public class IssueCreationDateCalculator extends IssueVisitor {
private final ScmInfoRepository scmInfoRepository;
private final IssueFieldsSetter issueUpdater;
private final AnalysisMetadataHolder analysisMetadataHolder;
private final IssueChangeContext changeContext;
private final ActiveRulesHolder activeRulesHolder;
private final RuleRepository ruleRepository;
private final AddedFileRepository addedFileRepository;
public IssueCreationDateCalculator(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository,
IssueFieldsSetter issueUpdater, ActiveRulesHolder activeRulesHolder, RuleRepository ruleRepository,
AddedFileRepository addedFileRepository) {
this.scmInfoRepository = scmInfoRepository;
this.issueUpdater = issueUpdater;
this.analysisMetadataHolder = analysisMetadataHolder;
this.ruleRepository = ruleRepository;
this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate()));
this.activeRulesHolder = activeRulesHolder;
this.addedFileRepository = addedFileRepository;
}
@Override
public void onIssue(Component component, DefaultIssue issue) {
if (!issue.isNew()) {
return;
}
Optional<Long> lastAnalysisOptional = lastAnalysis();
boolean firstAnalysis = !lastAnalysisOptional.isPresent();
if (firstAnalysis || isNewFile(component)) {
backdateIssue(component, issue);
return;
}
Rule rule = ruleRepository.findByKey(issue.getRuleKey())
.orElseThrow(illegalStateException("The rule with key '%s' raised an issue, but no rule with that key was found", issue.getRuleKey()));
if (rule.isExternal()) {
backdateIssue(component, issue);
} else {
// Rule can't be inactive (see contract of IssueVisitor)
ActiveRule activeRule = activeRulesHolder.get(issue.getRuleKey()).get();
if (activeRuleIsNewOrChanged(activeRule, lastAnalysisOptional.get())
|| ruleImplementationChanged(activeRule.getRuleKey(), activeRule.getPluginKey(), lastAnalysisOptional.get())) {
backdateIssue(component, issue);
}
}
}
private boolean isNewFile(Component component) {
return component.getType() == Component.Type.FILE && addedFileRepository.isAdded(component);
}
private void backdateIssue(Component component, DefaultIssue issue) {
getDateOfLatestChange(component, issue).ifPresent(changeDate -> updateDate(issue, changeDate));
}
private boolean ruleImplementationChanged(RuleKey ruleKey, @Nullable String pluginKey, long lastAnalysisDate) {
if (pluginKey == null) {
return false;
}
ScannerPlugin scannerPlugin = Optional.ofNullable(analysisMetadataHolder.getScannerPluginsByKey().get(pluginKey))
.orElseThrow(illegalStateException("The rule %s is declared to come from plugin %s, but this plugin was not used by scanner.", ruleKey, pluginKey));
return pluginIsNew(scannerPlugin, lastAnalysisDate)
|| basePluginIsNew(scannerPlugin, lastAnalysisDate);
}
private boolean basePluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
String basePluginKey = scannerPlugin.getBasePluginKey();
if (basePluginKey == null) {
return false;
}
ScannerPlugin basePlugin = analysisMetadataHolder.getScannerPluginsByKey().get(basePluginKey);
return lastAnalysisDate < basePlugin.getUpdatedAt();
}
private static boolean pluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysisDate) {
return lastAnalysisDate < scannerPlugin.getUpdatedAt();
}
private static boolean activeRuleIsNewOrChanged(ActiveRule activeRule, Long lastAnalysisDate) {
return lastAnalysisDate < activeRule.getUpdatedAt();
}
private Optional<Date> getDateOfLatestChange(Component component, DefaultIssue issue) {
return getScmInfo(component)
.flatMap(scmInfo -> getLatestChangeset(component, scmInfo, issue))
.map(IssueCreationDateCalculator::getChangeDate);
}
private Optional<Long> lastAnalysis() {
return Optional.ofNullable(analysisMetadataHolder.getBaseAnalysis()).map(Analysis::getCreatedAt);
}
private Optional<ScmInfo> getScmInfo(Component component) {
return scmInfoRepository.getScmInfo(component);
}
private static Optional<Changeset> getLatestChangeset(Component component, ScmInfo scmInfo, DefaultIssue issue) {
Optional<Changeset> mostRecentChangeset = IssueLocations.allLinesFor(issue, component.getUuid())
.filter(scmInfo::hasChangesetForLine)
.mapToObj(scmInfo::getChangesetForLine)
.max(Comparator.comparingLong(Changeset::getDate));
if (mostRecentChangeset.isPresent()) {
return mostRecentChangeset;
}
return Optional.of(scmInfo.getLatestChangeset());
}
private static Date getChangeDate(Changeset changesetForLine) {
return DateUtils.longToDate(changesetForLine.getDate());
}
private void updateDate(DefaultIssue issue, Date scmDate) {
issueUpdater.setCreationDate(issue, scmDate, changeContext);
}
private static Supplier<? extends IllegalStateException> illegalStateException(String str, Object... args) {
return () -> new IllegalStateException(String.format(str, args));
}
}