]> source.dussan.org Git - sonarqube.git/blob
58322389702e87e4457c10f5c2c0956a8680fe0b
[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.server.qualitygate;
21
22 import java.io.Serializable;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.metric.MetricDto;
33 import org.sonar.db.qualitygate.QualityGateConditionDto;
34
35 import static java.util.stream.Collectors.toUnmodifiableMap;
36 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
37 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY;
38 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
39 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
40 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
41 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
42 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
43 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING;
44 import static org.sonar.server.qualitygate.QualityGateCaycStatus.COMPLIANT;
45 import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT;
46 import static org.sonar.server.qualitygate.QualityGateCaycStatus.OVER_COMPLIANT;
47
48 public class QualityGateCaycChecker {
49
50   public static final List<Metric<? extends Serializable>> CAYC_METRICS = List.of(
51     NEW_MAINTAINABILITY_RATING,
52     NEW_RELIABILITY_RATING,
53     NEW_SECURITY_HOTSPOTS_REVIEWED,
54     NEW_SECURITY_RATING,
55     NEW_DUPLICATED_LINES_DENSITY,
56     NEW_COVERAGE
57   );
58
59   private static final Set<String> EXISTENCY_REQUIREMENTS = Set.of(
60     NEW_DUPLICATED_LINES_DENSITY_KEY,
61     NEW_COVERAGE_KEY
62   );
63
64   private static final Map<String, Double> BEST_VALUE_REQUIREMENTS = CAYC_METRICS.stream()
65     .filter(metric -> !EXISTENCY_REQUIREMENTS.contains(metric.getKey()))
66     .collect(toUnmodifiableMap(Metric::getKey, Metric::getBestValue));
67
68   private final DbClient dbClient;
69
70   public QualityGateCaycChecker(DbClient dbClient) {
71     this.dbClient = dbClient;
72   }
73
74   public QualityGateCaycStatus checkCaycCompliant(DbSession dbSession, String qualityGateUuid) {
75     var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid)
76       .stream()
77       .collect(Collectors.toMap(QualityGateConditionDto::getMetricUuid, Function.identity()));
78
79     if (conditionsByMetricId.size() < CAYC_METRICS.size()) {
80       return NON_COMPLIANT;
81     }
82
83     var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet())
84       .stream()
85       .filter(MetricDto::isEnabled)
86       .toList();
87
88     long count = metrics.stream()
89       .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric))
90       .count();
91
92     if (metrics.size() == count && count == CAYC_METRICS.size()) {
93       return COMPLIANT;
94     } else if (metrics.size() > count && count == CAYC_METRICS.size()) {
95       return OVER_COMPLIANT;
96     }
97
98     return NON_COMPLIANT;
99   }
100
101   public QualityGateCaycStatus checkCaycCompliantFromProject(DbSession dbSession, String projectUuid) {
102     return Optional.ofNullable(dbClient.qualityGateDao().selectByProjectUuid(dbSession, projectUuid))
103       .or(() -> Optional.ofNullable(dbClient.qualityGateDao().selectDefault(dbSession)))
104       .map(qualityGate -> checkCaycCompliant(dbSession, qualityGate.getUuid()))
105       .orElse(NON_COMPLIANT);
106   }
107
108   private static boolean checkMetricCaycCompliant(QualityGateConditionDto condition, MetricDto metric) {
109     if (EXISTENCY_REQUIREMENTS.contains(metric.getKey())) {
110       return true;
111     } else if (BEST_VALUE_REQUIREMENTS.containsKey(metric.getKey())) {
112       Double errorThreshold = Double.valueOf(condition.getErrorThreshold());
113       Double caycRequiredThreshold = BEST_VALUE_REQUIREMENTS.get(metric.getKey());
114       return caycRequiredThreshold.compareTo(errorThreshold) == 0;
115     } else {
116       return false;
117     }
118   }
119
120 }