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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.issue;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.sonar.api.batch.debt.DebtRemediationFunction;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.MessageException;
import org.sonar.core.issue.DefaultIssueBuilder;
import javax.annotation.Nullable;
/**
* Initialize the issues raised during scan.
*/
public class ModuleIssues {
private final ActiveRules activeRules;
private final Rules rules;
private final IssueCache cache;
private final Project project;
private final IssueFilters filters;
public ModuleIssues(ActiveRules activeRules, Rules rules, IssueCache cache, Project project, IssueFilters filters) {
this.activeRules = activeRules;
this.rules = rules;
this.cache = cache;
this.project = project;
this.filters = filters;
}
public boolean initAndAddIssue(DefaultIssue issue) {
return initAndAddIssue(issue, null);
}
public boolean initAndAddViolation(Violation violation) {
DefaultIssue issue = newIssue(violation);
return initAndAddIssue(issue, violation);
}
private DefaultIssue newIssue(Violation violation) {
return (DefaultIssue) new DefaultIssueBuilder()
.componentKey(violation.getResource().getEffectiveKey())
.projectKey(project.getRoot().getEffectiveKey())
.ruleKey(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey()))
.effortToFix(violation.getCost())
.line(violation.getLineId())
.message(violation.getMessage())
.severity(violation.getSeverity() != null ? violation.getSeverity().name() : null)
.build();
}
private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = rules.find(ruleKey);
validateRule(issue, rule);
ActiveRule activeRule = activeRules.find(ruleKey);
if (activeRule == null) {
// rule does not exist or is not enabled -> ignore the issue
return false;
}
updateIssue(issue, rule, activeRule);
if (filters.accept(issue, violation)) {
cache.put(issue);
return true;
}
return false;
}
private void validateRule(DefaultIssue issue, Rule rule) {
RuleKey ruleKey = issue.ruleKey();
if (rule == null) {
throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey));
}
if (Strings.isNullOrEmpty(rule.name()) && Strings.isNullOrEmpty(issue.message())) {
throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey));
}
}
private void updateIssue(DefaultIssue issue, Rule rule, ActiveRule activeRule) {
if (Strings.isNullOrEmpty(issue.message())) {
issue.setMessage(rule.name());
}
issue.setCreationDate(project.getAnalysisDate());
issue.setUpdateDate(project.getAnalysisDate());
if (issue.severity() == null) {
issue.setSeverity(activeRule.severity());
}
DebtRemediationFunction function = rule.debtRemediationFunction();
if (rule.debtSubCharacteristic() != null && function != null) {
issue.setDebt(calculateDebt(function, issue.effortToFix(), rule.key()));
}
}
private Duration calculateDebt(DebtRemediationFunction function, @Nullable Double effortToFix, RuleKey ruleKey) {
if (DebtRemediationFunction.Type.CONSTANT_ISSUE.equals(function.type()) && effortToFix != null) {
throw new IllegalArgumentException("Rule '" + ruleKey + "' can not use 'Constant/issue' remediation function " +
"because this rule does not have a fixed remediation cost.");
}
Duration result = Duration.create(0);
Duration factor = function.coefficient();
Duration offset = function.offset();
if (factor != null) {
int effortToFixValue = Objects.firstNonNull(effortToFix, 1).intValue();
result = factor.multiply(effortToFixValue);
}
if (offset != null) {
result = result.add(offset);
}
return result;
}
}
|