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