*/
package org.sonar.server.qualitygate;
+import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import static java.util.stream.Collectors.toUnmodifiableMap;
+import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
+import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
public class QualityGateCaycChecker {
- public static final List<Metric<Integer>> CAYC_METRICS = List.of(
+ public static final List<Metric<? extends Serializable>> CAYC_METRICS = List.of(
NEW_MAINTAINABILITY_RATING,
NEW_RELIABILITY_RATING,
NEW_SECURITY_HOTSPOTS_REVIEWED,
- NEW_SECURITY_RATING
+ NEW_SECURITY_RATING,
+ NEW_DUPLICATED_LINES_DENSITY,
+ NEW_COVERAGE
);
private static final Map<String, Double> CAYC_REQUIREMENTS = CAYC_METRICS.stream()
*/
package org.sonar.server.qualitygate.ws;
+import java.io.Serializable;
import java.util.Map;
+import java.util.Objects;
+import org.sonar.api.measures.Metric;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates.CreateResponse;
+import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY;
+import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
import static org.sonar.api.measures.Metric.DIRECTION_BETTER;
import static org.sonar.api.measures.Metric.DIRECTION_WORST;
import static org.sonar.server.qualitygate.Condition.Operator.GREATER_THAN;
public class CreateAction implements QualityGatesWsAction {
+ static final Map<String, Integer> DEFAULT_METRIC_VALUES = Map.of(
+ NEW_COVERAGE_KEY, 80,
+ NEW_DUPLICATED_LINES_DENSITY_KEY, 3
+ );
+
private static final Map<Integer, Condition.Operator> OPERATORS_BY_DIRECTION = Map.of(
DIRECTION_BETTER, LESS_THAN,
DIRECTION_WORST, GREATER_THAN);
private void addCaycConditions(DbSession dbSession, QualityGateDto newQualityGate) {
CAYC_METRICS.forEach(m ->
qualityGateConditionsUpdater.createCondition(dbSession, newQualityGate, m.getKey(), OPERATORS_BY_DIRECTION.get(m.getDirection()).getDbValue(),
- String.valueOf(m.getBestValue().intValue()))
+ String.valueOf(getDefaultCaycValue(m)))
);
}
+
+ private static int getDefaultCaycValue(Metric<? extends Serializable> metric) {
+ return DEFAULT_METRIC_VALUES.containsKey(metric.getKey()) ?
+ DEFAULT_METRIC_VALUES.get(metric.getKey()) :
+ Objects.requireNonNull(metric.getBestValue()).intValue();
+ }
}
*/
package org.sonar.server.qualitygate;
+import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
+import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
@Test
public void checkCaycCompliant() {
String qualityGateUuid = "abcd";
- List<Metric<Integer>> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING);
+ List<Metric<? extends Serializable>> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING,
+ NEW_DUPLICATED_LINES_DENSITY, NEW_COVERAGE);
CAYC_REQUIREMENT_METRICS
.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue();
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.metric.MetricDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.ForbiddenException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS;
+import static org.sonar.server.qualitygate.ws.CreateAction.DEFAULT_METRIC_VALUES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
@RunWith(DataProviderRunner.class)
CAYC_METRICS.stream()
.map(m -> dbClient.metricDao().selectByKey(dbSession, m.getKey()))
- .forEach(metricDto ->
- assertThat(conditions).anyMatch(c -> metricDto.getUuid().equals(c.getMetricUuid())));
+ .forEach(metricDto -> assertThat(conditions)
+ .anyMatch(c -> metricDto.getUuid().equals(c.getMetricUuid()) && c.getErrorThreshold().equals(String.valueOf(getDefaultCaycValue(metricDto)))));
}
@Test
.setHidden(metric.isHidden())
.setDirection(metric.getDirection()));
}
+
+ private Integer getDefaultCaycValue(MetricDto metricDto) {
+ return DEFAULT_METRIC_VALUES.containsKey(metricDto.getKey())
+ ? DEFAULT_METRIC_VALUES.get(metricDto.getKey())
+ : CAYC_METRICS.stream()
+ .filter(metric -> metricDto.getKey().equals(metric.getKey()))
+ .findAny()
+ .orElseThrow()
+ .getBestValue().intValue();
+ }
+
}