3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.api.posttask;
22 import com.google.common.base.Function;
24 import javax.annotation.Nonnull;
25 import org.sonar.api.ce.posttask.QualityGate;
26 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
27 import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus;
29 import static com.google.common.base.Preconditions.checkState;
30 import static java.lang.String.format;
33 * Converts a {@link Condition} from the Compute Engine internal API to a {@link org.sonar.api.ce.posttask.QualityGate.Condition}
36 class ConditionToCondition implements Function<Condition, QualityGate.Condition> {
37 private final ConditionImpl.Builder builder = ConditionImpl.newBuilder();
38 private final Map<Condition, ConditionStatus> statusPerConditions;
40 public ConditionToCondition(Map<Condition, ConditionStatus> statusPerConditions) {
41 this.statusPerConditions = statusPerConditions;
46 public QualityGate.Condition apply(Condition input) {
47 String metricKey = input.getMetric().getKey();
48 ConditionStatus conditionStatus = statusPerConditions.get(input);
49 checkState(conditionStatus != null, "Missing ConditionStatus for condition on metric key %s", metricKey);
51 .setStatus(convert(conditionStatus.getStatus()))
52 .setMetricKey(metricKey)
53 .setOperator(convert(input.getOperator()))
54 .setErrorThreshold(input.getErrorThreshold())
55 .setValue(conditionStatus.getValue())
59 private static QualityGate.EvaluationStatus convert(ConditionStatus.EvaluationStatus status) {
62 return QualityGate.EvaluationStatus.NO_VALUE;
64 return QualityGate.EvaluationStatus.OK;
66 return QualityGate.EvaluationStatus.ERROR;
68 throw new IllegalArgumentException(format(
69 "Unsupported value '%s' of ConditionStatus.EvaluationStatus can not be converted to QualityGate.EvaluationStatus",
74 private static QualityGate.Operator convert(Condition.Operator operator) {
77 return QualityGate.Operator.EQUALS;
79 return QualityGate.Operator.NOT_EQUALS;
81 return QualityGate.Operator.GREATER_THAN;
83 return QualityGate.Operator.LESS_THAN;
85 throw new IllegalArgumentException(format(
86 "Unsupported value '%s' of Condition.Operation can not be converted to QualityGate.Operator",