]> source.dussan.org Git - sonarqube.git/commitdiff
Do not send notifications on issue changes when no field diff
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 3 Jun 2013 08:14:28 +0000 (10:14 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 3 Jun 2013 08:14:28 +0000 (10:14 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java
sonar-core/src/main/java/org/sonar/core/issue/IssueNotifications.java
sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java

index d77560fa216ac9301482aa13af4ab8dccd00baed..32a3d0a5b22b8b130ff8cb85596ce05f08a681cb 100644 (file)
@@ -56,7 +56,7 @@ public class SendIssueNotificationsPostJob implements PostJob {
       if (issue.isNew() && issue.resolution() == null) {
         newIssues++;
       }
-      if (issue.isChanged() && issue.diffs() != null) {
+      if (issue.isChanged()) {
         Rule rule = ruleFinder.findByKey(issue.ruleKey());
         // TODO warning - rules with status REMOVED are currently ignored, but should not
         if (rule != null) {
index 51c08844168cbda89dc14f826e2d6d2f54db0574..f10e4da4ff6ab65aa9d3124275623a8b98c57e13 100644 (file)
@@ -35,7 +35,6 @@ import org.sonar.core.i18n.RuleI18nManager;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-
 import java.util.Locale;
 import java.util.Map;
 
@@ -70,22 +69,27 @@ public class IssueNotifications implements BatchComponent, ServerComponent {
 
   @CheckForNull
   public Notification sendChanges(DefaultIssue issue, IssueChangeContext context, Rule rule, Component project, @Nullable Component component) {
-    if (issue.diffs() != null) {
-      Notification notification = createNotification(issue, context, rule, project, component, null);
+    Notification notification = createChangeNotification(issue, context, rule, project, component, null);
+    if (notification != null) {
       notificationsManager.scheduleForSending(notification);
-      return notification;
     }
-    return null;
+    return notification;
   }
 
   @CheckForNull
   public Notification sendChanges(DefaultIssue issue, IssueChangeContext context, IssueQueryResult queryResult, @Nullable String comment) {
-    Notification notification = createNotification(issue, context, queryResult.rule(issue), queryResult.project(issue), queryResult.component(issue), comment);
-    notificationsManager.scheduleForSending(notification);
+    Notification notification = createChangeNotification(issue, context, queryResult.rule(issue), queryResult.project(issue), queryResult.component(issue), comment);
+    if (notification != null) {
+      notificationsManager.scheduleForSending(notification);
+    }
     return notification;
   }
 
-  private Notification createNotification(DefaultIssue issue, IssueChangeContext context, Rule rule, Component project, @Nullable Component component, @Nullable String comment){
+  @CheckForNull
+  private Notification createChangeNotification(DefaultIssue issue, IssueChangeContext context, Rule rule, Component project, @Nullable Component component, @Nullable String comment) {
+    if (comment == null && (issue.diffs() == null || issue.diffs().diffs().isEmpty())) {
+      return null;
+    }
     Notification notification = newNotification(project, "issue-changes");
     notification.setFieldValue("key", issue.key());
     notification.setFieldValue("changeAuthor", context.login());
index d94bd13d28fcbb3517591c7aec11a75bf10a8a41..eae906010a993d709bb52e0d9d79a36611842351 100644 (file)
@@ -111,4 +111,21 @@ public class IssueNotificationsTest {
     assertThat(notification.getFieldValue("comment")).isEqualTo("I don't know how to fix it?");
     Mockito.verify(manager).scheduleForSending(notification);
   }
+
+  @Test
+  public void should_not_send_changes_if_no_diffs() throws Exception {
+    IssueChangeContext context = IssueChangeContext.createScan(new Date());
+    DefaultIssue issue = new DefaultIssue()
+      .setMessage("the message")
+      .setKey("ABCDE")
+      .setComponentKey("struts:Action")
+      .setProjectKey("struts");
+    DefaultIssueQueryResult queryResult = new DefaultIssueQueryResult(Arrays.<Issue>asList(issue));
+    queryResult.addProjects(Arrays.<Component>asList(new Project("struts")));
+
+    Notification notification = issueNotifications.sendChanges(issue, context, queryResult, null);
+
+    assertThat(notification).isNull();
+    Mockito.verifyZeroInteractions(manager);
+  }
 }