use of org.sonar.server.computation.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()).toJson();
Measure detailsMeasure = Measure.newMeasureBuilder().create(detailMeasureValue);
Metric qgDetailsMetric = metricRepository.getByKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
measureRepository.add(project, qgDetailsMetric, detailsMeasure);
}
use of org.sonar.server.computation.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();
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("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
}
use of org.sonar.server.computation.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();
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("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
reset(measureRepository, eventRepository, notificationService);
}
use of org.sonar.server.computation.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 alterStatus for project %s is not a supported value. Can not compute Quality Gate event", project.getKey()));
checkNewQualityGate(project, rawStatus);
return;
}
QualityGateStatus baseStatus = baseMeasure.get().getQualityGateStatus();
if (baseStatus.getStatus() != rawStatus.getStatus()) {
// The QualityGate status has changed
String label = String.format("%s (was %s)", rawStatus.getStatus().getColorName(), baseStatus.getStatus().getColorName());
createEvent(project, label, rawStatus.getText());
boolean isNewKo = rawStatus.getStatus() == Measure.Level.OK;
notifyUsers(project, label, rawStatus, isNewKo);
}
}
use of org.sonar.server.computation.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(Collectors.index(Condition::getMetric, java.util.function.Function.identity()));
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;
}
MetricEvaluationResult metricEvaluationResult = evaluateQualityGate(measure.get(), entry.getValue());
String text = evaluationResultTextConverter.asText(metricEvaluationResult.condition, metricEvaluationResult.evaluationResult);
builder.addLabel(text);
Measure updatedMeasure = Measure.updatedMeasureBuilder(measure.get()).setQualityGateStatus(new QualityGateStatus(metricEvaluationResult.evaluationResult.getLevel(), text)).create();
measureRepository.update(project, metric, updatedMeasure);
builder.addEvaluatedCondition(metricEvaluationResult);
}
}
Aggregations