123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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 com.google.common.base.MoreObjects;
- import java.util.HashMap;
- import java.util.Map;
- import org.sonar.api.measures.CoreMetrics;
- import org.sonar.ce.task.projectanalysis.component.Component;
- import org.sonar.ce.task.projectanalysis.measure.Measure;
- import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
- import org.sonar.ce.task.projectanalysis.metric.Metric;
- import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
- import org.sonar.core.issue.DefaultIssue;
-
- import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
- import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
- import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
-
- /**
- * Compute new effort related measures :
- * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
- * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
- * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
- */
- public class NewEffortAggregator extends IssueVisitor {
- private final Map<String, NewEffortCounter> counterByComponentUuid = new HashMap<>();
- private final MeasureRepository measureRepository;
-
- private final Metric newMaintainabilityEffortMetric;
- private final Metric newReliabilityEffortMetric;
- private final Metric newSecurityEffortMetric;
- private final NewIssueClassifier newIssueClassifier;
-
- private NewEffortCounter counter = null;
-
- public NewEffortAggregator(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
- this.measureRepository = measureRepository;
-
- this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
- this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
- this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
- this.newIssueClassifier = newIssueClassifier;
- }
-
- @Override
- public void beforeComponent(Component component) {
- counter = new NewEffortCounter();
- counterByComponentUuid.put(component.getUuid(), counter);
- for (Component child : component.getChildren()) {
- NewEffortCounter childSum = counterByComponentUuid.remove(child.getUuid());
- if (childSum != null) {
- counter.add(childSum);
- }
- }
- }
-
- @Override
- public void onIssue(Component component, DefaultIssue issue) {
- if (issue.resolution() == null && issue.effortInMinutes() != null) {
- counter.add(component, issue);
- }
- }
-
- @Override
- public void afterComponent(Component component) {
- if (newIssueClassifier.isEnabled()) {
- computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
- computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
- computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
- }
- counter = null;
- }
-
- private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
- double variation = effortSum.isEmpty ? 0.0 : effortSum.newEffort;
- measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariation(variation).createNoValue());
- }
-
- private class NewEffortCounter {
- private final EffortSum maintainabilitySum = new EffortSum();
- private final EffortSum reliabilitySum = new EffortSum();
- private final EffortSum securitySum = new EffortSum();
-
- void add(NewEffortCounter otherCounter) {
- maintainabilitySum.add(otherCounter.maintainabilitySum);
- reliabilitySum.add(otherCounter.reliabilitySum);
- securitySum.add(otherCounter.securitySum);
- }
-
- void add(Component component, DefaultIssue issue) {
- long newEffort = calculate(component, issue);
- switch (issue.type()) {
- case CODE_SMELL:
- maintainabilitySum.add(newEffort);
- break;
- case BUG:
- reliabilitySum.add(newEffort);
- break;
- case VULNERABILITY:
- securitySum.add(newEffort);
- break;
- case SECURITY_HOTSPOT:
- // Not counted
- break;
- default:
- throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
- }
- }
-
- long calculate(Component component, DefaultIssue issue) {
- if (newIssueClassifier.isNew(component, issue)) {
- return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
- }
-
- return 0L;
- }
- }
-
- private static class EffortSum {
- private Double newEffort;
- private boolean isEmpty = true;
-
- void add(long newEffort) {
- double previous = MoreObjects.firstNonNull(this.newEffort, 0D);
- this.newEffort = previous + newEffort;
- isEmpty = false;
- }
-
- void add(EffortSum other) {
- Double otherValue = other.newEffort;
- if (otherValue != null) {
- add(otherValue.longValue());
- }
- }
- }
- }
|