3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Date;
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;
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}.
47 public class SendIssueNotificationsStep implements ComputationStep {
49 * Types of the notifications sent by this step
51 static final Set<String> NOTIF_TYPES = ImmutableSet.of(IssueChangeNotification.TYPE, NewIssuesNotification.TYPE, MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE);
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;
60 public SendIssueNotificationsStep(IssueCache issueCache, RuleRepository rules, TreeRootHolder treeRootHolder,
61 NotificationService service, AnalysisMetadataHolder analysisMetadataHolder,
62 NewIssuesNotificationFactory newIssuesNotificationFactory) {
63 this.issueCache = issueCache;
65 this.treeRootHolder = treeRootHolder;
66 this.service = service;
67 this.analysisMetadataHolder = analysisMetadataHolder;
68 this.newIssuesNotificationFactory = newIssuesNotificationFactory;
72 public void execute() {
73 Component project = treeRootHolder.getRoot();
74 if (service.hasProjectSubscribersForTypes(project.getUuid(), NOTIF_TYPES)) {
79 private void doExecute(Component project) {
80 NewIssuesStatistics newIssuesStats = new NewIssuesStatistics();
81 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
83 processIssues(newIssuesStats, issues, project);
87 if (newIssuesStats.hasIssues()) {
88 long analysisDate = analysisMetadataHolder.getAnalysisDate();
89 sendNewIssuesNotification(newIssuesStats, project, analysisDate);
90 sendNewIssuesNotificationToAssignees(newIssuesStats, project, analysisDate);
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);
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);
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);
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());
138 service.deliver(myNewIssuesNotification);
143 public String getDescription() {
144 return "Send issue notifications";
147 private String getBranchName() {
148 return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).map(Branch::getName).orElse(null);
151 private String getMainBranchProjectKey() {
152 return analysisMetadataHolder.getProject().getKey();