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.ce.task.projectanalysis.measure.qualitygatedetails;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonObject;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import java.util.stream.StreamSupport;
27 import javax.annotation.concurrent.Immutable;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
29 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
30 import org.sonar.server.qualitygate.ConditionComparator;
32 import static java.util.Objects.requireNonNull;
35 public class QualityGateDetailsData {
36 private static final String FIELD_LEVEL = "level";
37 private static final String FIELD_IGNORED_CONDITIONS = "ignoredConditions";
39 private final Measure.Level level;
40 private final List<EvaluatedCondition> conditions;
41 private final boolean ignoredConditions;
43 public QualityGateDetailsData(Measure.Level level, Iterable<EvaluatedCondition> conditions, boolean ignoredConditions) {
44 this.level = requireNonNull(level);
45 this.conditions = StreamSupport.stream(conditions.spliterator(), false)
46 .sorted(new ConditionComparator<>(c -> c.getCondition().getMetric().getKey()))
47 .collect(Collectors.toList());
48 this.ignoredConditions = ignoredConditions;
51 public String toJson() {
52 JsonObject details = new JsonObject();
53 details.addProperty(FIELD_LEVEL, level.toString());
54 JsonArray conditionResults = new JsonArray();
55 for (EvaluatedCondition condition : this.conditions) {
56 conditionResults.add(toJson(condition));
58 details.add("conditions", conditionResults);
59 details.addProperty(FIELD_IGNORED_CONDITIONS, ignoredConditions);
60 return details.toString();
63 private static JsonObject toJson(EvaluatedCondition evaluatedCondition) {
64 Condition condition = evaluatedCondition.getCondition();
66 JsonObject result = new JsonObject();
67 result.addProperty("metric", condition.getMetric().getKey());
68 result.addProperty("op", condition.getOperator().getDbValue());
69 if (condition.useVariation()) {
70 // without this for new_ metrics, the UI will show "-" instead of
71 // the actual value in the QG failure reason
72 result.addProperty("period", 1);
74 result.addProperty("error", condition.getErrorThreshold());
75 result.addProperty("actual", evaluatedCondition.getActualValue());
76 result.addProperty(FIELD_LEVEL, evaluatedCondition.getLevel().name());