]> source.dussan.org Git - sonarqube.git/blob
942135a776ca805c20657666ce3af05edc40a1e0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.resources.Scopes;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.CeTask;
28 import org.sonar.ce.task.CeTaskResult;
29 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification;
30 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder;
31 import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationSerializer;
32 import org.sonar.ce.taskprocessor.CeWorker;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.RowNotFoundException;
36 import org.sonar.db.ce.CeActivityDto;
37 import org.sonar.db.ce.CeTaskTypes;
38 import org.sonar.db.component.BranchDto;
39 import org.sonar.db.component.ComponentDto;
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         ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid);
78         BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, projectDto.branchUuid())
79           .orElseThrow(() -> new IllegalStateException("Could not find a branch for component uuid " + projectDto.uuid()));
80         checkScopeAndQualifier(projectDto);
81         CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid())
82           .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid())));
83         ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, branchDto, error);
84         ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification);
85         notificationService.deliverEmails(singleton(notification));
86
87         // compatibility with old API
88         notificationService.deliver(notification);
89       }
90     }
91   }
92
93   /**
94    * @throws IllegalArgumentException if specified {@link ComponentDto} is not a project.
95    */
96   private static void checkScopeAndQualifier(ComponentDto projectDto) {
97     String scope = projectDto.scope();
98     String qualifier = projectDto.qualifier();
99     checkArgument(
100       scope.equals(Scopes.PROJECT) && qualifier.equals(Qualifiers.PROJECT),
101       "Component %s must be a project (scope=%s, qualifier=%s)", projectDto.uuid(), scope, qualifier);
102   }
103
104   private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, BranchDto branchDto,
105     @Nullable Throwable error) {
106     String projectBranch = branchDto.isMain() ? null : branchDto.getBranchKey();
107     Long executedAt = ceActivityDto.getExecutedAt();
108     return new ReportAnalysisFailureNotificationBuilder(
109       new ReportAnalysisFailureNotificationBuilder.Project(
110         projectDto.uuid(),
111         projectDto.getKey(),
112         projectDto.name(),
113         projectBranch),
114       new ReportAnalysisFailureNotificationBuilder.Task(
115         ceActivityDto.getUuid(),
116         ceActivityDto.getSubmittedAt(),
117         executedAt == null ? system2.now() : executedAt),
118       error == null ? null : error.getMessage());
119   }
120 }