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