]> source.dussan.org Git - sonarqube.git/blob
b0e752ec7d4ecfaa924ecbf9e8f6ab6276d9500c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
21
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Date;
24 import java.util.Map;
25 import java.util.Set;
26 import org.sonar.core.issue.DefaultIssue;
27 import org.sonar.core.util.CloseableIterator;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
33 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository;
34 import org.sonar.server.computation.task.step.ComputationStep;
35 import org.sonar.server.issue.notification.IssueChangeNotification;
36 import org.sonar.server.issue.notification.MyNewIssuesNotification;
37 import org.sonar.server.issue.notification.NewIssuesNotification;
38 import org.sonar.server.issue.notification.NewIssuesNotificationFactory;
39 import org.sonar.server.issue.notification.NewIssuesStatistics;
40 import org.sonar.server.notification.NotificationService;
41
42 /**
43  * Reads issues from disk cache and send related notifications. For performance reasons,
44  * the standard notification DB queue is not used as a temporary storage. Notifications
45  * are directly processed by {@link NotificationService}.
46  */
47 public class SendIssueNotificationsStep implements ComputationStep {
48   /**
49    * Types of the notifications sent by this step
50    */
51   static final Set<String> NOTIF_TYPES = ImmutableSet.of(IssueChangeNotification.TYPE, NewIssuesNotification.TYPE, MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE);
52
53   private final IssueCache issueCache;
54   private final RuleRepository rules;
55   private final TreeRootHolder treeRootHolder;
56   private final NotificationService service;
57   private final AnalysisMetadataHolder analysisMetadataHolder;
58   private NewIssuesNotificationFactory newIssuesNotificationFactory;
59
60   public SendIssueNotificationsStep(IssueCache issueCache, RuleRepository rules, TreeRootHolder treeRootHolder,
61     NotificationService service, AnalysisMetadataHolder analysisMetadataHolder,
62     NewIssuesNotificationFactory newIssuesNotificationFactory) {
63     this.issueCache = issueCache;
64     this.rules = rules;
65     this.treeRootHolder = treeRootHolder;
66     this.service = service;
67     this.analysisMetadataHolder = analysisMetadataHolder;
68     this.newIssuesNotificationFactory = newIssuesNotificationFactory;
69   }
70
71   @Override
72   public void execute() {
73     Component project = treeRootHolder.getRoot();
74     if (service.hasProjectSubscribersForTypes(project.getUuid(), NOTIF_TYPES)) {
75       doExecute(project);
76     }
77   }
78
79   private void doExecute(Component project) {
80     NewIssuesStatistics newIssuesStats = new NewIssuesStatistics();
81     CloseableIterator<DefaultIssue> issues = issueCache.traverse();
82     try {
83       processIssues(newIssuesStats, issues, project);
84     } finally {
85       issues.close();
86     }
87     if (newIssuesStats.hasIssues()) {
88       long analysisDate = analysisMetadataHolder.getAnalysisDate();
89       sendNewIssuesNotification(newIssuesStats, project, analysisDate);
90       sendNewIssuesNotificationToAssignees(newIssuesStats, project, analysisDate);
91     }
92   }
93
94   private void processIssues(NewIssuesStatistics newIssuesStats, CloseableIterator<DefaultIssue> issues, Component project) {
95     while (issues.hasNext()) {
96       DefaultIssue issue = issues.next();
97       if (issue.isNew() && issue.resolution() == null) {
98         newIssuesStats.add(issue);
99       } else if (issue.isChanged() && issue.mustSendNotifications()) {
100         sendIssueChangeNotification(issue, project);
101       }
102     }
103   }
104
105   private void sendIssueChangeNotification(DefaultIssue issue, Component project) {
106     IssueChangeNotification changeNotification = new IssueChangeNotification();
107     changeNotification.setRuleName(rules.getByKey(issue.ruleKey()).getName());
108     changeNotification.setIssue(issue);
109     changeNotification.setProject(getMainBranchProjectKey(), project.getName(), getBranchName());
110     service.deliver(changeNotification);
111   }
112
113   private void sendNewIssuesNotification(NewIssuesStatistics statistics, Component project, long analysisDate) {
114     NewIssuesStatistics.Stats globalStatistics = statistics.globalStatistics();
115     NewIssuesNotification notification = newIssuesNotificationFactory
116       .newNewIssuesNotication()
117       .setProject(getMainBranchProjectKey(), project.getUuid(), project.getName(), getBranchName())
118       .setAnalysisDate(new Date(analysisDate))
119       .setStatistics(project.getName(), globalStatistics)
120       .setDebt(globalStatistics.debt());
121     service.deliver(notification);
122   }
123
124   private void sendNewIssuesNotificationToAssignees(NewIssuesStatistics statistics, Component project, long analysisDate) {
125     // send email to each user having issues
126     for (Map.Entry<String, NewIssuesStatistics.Stats> assigneeAndStatisticsTuple : statistics.assigneesStatistics().entrySet()) {
127       String assignee = assigneeAndStatisticsTuple.getKey();
128       NewIssuesStatistics.Stats assigneeStatistics = assigneeAndStatisticsTuple.getValue();
129       MyNewIssuesNotification myNewIssuesNotification = newIssuesNotificationFactory
130         .newMyNewIssuesNotification()
131         .setAssignee(assignee);
132       myNewIssuesNotification
133         .setProject(getMainBranchProjectKey(), project.getUuid(), project.getName(), getBranchName())
134         .setAnalysisDate(new Date(analysisDate))
135         .setStatistics(project.getName(), assigneeStatistics)
136         .setDebt(assigneeStatistics.debt());
137
138       service.deliver(myNewIssuesNotification);
139     }
140   }
141
142   @Override
143   public String getDescription() {
144     return "Send issue notifications";
145   }
146
147   private String getBranchName() {
148     return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).flatMap(Branch::getName).orElse(null);
149   }
150
151   private String getMainBranchProjectKey() {
152     return analysisMetadataHolder.getProject().getKey();
153   }
154
155 }