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.io.Serializable;
24 import java.util.Optional;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
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;
35 import static java.util.stream.Collectors.toMap;
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_SECURITY_HOTSPOTS_REVIEWED;
41 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS;
42 import static org.sonar.server.qualitygate.QualityGateCaycStatus.COMPLIANT;
43 import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT;
44 import static org.sonar.server.qualitygate.QualityGateCaycStatus.OVER_COMPLIANT;
46 public class QualityGateCaycChecker {
48 static final Map<String, Double> BEST_VALUE_REQUIREMENTS = Stream.of(
50 NEW_SECURITY_HOTSPOTS_REVIEWED
51 ).collect(toMap(Metric::getKey, Metric::getBestValue));
52 static final Set<String> EXISTENCY_REQUIREMENTS = Set.of(
53 NEW_DUPLICATED_LINES_DENSITY_KEY,
56 public static final Set<Metric<? extends Serializable>> CAYC_METRICS = Set.of(
58 NEW_SECURITY_HOTSPOTS_REVIEWED,
59 NEW_DUPLICATED_LINES_DENSITY,
63 private final DbClient dbClient;
65 public QualityGateCaycChecker(DbClient dbClient) {
66 this.dbClient = dbClient;
69 public QualityGateCaycStatus checkCaycCompliant(DbSession dbSession, String qualityGateUuid) {
70 var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid)
72 .collect(Collectors.toMap(QualityGateConditionDto::getMetricUuid, Function.identity()));
74 if (conditionsByMetricId.size() < CAYC_METRICS.size()) {
78 var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet())
80 .filter(MetricDto::isEnabled)
83 long count = metrics.stream()
84 .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric))
87 if (metrics.size() == count && count == CAYC_METRICS.size()) {
89 } else if (metrics.size() > count && count == CAYC_METRICS.size()) {
90 return OVER_COMPLIANT;
96 public QualityGateCaycStatus checkCaycCompliantFromProject(DbSession dbSession, String projectUuid) {
97 return Optional.ofNullable(dbClient.qualityGateDao().selectByProjectUuid(dbSession, projectUuid))
98 .or(() -> Optional.ofNullable(dbClient.qualityGateDao().selectDefault(dbSession)))
99 .map(qualityGate -> checkCaycCompliant(dbSession, qualityGate.getUuid()))
100 .orElse(NON_COMPLIANT);
103 public boolean isCaycCondition(MetricDto metric) {
104 return CAYC_METRICS.stream().map(Metric::getKey).anyMatch(metric.getKey()::equals);
107 private static boolean checkMetricCaycCompliant(QualityGateConditionDto condition, MetricDto metric) {
108 if (EXISTENCY_REQUIREMENTS.contains(metric.getKey())) {
110 } else if (BEST_VALUE_REQUIREMENTS.containsKey(metric.getKey())) {
111 Double errorThreshold = Double.valueOf(condition.getErrorThreshold());
112 Double caycRequiredThreshold = BEST_VALUE_REQUIREMENTS.get(metric.getKey());
113 return caycRequiredThreshold.compareTo(errorThreshold) == 0;