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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations