2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.core.issue.notification;
22 import org.sonar.api.batch.PostJob;
23 import org.sonar.api.batch.SensorContext;
24 import org.sonar.api.issue.internal.DefaultIssue;
25 import org.sonar.api.issue.internal.IssueChangeContext;
26 import org.sonar.api.resources.Project;
27 import org.sonar.api.rules.Rule;
28 import org.sonar.api.rules.RuleFinder;
29 import org.sonar.batch.issue.IssueCache;
30 import org.sonar.core.DryRunIncompatible;
31 import org.sonar.core.issue.IssueNotifications;
32 import org.sonar.core.issue.IssuesBySeverity;
34 import java.util.LinkedHashMap;
41 public class SendIssueNotificationsPostJob implements PostJob {
43 private final IssueCache issueCache;
44 private final IssueNotifications notifications;
45 private final RuleFinder ruleFinder;
47 public SendIssueNotificationsPostJob(IssueCache issueCache, IssueNotifications notifications, RuleFinder ruleFinder) {
48 this.issueCache = issueCache;
49 this.notifications = notifications;
50 this.ruleFinder = ruleFinder;
54 public void executeOn(Project project, SensorContext context) {
55 sendNotifications(project);
58 private void sendNotifications(Project project) {
59 IssuesBySeverity newIssues = new IssuesBySeverity();
60 IssueChangeContext context = IssueChangeContext.createScan(project.getAnalysisDate());
61 Map<DefaultIssue, Rule> changedIssuesRuleMap = new LinkedHashMap<>();
62 for (DefaultIssue issue : issueCache.all()) {
65 } else if (hasChangedAndNeedNotification(issue)) {
66 addIssueToMap(issue, changedIssuesRuleMap);
69 sendChangedIssues(project, context, changedIssuesRuleMap);
70 sendNewIssues(project, newIssues);
73 private void addIssueToMap(DefaultIssue issue, Map<DefaultIssue, Rule> changedIssuesRuleMap) {
74 Rule rule = ruleFinder.findByKey(issue.ruleKey());
75 // TODO warning - rules with status REMOVED are currently ignored, but should not
77 changedIssuesRuleMap.put(issue, rule);
81 private boolean isNew(DefaultIssue issue) {
82 return issue.isNew() && issue.resolution() == null;
85 private boolean hasChangedAndNeedNotification(DefaultIssue issue) {
86 return !issue.isNew() && issue.isChanged() && issue.mustSendNotifications();
89 private void sendChangedIssues(Project project, IssueChangeContext context, Map<DefaultIssue, Rule> changedIssuesRuleMap) {
90 if (!changedIssuesRuleMap.isEmpty()) {
91 notifications.sendChanges(changedIssuesRuleMap, context, project, null, null);
95 private void sendNewIssues(Project project, IssuesBySeverity newIssues) {
96 if (!newIssues.isEmpty()) {
97 notifications.sendNewIssues(project, newIssues);