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