]> source.dussan.org Git - sonarqube.git/blob
8f855709a18f2b7f822351e42c744e8c26d9e451
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.issue;
21
22 import com.google.common.collect.EnumMultiset;
23 import com.google.common.collect.HashMultiset;
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.Multiset;
26 import java.util.HashMap;
27 import java.util.Map;
28 import javax.annotation.Nullable;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.measure.Measure;
32 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
33 import org.sonar.ce.task.projectanalysis.metric.Metric;
34 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.core.issue.status.IssueStatus;
37
38 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
39 import static org.sonar.api.issue.Issue.STATUS_OPEN;
40 import static org.sonar.api.issue.Issue.STATUS_REOPENED;
41 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
42 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
43 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
44 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
45 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
46 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
47 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
49 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
50 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
51 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS_KEY;
53 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS_KEY;
54 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
55 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS_KEY;
56 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
57 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS_KEY;
58 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
59 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
60 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES_KEY;
61 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
62 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
63 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
64 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
65 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
66 import static org.sonar.api.rule.Severity.BLOCKER;
67 import static org.sonar.api.rule.Severity.CRITICAL;
68 import static org.sonar.api.rule.Severity.INFO;
69 import static org.sonar.api.rule.Severity.MAJOR;
70 import static org.sonar.api.rule.Severity.MINOR;
71 import static org.sonar.api.rules.RuleType.BUG;
72 import static org.sonar.api.rules.RuleType.CODE_SMELL;
73 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
74 import static org.sonar.api.rules.RuleType.VULNERABILITY;
75
76 /**
77  * For each component, computes the measures related to number of issues:
78  * <ul>
79  * <li>issues per status (open, reopen, confirmed)</li>
80  * <li>issues per resolution (unresolved, false-positives, won't fix)</li>
81  * <li>issues per severity (from info to blocker)</li>
82  * <li>issues per type (code smell, bug, vulnerability, security hotspots)</li>
83  * </ul>
84  * For each value, the variation on configured periods is also computed.
85  */
86 public class IssueCounter extends IssueVisitor {
87
88   private static final Map<String, String> SEVERITY_TO_METRIC_KEY = ImmutableMap.of(
89     BLOCKER, BLOCKER_VIOLATIONS_KEY,
90     CRITICAL, CRITICAL_VIOLATIONS_KEY,
91     MAJOR, MAJOR_VIOLATIONS_KEY,
92     MINOR, MINOR_VIOLATIONS_KEY,
93     INFO, INFO_VIOLATIONS_KEY);
94
95   private static final Map<String, String> SEVERITY_TO_NEW_METRIC_KEY = ImmutableMap.of(
96     BLOCKER, NEW_BLOCKER_VIOLATIONS_KEY,
97     CRITICAL, NEW_CRITICAL_VIOLATIONS_KEY,
98     MAJOR, NEW_MAJOR_VIOLATIONS_KEY,
99     MINOR, NEW_MINOR_VIOLATIONS_KEY,
100     INFO, NEW_INFO_VIOLATIONS_KEY);
101
102   private static final Map<RuleType, String> TYPE_TO_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
103     .put(CODE_SMELL, CODE_SMELLS_KEY)
104     .put(BUG, BUGS_KEY)
105     .put(VULNERABILITY, VULNERABILITIES_KEY)
106     .put(SECURITY_HOTSPOT, SECURITY_HOTSPOTS_KEY)
107     .build();
108   private static final Map<RuleType, String> TYPE_TO_NEW_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
109     .put(CODE_SMELL, NEW_CODE_SMELLS_KEY)
110     .put(BUG, NEW_BUGS_KEY)
111     .put(VULNERABILITY, NEW_VULNERABILITIES_KEY)
112     .put(SECURITY_HOTSPOT, NEW_SECURITY_HOTSPOTS_KEY)
113     .build();
114
115   private final MetricRepository metricRepository;
116   private final MeasureRepository measureRepository;
117   private final NewIssueClassifier newIssueClassifier;
118   private final Map<String, Counters> countersByComponentUuid = new HashMap<>();
119
120   private Counters currentCounters;
121
122   public IssueCounter(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
123     this.metricRepository = metricRepository;
124     this.measureRepository = measureRepository;
125     this.newIssueClassifier = newIssueClassifier;
126   }
127
128   @Override
129   public void beforeComponent(Component component) {
130     currentCounters = new Counters();
131     countersByComponentUuid.put(component.getUuid(), currentCounters);
132
133     // aggregate children counters
134     for (Component child : component.getChildren()) {
135       Counters childCounters = countersByComponentUuid.remove(child.getUuid());
136       currentCounters.add(childCounters);
137     }
138   }
139
140   @Override
141   public void onIssue(Component component, DefaultIssue issue) {
142     currentCounters.add(issue);
143     if (newIssueClassifier.isNew(component, issue)) {
144       currentCounters.addOnPeriod(issue);
145     }
146   }
147
148   @Override
149   public void afterComponent(Component component) {
150     addMeasuresBySeverity(component);
151     addMeasuresByStatus(component);
152     addMeasuresByType(component);
153     addNewMeasures(component);
154     currentCounters = null;
155   }
156
157   private void addMeasuresBySeverity(Component component) {
158     for (Map.Entry<String, String> entry : SEVERITY_TO_METRIC_KEY.entrySet()) {
159       String severity = entry.getKey();
160       String metricKey = entry.getValue();
161       addMeasure(component, metricKey, currentCounters.counter().severityBag.count(severity));
162     }
163   }
164
165   private void addMeasuresByStatus(Component component) {
166     addMeasure(component, VIOLATIONS_KEY, currentCounters.counter().unresolved);
167     addMeasure(component, OPEN_ISSUES_KEY, currentCounters.counter().open);
168     addMeasure(component, REOPENED_ISSUES_KEY, currentCounters.counter().reopened);
169     addMeasure(component, CONFIRMED_ISSUES_KEY, currentCounters.counter().confirmed);
170     addMeasure(component, FALSE_POSITIVE_ISSUES_KEY, currentCounters.counter().falsePositives);
171     addMeasure(component, ACCEPTED_ISSUES_KEY, currentCounters.counter().accepted);
172   }
173
174   private void addMeasuresByType(Component component) {
175     for (Map.Entry<RuleType, String> entry : TYPE_TO_METRIC_KEY.entrySet()) {
176       addMeasure(component, entry.getValue(), currentCounters.counter().typeBag.count(entry.getKey()));
177     }
178   }
179
180   private void addMeasure(Component component, String metricKey, int value) {
181     Metric metric = metricRepository.getByKey(metricKey);
182     measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
183   }
184
185   private void addNewMeasures(Component component) {
186     if (!newIssueClassifier.isEnabled()) {
187       return;
188     }
189     int unresolved = currentCounters.counterForPeriod().unresolved;
190     measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
191       .create(unresolved));
192
193     for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
194       String severity = entry.getKey();
195       String metricKey = entry.getValue();
196       Multiset<String> bag = currentCounters.counterForPeriod().severityBag;
197       Metric metric = metricRepository.getByKey(metricKey);
198       measureRepository.add(component, metric, Measure.newMeasureBuilder()
199         .create(bag.count(severity)));
200     }
201
202     // waiting for Java 8 lambda in order to factor this loop with the previous one
203     // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
204     for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
205       RuleType type = entry.getKey();
206       String metricKey = entry.getValue();
207       Multiset<RuleType> bag = currentCounters.counterForPeriod().typeBag;
208       Metric metric = metricRepository.getByKey(metricKey);
209       measureRepository.add(component, metric, Measure.newMeasureBuilder()
210         .create(bag.count(type)));
211     }
212   }
213
214   /**
215    * Count issues by status, resolutions, rules and severities
216    */
217   private static class Counter {
218     private int unresolved = 0;
219     private int open = 0;
220     private int reopened = 0;
221     private int confirmed = 0;
222     private int falsePositives = 0;
223     private int accepted = 0;
224     private final Multiset<String> severityBag = HashMultiset.create();
225     private final EnumMultiset<RuleType> typeBag = EnumMultiset.create(RuleType.class);
226
227     void add(Counter counter) {
228       unresolved += counter.unresolved;
229       open += counter.open;
230       reopened += counter.reopened;
231       confirmed += counter.confirmed;
232       falsePositives += counter.falsePositives;
233       accepted += counter.accepted;
234       severityBag.addAll(counter.severityBag);
235       typeBag.addAll(counter.typeBag);
236     }
237
238     void add(DefaultIssue issue) {
239       if (issue.type() == SECURITY_HOTSPOT) {
240         if (issue.resolution() == null) {
241           typeBag.add(SECURITY_HOTSPOT);
242         }
243         return;
244       }
245       if (issue.resolution() == null) {
246         unresolved++;
247         typeBag.add(issue.type());
248         severityBag.add(issue.severity());
249       } else if (IssueStatus.FALSE_POSITIVE.equals(issue.getIssueStatus())) {
250         falsePositives++;
251       } else if (IssueStatus.ACCEPTED.equals(issue.getIssueStatus())) {
252         accepted++;
253       }
254       switch (issue.status()) {
255         case STATUS_OPEN:
256           open++;
257           break;
258         case STATUS_REOPENED:
259           reopened++;
260           break;
261         case STATUS_CONFIRMED:
262           confirmed++;
263           break;
264         default:
265           // Other statuses are ignored
266       }
267     }
268   }
269
270   /**
271    * List of {@link Counter} for regular value and period.
272    */
273   private static class Counters {
274     private final Counter counter = new Counter();
275     private final Counter counterForPeriod = new Counter();
276
277     void add(@Nullable Counters other) {
278       if (other != null) {
279         counter.add(other.counter);
280         counterForPeriod.add(other.counterForPeriod);
281       }
282     }
283
284     void addOnPeriod(DefaultIssue issue) {
285       counterForPeriod.add(issue);
286     }
287
288     void add(DefaultIssue issue) {
289       counter.add(issue);
290     }
291
292     Counter counter() {
293       return counter;
294     }
295
296     Counter counterForPeriod() {
297       return counterForPeriod;
298     }
299   }
300 }