Search in sources :

Example 1 with Condition

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

the class WebhookPayloadFactoryImplTest method create_payload_for_successful_analysis.

@Test
public void create_payload_for_successful_analysis() {
    CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
    Condition condition = new Condition("coverage", Condition.Operator.GREATER_THAN, "70.0");
    EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder().setQualityGate(new QualityGate("G1", "Gate One", singleton(condition))).setStatus(Metric.Level.ERROR).addEvaluatedCondition(condition, EvaluatedCondition.EvaluationStatus.ERROR, "74.0").build();
    ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
    WebhookPayload payload = underTest.create(analysis);
    assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
    assertJson(payload.getJson()).isSimilarTo("{" + "  \"serverUrl\": \"http://foo\"," + "  \"taskId\": \"#1\"," + "  \"status\": \"SUCCESS\"," + "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," + "  \"revision\": \"sha1\"," + "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," + "  \"project\": {" + "    \"key\": \"P1\"," + "    \"name\": \"Project One\"," + "    \"url\": \"http://foo/dashboard?id=P1\"" + "  }," + "  \"qualityGate\": {" + "    \"name\": \"Gate One\"," + "    \"status\": \"ERROR\"," + "    \"conditions\": [" + "      {" + "        \"metric\": \"coverage\"," + "        \"operator\": \"GREATER_THAN\"," + "        \"value\": \"74.0\"," + "        \"status\": \"ERROR\"," + "        \"errorThreshold\": \"70.0\"" + "      }" + "    ]" + "  }," + "  \"properties\": {" + "  }" + "}");
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

Example 2 with Condition

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

the class WebhookPayloadFactoryImplTest method create_payload_with_gate_conditions_without_value.

@Test
public void create_payload_with_gate_conditions_without_value() {
    CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
    Condition condition = new Condition("coverage", Condition.Operator.GREATER_THAN, "70.0");
    EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder().setQualityGate(new QualityGate("G1", "Gate One", singleton(condition))).setStatus(Metric.Level.ERROR).addEvaluatedCondition(condition, EvaluatedCondition.EvaluationStatus.NO_VALUE, null).build();
    ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
    WebhookPayload payload = underTest.create(analysis);
    assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
    assertJson(payload.getJson()).isSimilarTo("{" + "  \"serverUrl\": \"http://foo\"," + "  \"taskId\": \"#1\"," + "  \"status\": \"SUCCESS\"," + "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," + "  \"revision\": \"sha1\"," + "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," + "  \"project\": {" + "    \"key\": \"P1\"," + "    \"name\": \"Project One\"," + "    \"url\": \"http://foo/dashboard?id=P1\"" + "  }," + "  \"qualityGate\": {" + "    \"name\": \"Gate One\"," + "    \"status\": \"ERROR\"," + "    \"conditions\": [" + "      {" + "        \"metric\": \"coverage\"," + "        \"operator\": \"GREATER_THAN\"," + "        \"status\": \"NO_VALUE\"," + "        \"errorThreshold\": \"70.0\"" + "      }" + "    ]" + "  }" + "}");
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

Example 3 with Condition

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

the class WebhookPostTaskTest method callWebHooks.

private void callWebHooks(@Nullable String analysisUUid, @Nullable QualityGate qualityGate) {
    Project project = newProjectBuilder().setUuid(randomAlphanumeric(3)).setKey(randomAlphanumeric(4)).setName(randomAlphanumeric(5)).build();
    CeTask ceTask = newCeTaskBuilder().setStatus(CeTask.Status.values()[random.nextInt(CeTask.Status.values().length)]).setId(randomAlphanumeric(6)).build();
    Date date = new Date();
    Map<String, String> properties = ImmutableMap.of(randomAlphanumeric(17), randomAlphanumeric(18));
    Branch branch = newBranchBuilder().setIsMain(random.nextBoolean()).setType(Branch.Type.values()[random.nextInt(Branch.Type.values().length)]).setName(randomAlphanumeric(29)).build();
    PostProjectAnalysisTaskTester.of(underTest).at(date).withCeTask(ceTask).withProject(project).withBranch(branch).withQualityGate(qualityGate).withScannerContext(newScannerContextBuilder().addProperties(properties).build()).withAnalysisUuid(analysisUUid).withQualityGate(qualityGate).execute();
    ArgumentCaptor<Supplier> supplierCaptor = ArgumentCaptor.forClass(Supplier.class);
    verify(webHooks).sendProjectAnalysisUpdate(eq(new WebHooks.Analysis(project.getUuid(), analysisUUid, ceTask.getId())), supplierCaptor.capture(), any(LogStatistics.class));
    assertThat(supplierCaptor.getValue().get()).isSameAs(webhookPayload);
    EvaluatedQualityGate webQualityGate = null;
    if (qualityGate != null) {
        QualityGate.Condition condition = qualityGate.getConditions().iterator().next();
        Condition qgCondition = new Condition(condition.getMetricKey(), Condition.Operator.valueOf(condition.getOperator().name()), condition.getErrorThreshold());
        webQualityGate = EvaluatedQualityGate.newBuilder().setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition))).setStatus(Metric.Level.valueOf(qualityGate.getStatus().name())).addEvaluatedCondition(qgCondition, EvaluatedCondition.EvaluationStatus.valueOf(condition.getStatus().name()), condition.getValue()).build();
    }
    verify(payloadFactory).create(new ProjectAnalysis(new org.sonar.server.webhook.Project(project.getUuid(), project.getKey(), project.getName()), new org.sonar.server.webhook.CeTask(ceTask.getId(), org.sonar.server.webhook.CeTask.Status.valueOf(ceTask.getStatus().name())), analysisUUid == null ? null : new Analysis(analysisUUid, date.getTime(), null), new org.sonar.server.webhook.Branch(branch.isMain(), branch.getName().get(), org.sonar.server.webhook.Branch.Type.valueOf(branch.getType().name())), webQualityGate, analysisUUid == null ? null : date.getTime(), properties));
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) CeTask(org.sonar.api.ce.posttask.CeTask) Date(java.util.Date) QualityGate(org.sonar.api.ce.posttask.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Project(org.sonar.api.ce.posttask.Project) ProjectAnalysis(org.sonar.server.webhook.ProjectAnalysis) Branch(org.sonar.api.ce.posttask.Branch) Analysis(org.sonar.server.webhook.Analysis) ProjectAnalysis(org.sonar.server.webhook.ProjectAnalysis) LogStatistics(org.sonar.api.ce.posttask.PostProjectAnalysisTask.LogStatistics) Supplier(java.util.function.Supplier) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Example 4 with Condition

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

the class LiveQualityGateComputerImplTest method getMetricsRelatedTo.

@Test
public void getMetricsRelatedTo() {
    Condition condition = new Condition("metric1", Condition.Operator.GREATER_THAN, "10");
    QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
    Set<String> result = underTest.getMetricsRelatedTo(gate);
    assertThat(result).containsExactlyInAnyOrder(// the metrics needed to compute the status of gate
    condition.getMetricKey(), // generated metrics
    CoreMetrics.ALERT_STATUS_KEY, CoreMetrics.QUALITY_GATE_DETAILS_KEY);
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

Example 5 with Condition

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

the class LiveQualityGateComputerImplTest method refreshGateStatus_generates_gate_related_measures.

@Test
public void refreshGateStatus_generates_gate_related_measures() {
    ComponentDto project = ComponentTesting.newPublicProjectDto();
    MetricDto conditionMetric = newMetricDto();
    MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
    MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
    Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10");
    QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
    MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList());
    EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix, configuration);
    QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
    assertThat(measures.get(conditionMetric.getKey())).isEmpty();
    assertThat(result.getStatus()).isEqualTo(Metric.Level.OK);
    assertThat(result.getEvaluatedConditions()).extracting(EvaluatedCondition::getStatus).containsExactly(EvaluatedCondition.EvaluationStatus.OK);
    assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name());
    assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString()).isNotEmpty().startsWith("{").endsWith("}");
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) MetricDto(org.sonar.db.metric.MetricDto) ComponentDto(org.sonar.db.component.ComponentDto) QualityGateEvaluator(org.sonar.server.qualitygate.QualityGateEvaluator) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

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