]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9552 support branch name in subject of new issue notifications
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 14 Sep 2017 09:37:11 +0000 (11:37 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 2 Oct 2017 11:03:35 +0000 (13:03 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/notification/AbstractNewIssuesEmailTemplate.java
server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplate.java
server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplateTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesEmailTemplateTest.java

index 039f7ec9f7bb17e5d2c8d2f4da9de3e7aa881623..46c7ef956f70e765604b0cd8c60ae316eb755908 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Locale;
+import javax.annotation.Nullable;
 import org.sonar.api.config.EmailSettings;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.notifications.Notification;
@@ -76,9 +77,11 @@ public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
       return null;
     }
     String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));
+    String branchName = notification.getFieldValue(FIELD_BRANCH);
+    String fullProjectName = computeFullProjectName(projectName, branchName);
 
     StringBuilder message = new StringBuilder();
-    message.append("Project: ").append(projectName).append(NEW_LINE).append(NEW_LINE);
+    message.append("Project: ").append(fullProjectName).append(NEW_LINE).append(NEW_LINE);
     appendRuleType(message, notification);
     appendAssignees(message, notification);
     appendRules(message, notification);
@@ -88,15 +91,22 @@ public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
 
     return new EmailMessage()
       .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
-      .setSubject(subject(notification, projectName))
+      .setSubject(subject(notification, fullProjectName))
       .setMessage(message.toString());
   }
 
+  private static String computeFullProjectName(String projectName, @Nullable String branchName) {
+    if (branchName == null || branchName.isEmpty()) {
+      return projectName;
+    }
+    return String.format("%s (%s)", projectName, branchName);
+  }
+
   protected abstract boolean shouldNotFormat(Notification notification);
 
-  protected String subject(Notification notification, String projectName) {
+  protected String subject(Notification notification, String fullProjectName) {
     return String.format("%s: %s new issues (new debt: %s)",
-      projectName,
+      fullProjectName,
       notification.getFieldValue(Metric.RULE_TYPE + COUNT),
       notification.getFieldValue(Metric.EFFORT + COUNT));
   }
@@ -180,7 +190,7 @@ public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
       Date date = DateUtils.parseDateTime(dateString);
       String url = String.format("%s/project/issues?id=%s",
         settings.getServerBaseURL(), encode(projectKey));
-      String branchName = notification.getFieldValue("branch");
+      String branchName = notification.getFieldValue(FIELD_BRANCH);
       if (branchName != null) {
         url += "&branch=" + encode(branchName);
       }
index c112a77d6a94af6c05ae66262f81a1d07ec1400c..5d079155a50453987d3ef8ef5867c0ffa9f2f8f0 100644 (file)
  */
 package org.sonar.server.issue.notification;
 
+import java.util.Date;
 import org.sonar.api.config.EmailSettings;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.notifications.Notification;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.server.issue.notification.NewIssuesStatistics.Metric;
 
-import java.util.Date;
-
 /**
  * Creates email message for notification "my-new-issues".
  */
@@ -47,10 +46,10 @@ public class MyNewIssuesEmailTemplate extends AbstractNewIssuesEmailTemplate {
   }
 
   @Override
-  protected String subject(Notification notification, String projectName) {
+  protected String subject(Notification notification, String fullProjectName) {
     return String.format("You have %s new issues on project %s",
       notification.getFieldValue(Metric.RULE_TYPE + COUNT),
-      projectName);
+      fullProjectName);
   }
 
   @Override
index d87c1ce541be666e1144dc9095cd5d7f33b5c965..587ecd3c92a4aac8d61507308864c9386f4e48f1 100644 (file)
@@ -128,6 +128,16 @@ public class MyNewIssuesEmailTemplateTest {
     assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
   }
 
+  @Test
+  public void subject_on_branch() {
+    Notification notification = newNotification(32)
+      .setFieldValue("branch", "feature1");
+
+    EmailMessage message = underTest.format(notification);
+
+    assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts (feature1)");
+  }
+
   @Test
   public void format_email_with_no_assignees_tags_nor_components() throws Exception {
     Notification notification = newNotification(32);
@@ -155,7 +165,7 @@ public class MyNewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts\n" +
+      .startsWith("Project: Struts (feature1)\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
         "\n" +
index 41cdb6bf3807ec8f2afac369fd0fbda44250da26..80455bb8205a1c133ce80e09b3cd9a0f8e1d8977 100644 (file)
@@ -93,6 +93,16 @@ public class NewIssuesEmailTemplateTest {
     assertThat(message.getSubject()).isEqualTo("Struts: 32 new issues (new debt: 1d3h)");
   }
 
+  @Test
+  public void subject_on_branch() throws Exception {
+    Notification notification = newNotification(32)
+      .setFieldValue("branch", "feature1");
+
+    EmailMessage message = template.format(notification);
+
+    assertThat(message.getSubject()).isEqualTo("Struts (feature1): 32 new issues (new debt: 1d3h)");
+  }
+
   @Test
   public void format_email_with_all_fields_filled() throws Exception {
     Notification notification = newNotification(32);
@@ -168,7 +178,7 @@ public class NewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts\n" +
+      .startsWith("Project: Struts (feature1)\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
         "\n" +