Search in sources :

Example 1 with ReportAnalysisFailureNotificationBuilder

use of org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder in project sonarqube by SonarSource.

the class ReportAnalysisFailureNotificationExecutionListener method onEnd.

@Override
public void onEnd(CeTask ceTask, CeActivityDto.Status status, Duration duration, @Nullable CeTaskResult taskResult, @Nullable Throwable error) {
    if (status == CeActivityDto.Status.SUCCESS) {
        return;
    }
    String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null);
    if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) {
        return;
    }
    if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.class))) {
        try (DbSession dbSession = dbClient.openSession(false)) {
            ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid);
            checkScopeAndQualifier(projectDto);
            CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid()).orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid())));
            ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, error);
            ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification);
            notificationService.deliverEmails(singleton(notification));
            // compatibility with old API
            notificationService.deliver(notification);
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) ReportAnalysisFailureNotificationBuilder(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder) CeActivityDto(org.sonar.db.ce.CeActivityDto) RowNotFoundException(org.sonar.db.RowNotFoundException) ComponentDto(org.sonar.db.component.ComponentDto) CeTask(org.sonar.ce.task.CeTask) ReportAnalysisFailureNotification(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification)

Example 2 with ReportAnalysisFailureNotificationBuilder

use of org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder in project sonarqube by SonarSource.

the class ReportAnalysisFailureNotificationExecutionListenerTest method onEnd_creates_notification_with_null_error_message_if_Throwable_is_null.

@Test
public void onEnd_creates_notification_with_null_error_message_if_Throwable_is_null() {
    String taskUuid = randomAlphanumeric(12);
    initMocksToPassConditions(taskUuid, random.nextInt(999_999), (long) random.nextInt(999_999));
    Notification notificationMock = mockSerializer();
    underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, null);
    verify(notificationService).deliver(same(notificationMock));
    ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
    ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
    assertThat(reportAnalysisFailureNotificationBuilder.getErrorMessage()).isNull();
}
Also used : ReportAnalysisFailureNotificationBuilder(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder) Notification(org.sonar.api.notifications.Notification) ReportAnalysisFailureNotification(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification) Test(org.junit.Test)

Example 3 with ReportAnalysisFailureNotificationBuilder

use of org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder in project sonarqube by SonarSource.

the class ReportAnalysisFailureNotificationExecutionListenerTest method onEnd_creates_notification_with_error_message_from_Throwable_argument_message.

@Test
public void onEnd_creates_notification_with_error_message_from_Throwable_argument_message() {
    initMocksToPassConditions(randomAlphanumeric(12), random.nextInt(999_999), (long) random.nextInt(999_999));
    String message = randomAlphanumeric(66);
    when(throwableMock.getMessage()).thenReturn(message);
    underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
    ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
    ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
    assertThat(reportAnalysisFailureNotificationBuilder.getErrorMessage()).isEqualTo(message);
}
Also used : ReportAnalysisFailureNotificationBuilder(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder) Test(org.junit.Test)

Example 4 with ReportAnalysisFailureNotificationBuilder

use of org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder in project sonarqube by SonarSource.

the class ReportAnalysisFailureNotificationExecutionListenerTest method onEnd_uses_system_data_as_failedAt_if_task_has_no_executedAt.

@Test
public void onEnd_uses_system_data_as_failedAt_if_task_has_no_executedAt() {
    String taskUuid = randomAlphanumeric(12);
    initMocksToPassConditions(taskUuid, random.nextInt(999_999), null);
    long now = random.nextInt(999_999);
    when(system2.now()).thenReturn(now);
    Notification notificationMock = mockSerializer();
    underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, null);
    verify(notificationService).deliver(same(notificationMock));
    ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
    assertThat(notificationCaptor.getValue().getTask().getFailedAt()).isEqualTo(now);
}
Also used : ReportAnalysisFailureNotificationBuilder(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder) Notification(org.sonar.api.notifications.Notification) ReportAnalysisFailureNotification(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification) Test(org.junit.Test)

Example 5 with ReportAnalysisFailureNotificationBuilder

use of org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder in project sonarqube by SonarSource.

the class ReportAnalysisFailureNotificationExecutionListenerTest method onEnd_creates_notification_with_data_from_activity_and_project_and_deliver_it.

@Test
public void onEnd_creates_notification_with_data_from_activity_and_project_and_deliver_it() {
    String taskUuid = randomAlphanumeric(12);
    int createdAt = random.nextInt(999_999);
    long executedAt = random.nextInt(999_999);
    ComponentDto project = initMocksToPassConditions(taskUuid, createdAt, executedAt);
    Notification notificationMock = mockSerializer();
    underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
    ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
    verify(notificationService).deliver(same(notificationMock));
    ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
    ReportAnalysisFailureNotificationBuilder.Project notificationProject = reportAnalysisFailureNotificationBuilder.getProject();
    assertThat(notificationProject.getName()).isEqualTo(project.name());
    assertThat(notificationProject.getKey()).isEqualTo(project.getKey());
    assertThat(notificationProject.getUuid()).isEqualTo(project.uuid());
    assertThat(notificationProject.getBranchName()).isEqualTo(project.getBranch());
    ReportAnalysisFailureNotificationBuilder.Task notificationTask = reportAnalysisFailureNotificationBuilder.getTask();
    assertThat(notificationTask.getUuid()).isEqualTo(taskUuid);
    assertThat(notificationTask.getCreatedAt()).isEqualTo(createdAt);
    assertThat(notificationTask.getFailedAt()).isEqualTo(executedAt);
}
Also used : ReportAnalysisFailureNotificationBuilder(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder) ComponentDto(org.sonar.db.component.ComponentDto) Notification(org.sonar.api.notifications.Notification) ReportAnalysisFailureNotification(org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification) Test(org.junit.Test)

Aggregations

ReportAnalysisFailureNotificationBuilder (org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder)5 Test (org.junit.Test)4 ReportAnalysisFailureNotification (org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification)4 Notification (org.sonar.api.notifications.Notification)3 ComponentDto (org.sonar.db.component.ComponentDto)2 CeTask (org.sonar.ce.task.CeTask)1 DbSession (org.sonar.db.DbSession)1 RowNotFoundException (org.sonar.db.RowNotFoundException)1 CeActivityDto (org.sonar.db.ce.CeActivityDto)1