]> source.dussan.org Git - sonarqube.git/blob
9c30c2f01f3e7c01c3e680db46431d473ff5961a
[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.ComponentDto;
39 import org.sonar.server.notification.NotificationService;
40
41 import static com.google.common.base.Preconditions.checkArgument;
42 import static java.lang.String.format;
43 import static java.util.Collections.singleton;
44
45 public class ReportAnalysisFailureNotificationExecutionListener implements CeWorker.ExecutionListener {
46   private final NotificationService notificationService;
47   private final DbClient dbClient;
48   private final ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer;
49   private final System2 system2;
50
51   public ReportAnalysisFailureNotificationExecutionListener(NotificationService notificationService, DbClient dbClient,
52     ReportAnalysisFailureNotificationSerializer taskFailureNotificationSerializer, System2 system2) {
53     this.notificationService = notificationService;
54     this.dbClient = dbClient;
55     this.taskFailureNotificationSerializer = taskFailureNotificationSerializer;
56     this.system2 = system2;
57   }
58
59   @Override
60   public void onStart(CeTask ceTask) {
61     // nothing to do
62   }
63
64   @Override
65   public void onEnd(CeTask ceTask, CeActivityDto.Status status, Duration duration, @Nullable CeTaskResult taskResult, @Nullable Throwable error) {
66     if (status == CeActivityDto.Status.SUCCESS) {
67       return;
68     }
69     String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null);
70     if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) {
71       return;
72     }
73
74     if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.class))) {
75       try (DbSession dbSession = dbClient.openSession(false)) {
76         ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid);
77         checkScopeAndQualifier(projectDto);
78         CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid())
79           .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid())));
80         ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, error);
81         ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification);
82         notificationService.deliverEmails(singleton(notification));
83
84         // compatibility with old API
85         notificationService.deliver(notification);
86       }
87     }
88   }
89
90   /**
91    * @throws IllegalArgumentException if specified {@link ComponentDto} is not a project.
92    */
93   private static void checkScopeAndQualifier(ComponentDto projectDto) {
94     String scope = projectDto.scope();
95     String qualifier = projectDto.qualifier();
96     checkArgument(
97       scope.equals(Scopes.PROJECT) && qualifier.equals(Qualifiers.PROJECT),
98       "Component %s must be a project (scope=%s, qualifier=%s)", projectDto.uuid(), scope, qualifier);
99   }
100
101   private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, @Nullable Throwable error) {
102     Long executedAt = ceActivityDto.getExecutedAt();
103     return new ReportAnalysisFailureNotificationBuilder(
104       new ReportAnalysisFailureNotificationBuilder.Project(
105         projectDto.uuid(),
106         projectDto.getKey(),
107         projectDto.name(),
108         projectDto.getBranch()),
109       new ReportAnalysisFailureNotificationBuilder.Task(
110         ceActivityDto.getUuid(),
111         ceActivityDto.getSubmittedAt(),
112         executedAt == null ? system2.now() : executedAt),
113       error == null ? null : error.getMessage());
114   }
115 }