3 * Copyright (C) 2009-2024 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.ArrayList;
23 import java.util.Collection;
24 import java.util.EnumSet;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import javax.annotation.Nullable;
33 import org.sonar.api.measures.Metric.ValueType;
34 import org.sonar.core.util.Uuids;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.metric.MetricDto;
38 import org.sonar.db.qualitygate.QualityGateConditionDto;
39 import org.sonar.db.qualitygate.QualityGateDto;
40 import org.sonar.server.exceptions.NotFoundException;
41 import org.sonar.server.measure.Rating;
42 import org.sonar.server.qualitygate.ws.StandardToMQRMetrics;
44 import static com.google.common.base.Strings.isNullOrEmpty;
45 import static java.lang.Double.parseDouble;
46 import static java.lang.Integer.parseInt;
47 import static java.lang.Long.parseLong;
48 import static java.lang.String.format;
49 import static java.util.Arrays.stream;
50 import static java.util.Objects.requireNonNull;
51 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
53 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
54 import static org.sonar.api.measures.Metric.DIRECTION_BETTER;
55 import static org.sonar.api.measures.Metric.DIRECTION_NONE;
56 import static org.sonar.api.measures.Metric.DIRECTION_WORST;
57 import static org.sonar.api.measures.Metric.ValueType.RATING;
58 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
59 import static org.sonar.server.exceptions.BadRequestException.throwBadRequestException;
60 import static org.sonar.server.measure.Rating.E;
61 import static org.sonar.server.qualitygate.Condition.Operator.GREATER_THAN;
62 import static org.sonar.server.qualitygate.Condition.Operator.LESS_THAN;
63 import static org.sonar.server.qualitygate.ValidRatingMetrics.isCoreRatingMetric;
64 import static org.sonar.server.qualitygate.ValidRatingMetrics.isSoftwareQualityRatingMetric;
66 public class QualityGateConditionsUpdater {
67 public static final Set<String> INVALID_METRIC_KEYS = Stream.of(ALERT_STATUS_KEY, SECURITY_HOTSPOTS_KEY, NEW_SECURITY_HOTSPOTS_KEY)
68 .collect(Collectors.toUnmodifiableSet());
70 private static final Map<Integer, Set<Condition.Operator>> VALID_OPERATORS_BY_DIRECTION = Map.of(
71 DIRECTION_NONE, Set.of(GREATER_THAN, LESS_THAN),
72 DIRECTION_BETTER, Set.of(LESS_THAN),
73 DIRECTION_WORST, Set.of(GREATER_THAN));
75 private static final EnumSet<ValueType> VALID_METRIC_TYPES = EnumSet.of(
84 private static final List<String> RATING_VALID_INT_VALUES = stream(Rating.values()).map(r -> Integer.toString(r.getIndex())).toList();
86 private final DbClient dbClient;
88 public QualityGateConditionsUpdater(DbClient dbClient) {
89 this.dbClient = dbClient;
93 public QualityGateConditionDto createCondition(DbSession dbSession, QualityGateDto qualityGate, String metricKey, String operator,
94 String errorThreshold) {
95 MetricDto metric = getNonNullMetric(dbSession, metricKey);
96 validateCondition(metric, operator, errorThreshold);
97 Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate.getUuid());
98 checkConditionDoesNotExistOnSameMetric(conditions, metric);
99 checkConditionDoesNotExistOnEquivalentMetric(dbSession, conditions, metric);
100 QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateUuid(qualityGate.getUuid())
101 .setUuid(Uuids.create())
102 .setMetricUuid(metric.getUuid()).setMetricKey(metric.getKey())
103 .setOperator(operator)
104 .setErrorThreshold(errorThreshold);
105 dbClient.gateConditionDao().insert(newCondition, dbSession);
109 public QualityGateConditionDto updateCondition(DbSession dbSession, QualityGateConditionDto condition, String metricKey, String operator,
110 String errorThreshold) {
111 MetricDto metric = getNonNullMetric(dbSession, metricKey);
112 validateCondition(metric, operator, errorThreshold);
113 Collection<QualityGateConditionDto> otherConditions = getConditions(dbSession, condition.getQualityGateUuid())
115 .filter(c -> !c.getUuid().equals(condition.getUuid()))
117 checkConditionDoesNotExistOnEquivalentMetric(dbSession, otherConditions, metric);
119 .setMetricUuid(metric.getUuid())
120 .setMetricKey(metric.getKey())
121 .setOperator(operator)
122 .setErrorThreshold(errorThreshold);
123 dbClient.gateConditionDao().update(condition, dbSession);
127 private MetricDto getNonNullMetric(DbSession dbSession, String metricKey) {
128 MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey);
129 if (metric == null) {
130 throw new NotFoundException(format("There is no metric with key=%s", metricKey));
135 private Collection<QualityGateConditionDto> getConditions(DbSession dbSession, String qGateUuid) {
136 return dbClient.gateConditionDao().selectForQualityGate(dbSession, qGateUuid);
139 private static void validateCondition(MetricDto metric, String operator, String errorThreshold) {
140 List<String> errors = new ArrayList<>();
141 validateMetric(metric, errors);
142 checkOperator(metric, operator, errors);
143 checkErrorThreshold(metric, errorThreshold, errors);
144 checkRatingMetric(metric, errorThreshold, errors);
145 checkRequest(errors.isEmpty(), errors);
148 private static void validateMetric(MetricDto metric, List<String> errors) {
149 check(isValid(metric), errors, "Metric '%s' cannot be used to define a condition.", metric.getKey());
152 private static boolean isValid(MetricDto metric) {
153 return !metric.isHidden()
154 && VALID_METRIC_TYPES.contains(ValueType.valueOf(metric.getValueType()))
155 && !INVALID_METRIC_KEYS.contains(metric.getKey());
158 private static void checkOperator(MetricDto metric, String operator, List<String> errors) {
160 Condition.Operator.isValid(operator) && isAllowedOperator(operator, metric),
162 "Operator %s is not allowed for this metric.", operator);
165 private static void checkErrorThreshold(MetricDto metric, String errorThreshold, List<String> errors) {
166 requireNonNull(errorThreshold, "errorThreshold can not be null");
167 validateErrorThresholdValue(metric, errorThreshold, errors);
170 private static void checkConditionDoesNotExistOnSameMetric(Collection<QualityGateConditionDto> conditions, MetricDto metric) {
171 if (conditions.isEmpty()) {
175 boolean conditionExists = conditions.stream().anyMatch(c -> c.getMetricUuid().equals(metric.getUuid()));
176 checkRequest(!conditionExists, format("Condition on metric '%s' already exists.", metric.getShortName()));
179 private void checkConditionDoesNotExistOnEquivalentMetric(DbSession dbSession, Collection<QualityGateConditionDto> conditions, MetricDto metric) {
180 Optional<String> equivalentMetric = StandardToMQRMetrics.getEquivalentMetric(metric.getKey());
182 if (conditions.isEmpty() || equivalentMetric.isEmpty()) {
186 MetricDto equivalentMetricDto = dbClient.metricDao().selectByKey(dbSession, equivalentMetric.get());
187 boolean conditionExists = conditions.stream()
188 .anyMatch(c -> equivalentMetricDto != null && c.getMetricUuid().equals(equivalentMetricDto.getUuid()));
190 if (conditionExists) {
191 throwBadRequestException(
192 format("Condition for metric '%s' already exists on equivalent metric '%s''.", metric.getKey(), equivalentMetricDto.getKey()));
196 private static boolean isAllowedOperator(String operator, MetricDto metric) {
197 if (VALID_OPERATORS_BY_DIRECTION.containsKey(metric.getDirection())) {
198 return VALID_OPERATORS_BY_DIRECTION.get(metric.getDirection()).contains(Condition.Operator.fromDbValue(operator));
204 private static void validateErrorThresholdValue(MetricDto metric, String errorThreshold, List<String> errors) {
206 ValueType valueType = ValueType.valueOf(metric.getValueType());
208 case BOOL, INT, RATING:
209 parseInt(errorThreshold);
211 case MILLISEC, WORK_DUR:
212 parseLong(errorThreshold);
215 parseDouble(errorThreshold);
220 throw new IllegalArgumentException(format("Unsupported value type %s. Cannot convert condition value", valueType));
222 } catch (Exception e) {
223 errors.add(format("Invalid value '%s' for metric '%s'", errorThreshold, metric.getShortName()));
227 private static void checkRatingMetric(MetricDto metric, String errorThreshold, List<String> errors) {
228 if (!metric.getValueType().equals(RATING.name())) {
231 if (!isCoreRatingMetric(metric.getKey()) && !isSoftwareQualityRatingMetric(metric.getKey())) {
232 errors.add(format("The metric '%s' cannot be used", metric.getShortName()));
234 if (!isValidRating(errorThreshold)) {
235 addInvalidRatingError(errorThreshold, errors);
238 checkRatingGreaterThanOperator(errorThreshold, errors);
241 private static void addInvalidRatingError(@Nullable String value, List<String> errors) {
242 errors.add(format("'%s' is not a valid rating", value));
245 private static void checkRatingGreaterThanOperator(@Nullable String value, List<String> errors) {
246 check(isNullOrEmpty(value) || !Objects.equals(toRating(value), E), errors, "There's no worse rating than E (%s)", value);
249 private static Rating toRating(String value) {
250 return Rating.valueOf(parseInt(value));
253 private static boolean isValidRating(@Nullable String value) {
254 return isNullOrEmpty(value) || RATING_VALID_INT_VALUES.contains(value);
257 private static boolean check(boolean expression, List<String> errors, String message, String... args) {
259 errors.add(format(message, args));