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