Search in sources :

Example 6 with Condition

use of org.sonar.server.qualitygate.Condition in project sonarqube by SonarSource.

the class LiveQualityGateComputerImpl method loadQualityGate.

@Override
public QualityGate loadQualityGate(DbSession dbSession, ProjectDto project, BranchDto branch) {
    QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, project).getQualityGate();
    Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getUuid());
    Set<String> metricUuids = conditionDtos.stream().map(QualityGateConditionDto::getMetricUuid).collect(toHashSet(conditionDtos.size()));
    Map<String, MetricDto> metricsByUuid = dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().collect(uniqueIndex(MetricDto::getUuid));
    Stream<Condition> conditions = conditionDtos.stream().map(conditionDto -> {
        String metricKey = metricsByUuid.get(conditionDto.getMetricUuid()).getKey();
        Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator());
        return new Condition(metricKey, operator, conditionDto.getErrorThreshold());
    });
    if (branch.getBranchType() == BranchType.PULL_REQUEST) {
        conditions = conditions.filter(Condition::isOnLeakPeriod);
    }
    return new QualityGate(String.valueOf(gateDto.getUuid()), gateDto.getName(), conditions.collect(toHashSet(conditionDtos.size())));
}
Also used : Condition(org.sonar.server.qualitygate.Condition) MetricDto(org.sonar.db.metric.MetricDto) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto) QualityGateDto(org.sonar.db.qualitygate.QualityGateDto) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Example 7 with Condition

use of org.sonar.server.qualitygate.Condition in project sonarqube by SonarSource.

the class WebhookPayloadFactoryImpl method writeQualityGate.

private static void writeQualityGate(JsonWriter writer, EvaluatedQualityGate gate) {
    writer.name("qualityGate").beginObject().prop("name", gate.getQualityGate().getName()).prop(PROPERTY_STATUS, gate.getStatus().toString()).name("conditions").beginArray();
    for (EvaluatedCondition evaluatedCondition : gate.getEvaluatedConditions()) {
        Condition condition = evaluatedCondition.getCondition();
        writer.beginObject().prop("metric", condition.getMetricKey()).prop("operator", condition.getOperator().name());
        evaluatedCondition.getValue().ifPresent(t -> writer.prop("value", t));
        writer.prop(PROPERTY_STATUS, evaluatedCondition.getStatus().name()).prop("errorThreshold", condition.getErrorThreshold()).endObject();
    }
    writer.endArray().endObject();
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition)

Example 8 with Condition

use of org.sonar.server.qualitygate.Condition in project sonarqube by SonarSource.

the class QGToEvaluatedQG method apply.

@Override
public EvaluatedQualityGate apply(QualityGate qg) {
    EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder();
    Set<Condition> conditions = qg.getConditions().stream().map(q -> {
        Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold());
        builder.addEvaluatedCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == EvaluationStatus.NO_VALUE ? null : q.getValue());
        return condition;
    }).collect(MoreCollectors.toSet());
    return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions)).setStatus(Metric.Level.valueOf(qg.getStatus().name())).build();
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) QualityGate(org.sonar.api.ce.posttask.QualityGate) EvaluationStatus(org.sonar.api.ce.posttask.QualityGate.EvaluationStatus) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Set(java.util.Set) Function(java.util.function.Function) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Metric(org.sonar.api.measures.Metric) Condition(org.sonar.server.qualitygate.Condition) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Aggregations

Condition (org.sonar.server.qualitygate.Condition)8 EvaluatedCondition (org.sonar.server.qualitygate.EvaluatedCondition)7 EvaluatedQualityGate (org.sonar.server.qualitygate.EvaluatedQualityGate)7 QualityGate (org.sonar.server.qualitygate.QualityGate)5 Test (org.junit.Test)4 QualityGate (org.sonar.api.ce.posttask.QualityGate)2 MetricDto (org.sonar.db.metric.MetricDto)2 Date (java.util.Date)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Branch (org.sonar.api.ce.posttask.Branch)1 CeTask (org.sonar.api.ce.posttask.CeTask)1 LogStatistics (org.sonar.api.ce.posttask.PostProjectAnalysisTask.LogStatistics)1 Project (org.sonar.api.ce.posttask.Project)1 EvaluationStatus (org.sonar.api.ce.posttask.QualityGate.EvaluationStatus)1 Metric (org.sonar.api.measures.Metric)1 MoreCollectors (org.sonar.core.util.stream.MoreCollectors)1 ComponentDto (org.sonar.db.component.ComponentDto)1 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)1