3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.notification;
22 import javax.annotation.Nullable;
23 import org.sonar.api.resources.Qualifiers;
24 import org.sonar.api.resources.Scopes;
25 import org.sonar.api.utils.System2;
26 import org.sonar.ce.task.CeTask;
27 import org.sonar.ce.task.CeTaskResult;
28 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification;
29 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder;
30 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationSerializer;
31 import org.sonar.ce.taskprocessor.CeWorker;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.RowNotFoundException;
35 import org.sonar.db.ce.CeActivityDto;
36 import org.sonar.db.ce.CeTaskTypes;
37 import org.sonar.db.component.ComponentDto;
38 import org.sonar.server.notification.NotificationService;
40 import static com.google.common.base.Preconditions.checkArgument;
41 import static java.lang.String.format;
42 import static java.util.Collections.singleton;
44 public class ReportAnalysisFailureNotificationExecutionListener implements CeWorker.ExecutionListener {
45 private final NotificationService notificationService;
46 private final DbClient dbClient;
47 private final ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer;
48 private final System2 system2;
50 public ReportAnalysisFailureNotificationExecutionListener(NotificationService notificationService, DbClient dbClient,
51 ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer, System2 system2) {
52 this.notificationService = notificationService;
53 this.dbClient = dbClient;
54 this.taskFailureNotificationSerializer = taskFailureNotificationSerializer;
55 this.system2 = system2;
59 public void onStart(CeTask ceTask) {
64 public void onEnd(CeTask ceTask, CeActivityDto.Status status, @Nullable CeTaskResult taskResult, @Nullable Throwable error) {
65 if (status == CeActivityDto.Status.SUCCESS) {
68 String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null);
69 if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) {
73 if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.class))) {
74 try (DbSession dbSession = dbClient.openSession(false)) {
75 ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid);
76 checkScopeAndQualifier(projectDto);
77 CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid())
78 .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid())));
79 ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, error);
80 ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification);
81 notificationService.deliverEmails(singleton(notification));
83 // compatibility with old API
84 notificationService.deliver(notification);
90 * @throws IllegalArgumentException if specified {@link ComponentDto} is not a project.
92 private static void checkScopeAndQualifier(ComponentDto projectDto) {
93 String scope = projectDto.scope();
94 String qualifier = projectDto.qualifier();
96 scope.equals(Scopes.PROJECT) && qualifier.equals(Qualifiers.PROJECT),
97 "Component %s must be a project (scope=%s, qualifier=%s)", projectDto.uuid(), scope, qualifier);
100 private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, @Nullable Throwable error) {
101 Long executedAt = ceActivityDto.getExecutedAt();
102 return new ReportAnalysisFailureNotificationBuilder(
103 new ReportAnalysisFailureNotificationBuilder.Project(
107 projectDto.getBranch()),
108 new ReportAnalysisFailureNotificationBuilder.Task(
109 ceActivityDto.getUuid(),
110 ceActivityDto.getSubmittedAt(),
111 executedAt == null ? system2.now() : executedAt),
112 error == null ? null : error.getMessage());