]> source.dussan.org Git - sonarqube.git/blob
94afc9a3462b0e1b2bc945454c3f576d325787bd
[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.util.List;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.metric.MetricDto;
30 import org.sonar.db.qualitygate.QualityGateConditionDto;
31
32 import static java.util.stream.Collectors.toUnmodifiableMap;
33 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
34 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
35 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
36 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING;
37 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
38
39 public class QualityGateCaycChecker {
40
41   public static final List<Metric<Integer>> CAYC_METRICS = List.of(
42     NEW_MAINTAINABILITY_RATING,
43     NEW_RELIABILITY_RATING,
44     NEW_SECURITY_HOTSPOTS_REVIEWED,
45     NEW_SECURITY_RATING
46   );
47
48   private static final Map<String, Double> CAYC_REQUIREMENTS = CAYC_METRICS.stream()
49     .collect(toUnmodifiableMap(Metric::getKey, Metric::getBestValue));
50
51   private final DbClient dbClient;
52
53   public QualityGateCaycChecker(DbClient dbClient) {
54     this.dbClient = dbClient;
55   }
56
57   public boolean checkCaycCompliant(DbSession dbSession, String qualityGateUuid) {
58     var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid)
59       .stream()
60       .collect(uniqueIndex(QualityGateConditionDto::getMetricUuid));
61     var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet())
62       .stream()
63       .filter(MetricDto::isEnabled)
64       .collect(Collectors.toSet());
65     long count = metrics.stream()
66       .filter(metric -> CAYC_REQUIREMENTS.containsKey(metric.getKey()))
67       .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric))
68       .count();
69     return count == CAYC_REQUIREMENTS.size();
70   }
71
72   public boolean checkCaycCompliantFromProject(DbSession dbSession, String projectUuid) {
73     return Optional.ofNullable(dbClient.qualityGateDao().selectByProjectUuid(dbSession, projectUuid))
74       .or(() -> Optional.ofNullable(dbClient.qualityGateDao().selectDefault(dbSession)))
75       .map(qualityGate -> checkCaycCompliant(dbSession, qualityGate.getUuid()))
76       .orElse(false);
77   }
78
79   private static boolean checkMetricCaycCompliant(QualityGateConditionDto condition, MetricDto metric) {
80     Double errorThreshold = Double.valueOf(condition.getErrorThreshold());
81     Double caycRequiredThreshold = CAYC_REQUIREMENTS.get(metric.getKey());
82     return caycRequiredThreshold.compareTo(errorThreshold) == 0;
83   }
84
85 }