Search in sources :

Example 1 with QualityGateStatus

use of org.sonar.ce.task.projectanalysis.measure.QualityGateStatus in project sonarqube by SonarSource.

the class QualityGateEventsStep method checkQualityGateStatusChange.

private void checkQualityGateStatusChange(Component project, Metric metric, QualityGateStatus rawStatus) {
    Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(project, metric);
    if (!baseMeasure.isPresent()) {
        checkNewQualityGate(project, rawStatus);
        return;
    }
    if (!baseMeasure.get().hasQualityGateStatus()) {
        LOGGER.warn(String.format("Previous Quality gate status for project %s is not a supported value. Can not compute Quality Gate event", project.getDbKey()));
        checkNewQualityGate(project, rawStatus);
        return;
    }
    QualityGateStatus baseStatus = baseMeasure.get().getQualityGateStatus();
    if (baseStatus.getStatus() != rawStatus.getStatus()) {
        // The QualityGate status has changed
        createEvent(project, rawStatus.getStatus().getLabel(), rawStatus.getText());
        boolean isNewKo = rawStatus.getStatus() == Measure.Level.OK;
        notifyUsers(project, rawStatus, isNewKo);
    }
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Measure(org.sonar.ce.task.projectanalysis.measure.Measure)

Example 2 with QualityGateStatus

use of org.sonar.ce.task.projectanalysis.measure.QualityGateStatus in project sonarqube by SonarSource.

the class QualityGateMeasuresStep method updateMeasures.

private void updateMeasures(Component project, Set<Condition> conditions, QualityGateDetailsDataBuilder builder) {
    Multimap<Metric, Condition> conditionsPerMetric = conditions.stream().collect(MoreCollectors.index(Condition::getMetric, java.util.function.Function.identity()));
    boolean ignoredConditions = false;
    for (Map.Entry<Metric, Collection<Condition>> entry : conditionsPerMetric.asMap().entrySet()) {
        Metric metric = entry.getKey();
        Optional<Measure> measure = measureRepository.getRawMeasure(project, metric);
        if (!measure.isPresent()) {
            continue;
        }
        final MetricEvaluationResult metricEvaluationResult = evaluateQualityGate(measure.get(), entry.getValue());
        final MetricEvaluationResult finalMetricEvaluationResult;
        if (smallChangesetQualityGateSpecialCase.appliesTo(project, metricEvaluationResult)) {
            finalMetricEvaluationResult = smallChangesetQualityGateSpecialCase.apply(metricEvaluationResult);
            ignoredConditions = true;
        } else {
            finalMetricEvaluationResult = metricEvaluationResult;
        }
        String text = evaluationResultTextConverter.asText(finalMetricEvaluationResult.condition, finalMetricEvaluationResult.evaluationResult);
        builder.addLabel(text);
        Measure updatedMeasure = Measure.updatedMeasureBuilder(measure.get()).setQualityGateStatus(new QualityGateStatus(finalMetricEvaluationResult.evaluationResult.getLevel(), text)).create();
        measureRepository.update(project, metric, updatedMeasure);
        builder.addEvaluatedCondition(finalMetricEvaluationResult);
    }
    builder.setIgnoredConditions(ignoredConditions);
}
Also used : EvaluatedCondition(org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Collection(java.util.Collection) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with QualityGateStatus

use of org.sonar.ce.task.projectanalysis.measure.QualityGateStatus in project sonarqube by SonarSource.

the class QualityGateEventsStepTest method verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed.

private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus, QualityGateStatus newQualityGateStatus, String expectedLabel) {
    when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
    when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
    underTest.execute(new TestComputationStepContext());
    verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
    verifyNoMoreInteractions(measureRepository, eventRepository);
    Event event = eventArgumentCaptor.getValue();
    assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
    assertThat(event.getName()).isEqualTo(expectedLabel);
    assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
    assertThat(event.getData()).isNull();
    verify(notificationService).deliver(notificationArgumentCaptor.capture());
    Notification notification = notificationArgumentCaptor.getValue();
    assertThat(notification.getType()).isEqualTo("alerts");
    assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
    assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
    assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getProjectVersion());
    assertThat(notification.getFieldValue("branch")).isNull();
    assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
    assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
    reset(measureRepository, eventRepository, notificationService);
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Event(org.sonar.ce.task.projectanalysis.event.Event) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Notification(org.sonar.api.notifications.Notification) QGChangeNotification(org.sonar.server.qualitygate.notification.QGChangeNotification)

Example 4 with QualityGateStatus

use of org.sonar.ce.task.projectanalysis.measure.QualityGateStatus in project sonarqube by SonarSource.

the class QualityGateEventsStepTest method verify_event_created_if_no_base_ALERT_STATUS_measure.

private void verify_event_created_if_no_base_ALERT_STATUS_measure(Measure.Level rawAlterStatus, String expectedLabel) {
    QualityGateStatus someQGStatus = new QualityGateStatus(rawAlterStatus, ALERT_TEXT);
    when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
    when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
    underTest.execute(new TestComputationStepContext());
    verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
    verifyNoMoreInteractions(measureRepository, eventRepository);
    Event event = eventArgumentCaptor.getValue();
    assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
    assertThat(event.getName()).isEqualTo(expectedLabel);
    assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
    assertThat(event.getData()).isNull();
    ArgumentCaptor<Collection> collectionCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(notificationService).deliverEmails(collectionCaptor.capture());
    verify(notificationService).deliver(notificationArgumentCaptor.capture());
    Notification notification = notificationArgumentCaptor.getValue();
    assertThat(collectionCaptor.getValue()).hasSize(1);
    assertThat(collectionCaptor.getValue().iterator().next()).isSameAs(notification);
    assertThat(notification).isInstanceOf(QGChangeNotification.class);
    assertThat(notification.getType()).isEqualTo("alerts");
    assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
    assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
    assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getProjectVersion());
    assertThat(notification.getFieldValue("branch")).isNull();
    assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
    assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Event(org.sonar.ce.task.projectanalysis.event.Event) Collection(java.util.Collection) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Notification(org.sonar.api.notifications.Notification) QGChangeNotification(org.sonar.server.qualitygate.notification.QGChangeNotification)

Example 5 with QualityGateStatus

use of org.sonar.ce.task.projectanalysis.measure.QualityGateStatus in project sonarqube by SonarSource.

the class QualityGateMeasuresStep method addProjectMeasure.

private void addProjectMeasure(Component project, QualityGateDetailsDataBuilder builder) {
    Measure globalMeasure = Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(builder.getGlobalLevel(), StringUtils.join(builder.getLabels(), ", "))).create(builder.getGlobalLevel());
    Metric metric = metricRepository.getByKey(CoreMetrics.ALERT_STATUS_KEY);
    measureRepository.add(project, metric, globalMeasure);
    String detailMeasureValue = new QualityGateDetailsData(builder.getGlobalLevel(), builder.getEvaluatedConditions(), builder.isIgnoredConditions()).toJson();
    Measure detailsMeasure = Measure.newMeasureBuilder().create(detailMeasureValue);
    Metric qgDetailsMetric = metricRepository.getByKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
    measureRepository.add(project, qgDetailsMetric, detailsMeasure);
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) QualityGateDetailsData(org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData) Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Metric(org.sonar.ce.task.projectanalysis.metric.Metric)

Aggregations

QualityGateStatus (org.sonar.ce.task.projectanalysis.measure.QualityGateStatus)7 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)4 Notification (org.sonar.api.notifications.Notification)3 Measure (org.sonar.ce.task.projectanalysis.measure.Measure)3 QGChangeNotification (org.sonar.server.qualitygate.notification.QGChangeNotification)3 Collection (java.util.Collection)2 Test (org.junit.Test)2 Event (org.sonar.ce.task.projectanalysis.event.Event)2 Metric (org.sonar.ce.task.projectanalysis.metric.Metric)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 DefaultBranchImpl (org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl)1 EvaluatedCondition (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition)1 QualityGateDetailsData (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData)1 Condition (org.sonar.ce.task.projectanalysis.qualitygate.Condition)1