]> source.dussan.org Git - sonarqube.git/blob
db89507ff10d380d09b7ca6db3e59eb4a2217dce
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.notification;
21
22 import java.time.Duration;
23 import javax.annotation.Nullable;
24 import org.sonar.api.resources.Qualifiers;
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.BranchDto;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.project.ProjectDto;
40 import org.sonar.server.notification.NotificationService;
41
42 import static com.google.common.base.Preconditions.checkArgument;
43 import static java.lang.String.format;
44 import static java.util.Collections.singleton;
45
46 public class ReportAnalysisFailureNotificationExecutionListener implements CeWorker.ExecutionListener {
47   private final NotificationService notificationService;
48   private final DbClient dbClient;
49   private final ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer;
50   private final System2 system2;
51
52   public ReportAnalysisFailureNotificationExecutionListener(NotificationService notificationService, DbClient dbClient,
53     ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer, System2 system2) {
54     this.notificationService = notificationService;
55     this.dbClient = dbClient;
56     this.taskFailureNotificationSerializer = taskFailureNotificationSerializer;
57     this.system2 = system2;
58   }
59
60   @Override
61   public void onStart(CeTask ceTask) {
62     // nothing to do
63   }
64
65   @Override
66   public void onEnd(CeTask ceTask, CeActivityDto.Status status, Duration duration, @Nullable CeTaskResult taskResult, @Nullable Throwable error) {
67     if (status == CeActivityDto.Status.SUCCESS) {
68       return;
69     }
70     String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null);
71     if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) {
72       return;
73     }
74
75     if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.class))) {
76       try (DbSession dbSession = dbClient.openSession(false)) {
77         ProjectDto projectDto = dbClient.projectDao().selectByUuid(dbSession, projectUuid)
78           .orElseThrow(() -> new IllegalStateException("Could not find project uuid " + projectUuid));
79         BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, projectDto.getUuid())
80           .orElseThrow(() -> new IllegalStateException("Could not find a branch for project uuid " + projectDto.getUuid()));
81         checkQualifier(projectDto);
82         CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid())
83           .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid())));
84         ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, branchDto, error);
85         ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification);
86         notificationService.deliverEmails(singleton(notification));
87
88         // compatibility with old API
89         notificationService.deliver(notification);
90       }
91     }
92   }
93
94   /**
95    * @throws IllegalArgumentException if specified {@link ComponentDto} is not a project.
96    */
97   private static void checkQualifier(ProjectDto projectDto) {
98     String qualifier = projectDto.getQualifier();
99     checkArgument(qualifier.equals(Qualifiers.PROJECT), "Component %s must be a project (qualifier=%s)", projectDto.getUuid(), qualifier);
100   }
101
102   private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ProjectDto projectDto, BranchDto branchDto,
103     @Nullable Throwable error) {
104     String projectBranch = branchDto.isMain() ? null : branchDto.getBranchKey();
105     Long executedAt = ceActivityDto.getExecutedAt();
106     return new ReportAnalysisFailureNotificationBuilder(
107       new ReportAnalysisFailureNotificationBuilder.Project(
108         projectDto.getUuid(),
109         projectDto.getKey(),
110         projectDto.getName(),
111         projectBranch),
112       new ReportAnalysisFailureNotificationBuilder.Task(
113         ceActivityDto.getUuid(),
114         ceActivityDto.getSubmittedAt(),
115         executedAt == null ? system2.now() : executedAt),
116       error == null ? null : error.getMessage());
117   }
118 }