]> source.dussan.org Git - sonarqube.git/blob
5006fe2fe1637ac6453ee1114b021523020b57ca
[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.base.MoreObjects;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.measure.Measure;
28 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
29 import org.sonar.ce.task.projectanalysis.metric.Metric;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.server.metric.SoftwareQualitiesMetrics;
33
34 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
35 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
36 import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
37 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY;
38 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY;
39 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY;
40
41 /**
42  * Compute new effort related measures :
43  * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
44  * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
45  * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
46  * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY}
47  * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY}
48  * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY}
49  */
50 public class NewEffortAggregator extends IssueVisitor {
51   private final Map<String, NewEffortCounter> counterByComponentUuid = new HashMap<>();
52   private final MeasureRepository measureRepository;
53
54   private final Metric newMaintainabilityEffortMetric;
55   private final Metric newReliabilityEffortMetric;
56   private final Metric newSecurityEffortMetric;
57   private final NewIssueClassifier newIssueClassifier;
58   private final Metric newSoftwareQualityMaintainabilityEffortMetric;
59   private final Metric newSoftwareQualityReliabilityEffortMetric;
60   private final Metric newSoftwareQualitySecurityEffortMetric;
61
62   private NewEffortCounter counter = null;
63
64   public NewEffortAggregator(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
65     this.measureRepository = measureRepository;
66
67     // Based on issue Type and Severity
68     this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
69     this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
70     this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
71
72     // Based on software qualities
73     this.newSoftwareQualityMaintainabilityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY);
74     this.newSoftwareQualityReliabilityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY);
75     this.newSoftwareQualitySecurityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY);
76
77     this.newIssueClassifier = newIssueClassifier;
78   }
79
80   @Override
81   public void beforeComponent(Component component) {
82     counter = new NewEffortCounter();
83     counterByComponentUuid.put(component.getUuid(), counter);
84     for (Component child : component.getChildren()) {
85       NewEffortCounter childSum = counterByComponentUuid.remove(child.getUuid());
86       if (childSum != null) {
87         counter.add(childSum);
88       }
89     }
90   }
91
92   @Override
93   public void onIssue(Component component, DefaultIssue issue) {
94     if (issue.resolution() == null && issue.effortInMinutes() != null) {
95       counter.add(component, issue);
96     }
97   }
98
99   @Override
100   public void afterComponent(Component component) {
101     if (newIssueClassifier.isEnabled()) {
102       computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
103       computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
104       computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
105
106       computeMeasure(component, newSoftwareQualityMaintainabilityEffortMetric, counter.softwareQualityMaintainabilitySum);
107       computeMeasure(component, newSoftwareQualityReliabilityEffortMetric, counter.softwareQualityReliabilitySum);
108       computeMeasure(component, newSoftwareQualitySecurityEffortMetric, counter.softwareQualitySecuritySum);
109     }
110     counter = null;
111   }
112
113   private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
114     long value = effortSum.isEmpty ? 0 : effortSum.newEffort;
115     measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
116   }
117
118   private class NewEffortCounter {
119     private final EffortSum maintainabilitySum = new EffortSum();
120     private final EffortSum reliabilitySum = new EffortSum();
121     private final EffortSum securitySum = new EffortSum();
122
123     private final EffortSum softwareQualityMaintainabilitySum = new EffortSum();
124     private final EffortSum softwareQualityReliabilitySum = new EffortSum();
125     private final EffortSum softwareQualitySecuritySum = new EffortSum();
126
127     void add(NewEffortCounter otherCounter) {
128       maintainabilitySum.add(otherCounter.maintainabilitySum);
129       reliabilitySum.add(otherCounter.reliabilitySum);
130       securitySum.add(otherCounter.securitySum);
131
132       softwareQualityMaintainabilitySum.add(otherCounter.softwareQualityMaintainabilitySum);
133       softwareQualityReliabilitySum.add(otherCounter.softwareQualityReliabilitySum);
134       softwareQualitySecuritySum.add(otherCounter.softwareQualitySecuritySum);
135     }
136
137     void add(Component component, DefaultIssue issue) {
138       long newEffort = calculate(component, issue);
139       computeTypeEffort(issue, newEffort);
140       computeSoftwareQualityEffort(issue, newEffort);
141     }
142
143     private void computeSoftwareQualityEffort(DefaultIssue issue, long newEffort) {
144       issue.impacts().forEach((sq, severity) -> {
145         switch (sq) {
146           case MAINTAINABILITY:
147             softwareQualityMaintainabilitySum.add(newEffort);
148             break;
149           case RELIABILITY:
150             softwareQualityReliabilitySum.add(newEffort);
151             break;
152           case SECURITY:
153             softwareQualitySecuritySum.add(newEffort);
154             break;
155           default:
156             throw new IllegalStateException(String.format("Unknown software quality '%s'", sq));
157         }
158       });
159     }
160
161     private void computeTypeEffort(DefaultIssue issue, long newEffort) {
162       switch (issue.type()) {
163         case CODE_SMELL:
164           maintainabilitySum.add(newEffort);
165           break;
166         case BUG:
167           reliabilitySum.add(newEffort);
168           break;
169         case VULNERABILITY:
170           securitySum.add(newEffort);
171           break;
172         case SECURITY_HOTSPOT:
173           // Not counted
174           break;
175         default:
176           throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
177       }
178     }
179
180     long calculate(Component component, DefaultIssue issue) {
181       if (newIssueClassifier.isNew(component, issue)) {
182         return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
183       }
184
185       return 0L;
186     }
187   }
188
189   private static class EffortSum {
190     private Long newEffort;
191     private boolean isEmpty = true;
192
193     void add(long newEffort) {
194       long previous = MoreObjects.firstNonNull(this.newEffort, 0L);
195       this.newEffort = previous + newEffort;
196       isEmpty = false;
197     }
198
199     void add(EffortSum other) {
200       Long otherValue = other.newEffort;
201       if (otherValue != null) {
202         add(otherValue);
203       }
204     }
205   }
206 }