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.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.Date;
26 import java.util.Optional;
28 import org.sonar.core.issue.DefaultIssue;
29 import org.sonar.core.util.CloseableIterator;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
34 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
36 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
37 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
38 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository;
39 import org.sonar.server.computation.task.step.ComputationStep;
40 import org.sonar.server.issue.notification.IssueChangeNotification;
41 import org.sonar.server.issue.notification.MyNewIssuesNotification;
42 import org.sonar.server.issue.notification.NewIssuesNotification;
43 import org.sonar.server.issue.notification.NewIssuesNotificationFactory;
44 import org.sonar.server.issue.notification.NewIssuesStatistics;
45 import org.sonar.server.notification.NotificationService;
47 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
50 * Reads issues from disk cache and send related notifications. For performance reasons,
51 * the standard notification DB queue is not used as a temporary storage. Notifications
52 * are directly processed by {@link NotificationService}.
54 public class SendIssueNotificationsStep implements ComputationStep {
56 * Types of the notifications sent by this step
58 static final Set<String> NOTIF_TYPES = ImmutableSet.of(IssueChangeNotification.TYPE, NewIssuesNotification.TYPE, MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE);
60 private final IssueCache issueCache;
61 private final RuleRepository rules;
62 private final TreeRootHolder treeRootHolder;
63 private final NotificationService service;
64 private final AnalysisMetadataHolder analysisMetadataHolder;
65 private final NewIssuesNotificationFactory newIssuesNotificationFactory;
66 private Map<String, Component> componentsByDbKey;
68 public SendIssueNotificationsStep(IssueCache issueCache, RuleRepository rules, TreeRootHolder treeRootHolder,
69 NotificationService service, AnalysisMetadataHolder analysisMetadataHolder,
70 NewIssuesNotificationFactory newIssuesNotificationFactory) {
71 this.issueCache = issueCache;
73 this.treeRootHolder = treeRootHolder;
74 this.service = service;
75 this.analysisMetadataHolder = analysisMetadataHolder;
76 this.newIssuesNotificationFactory = newIssuesNotificationFactory;
80 public void execute() {
81 Component project = treeRootHolder.getRoot();
82 if (service.hasProjectSubscribersForTypes(project.getUuid(), NOTIF_TYPES)) {
87 private void doExecute(Component project) {
88 NewIssuesStatistics newIssuesStats = new NewIssuesStatistics();
89 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
91 processIssues(newIssuesStats, issues, project);
95 if (newIssuesStats.hasIssues()) {
96 long analysisDate = analysisMetadataHolder.getAnalysisDate();
97 sendNewIssuesNotification(newIssuesStats, project, analysisDate);
98 sendNewIssuesNotificationToAssignees(newIssuesStats, project, analysisDate);
102 private void processIssues(NewIssuesStatistics newIssuesStats, CloseableIterator<DefaultIssue> issues, Component project) {
103 while (issues.hasNext()) {
104 DefaultIssue issue = issues.next();
105 if (issue.isNew() && issue.resolution() == null) {
106 newIssuesStats.add(issue);
107 } else if (issue.isChanged() && issue.mustSendNotifications()) {
108 sendIssueChangeNotification(issue, project);
113 private void sendIssueChangeNotification(DefaultIssue issue, Component project) {
114 IssueChangeNotification changeNotification = new IssueChangeNotification();
115 changeNotification.setRuleName(rules.getByKey(issue.ruleKey()).getName());
116 changeNotification.setIssue(issue);
117 changeNotification.setProject(project.getPublicKey(), project.getName(), getBranchName());
118 getComponentKey(issue).ifPresent(c -> changeNotification.setComponent(c.getPublicKey(), c.getName()));
119 service.deliver(changeNotification);
122 private void sendNewIssuesNotification(NewIssuesStatistics statistics, Component project, long analysisDate) {
123 NewIssuesStatistics.Stats globalStatistics = statistics.globalStatistics();
124 NewIssuesNotification notification = newIssuesNotificationFactory
125 .newNewIssuesNotication()
126 .setProject(project.getPublicKey(), project.getUuid(), project.getName(), getBranchName())
127 .setAnalysisDate(new Date(analysisDate))
128 .setStatistics(project.getName(), globalStatistics)
129 .setDebt(globalStatistics.debt());
130 service.deliver(notification);
133 private void sendNewIssuesNotificationToAssignees(NewIssuesStatistics statistics, Component project, long analysisDate) {
134 // send email to each user having issues
135 for (Map.Entry<String, NewIssuesStatistics.Stats> assigneeAndStatisticsTuple : statistics.assigneesStatistics().entrySet()) {
136 String assignee = assigneeAndStatisticsTuple.getKey();
137 NewIssuesStatistics.Stats assigneeStatistics = assigneeAndStatisticsTuple.getValue();
138 MyNewIssuesNotification myNewIssuesNotification = newIssuesNotificationFactory
139 .newMyNewIssuesNotification()
140 .setAssignee(assignee);
141 myNewIssuesNotification
142 .setProject(project.getPublicKey(), project.getUuid(), project.getName(), getBranchName())
143 .setAnalysisDate(new Date(analysisDate))
144 .setStatistics(project.getName(), assigneeStatistics)
145 .setDebt(assigneeStatistics.debt());
147 service.deliver(myNewIssuesNotification);
151 private Optional<Component> getComponentKey(DefaultIssue issue) {
152 if (componentsByDbKey == null) {
153 final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
154 new DepthTraversalTypeAwareCrawler(
155 new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, POST_ORDER) {
157 public void visitAny(Component component) {
158 builder.put(component.getKey(), component);
160 }).visit(this.treeRootHolder.getRoot());
161 this.componentsByDbKey = builder.build();
163 return Optional.ofNullable(componentsByDbKey.get(issue.componentKey()));
167 public String getDescription() {
168 return "Send issue notifications";
171 private String getBranchName() {
172 return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).map(Branch::getName).orElse(null);