3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualitygate;
22 import java.util.List;
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;
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;
39 public class QualityGateCaycChecker {
41 public static final List<Metric<Integer>> CAYC_METRICS = List.of(
42 NEW_MAINTAINABILITY_RATING,
43 NEW_RELIABILITY_RATING,
44 NEW_SECURITY_HOTSPOTS_REVIEWED,
48 private static final Map<String, Double> CAYC_REQUIREMENTS = CAYC_METRICS.stream()
49 .collect(toUnmodifiableMap(Metric::getKey, Metric::getBestValue));
51 private final DbClient dbClient;
53 public QualityGateCaycChecker(DbClient dbClient) {
54 this.dbClient = dbClient;
57 public boolean checkCaycCompliant(DbSession dbSession, String qualityGateUuid) {
58 var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid)
60 .collect(uniqueIndex(QualityGateConditionDto::getMetricUuid));
61 var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet())
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))
69 return count == CAYC_REQUIREMENTS.size();
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()))
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;