]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9917 Improve notification messages on branches
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Oct 2017 13:44:55 +0000 (15:44 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 20 Oct 2017 11:09:23 +0000 (13:09 +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/IssueChangesEmailTemplate.java
server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.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
server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java
server/sonar-server/src/test/resources/org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt

index 48673c575d488c94481ee4bc5acc84edc0e9fa06..cc49d5cbabed6c2ddda94227fdc14418ff0179ef 100644 (file)
@@ -78,10 +78,12 @@ public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
     }
     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(fullProjectName).append(NEW_LINE);
+    message.append("Project: ").append(projectName).append(NEW_LINE);
+    if (branchName != null) {
+      message.append("Branch: ").append(branchName).append(NEW_LINE);
+    }
     String version = notification.getFieldValue(FIELD_PROJECT_VERSION);
     if (version != null) {
       message.append("Version: ").append(version).append(NEW_LINE);
@@ -96,7 +98,7 @@ public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
 
     return new EmailMessage()
       .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
-      .setSubject(subject(notification, fullProjectName))
+      .setSubject(subject(notification, computeFullProjectName(projectName, branchName)))
       .setMessage(message.toString());
   }
 
index c8e066918156ccdddfbdc4f40b11358955f659b4..dbf967d43f36f68883b1fae0829526a743c29352 100644 (file)
@@ -99,11 +99,15 @@ public class IssueChangesEmailTemplate extends EmailTemplate {
 
   private static void appendHeader(Notification notif, StringBuilder sb) {
     appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
+    String branchName = notif.getFieldValue("branch");
+    if (branchName != null) {
+      appendField(sb, "Branch", null, branchName);
+    }
     appendField(sb, "Rule", null, notif.getFieldValue("ruleName"));
     appendField(sb, "Message", null, notif.getFieldValue("message"));
   }
 
-  private void appendFooter(StringBuilder sb, Notification notification){
+  private void appendFooter(StringBuilder sb, Notification notification) {
     String issueKey = notification.getFieldValue("key");
     try {
       sb.append("More details at: ").append(settings.getServerBaseURL())
index 2338d675258b87314b77fcab98f551c520c41209..74b87ac6dffeec4f8765c8ba7f07a7609a4ab325 100644 (file)
@@ -60,7 +60,7 @@ public class AlertsEmailTemplate extends EmailTemplate {
 
     // Generate text
     String subject = generateSubject(fullProjectName, alertLevel, isNewAlert);
-    String messageBody = generateMessageBody(fullProjectName, projectKey, projectVersion, branchName, alertName, alertText, isNewAlert);
+    String messageBody = generateMessageBody(projectName, projectKey, projectVersion, branchName, alertName, alertText, isNewAlert);
 
     // And finally return the email that will be sent
     return new EmailMessage()
@@ -88,11 +88,14 @@ public class AlertsEmailTemplate extends EmailTemplate {
     return subjectBuilder.toString();
   }
 
-  private String generateMessageBody(String fullProjectName, String projectKey,
+  private String generateMessageBody(String projectName, String projectKey,
     @Nullable String projectVersion, @Nullable String branchName,
     String alertName, String alertText, boolean isNewAlert) {
     StringBuilder messageBody = new StringBuilder();
-    messageBody.append("Project: ").append(fullProjectName).append("\n");
+    messageBody.append("Project: ").append(projectName).append("\n");
+    if (branchName != null) {
+      messageBody.append("Branch: ").append(branchName).append("\n");
+    }
     if (projectVersion != null) {
       messageBody.append("Version: ").append(projectVersion).append("\n");
     }
index f6ebece6a6847237adb0914df9808c494863a216..44e748f476ec6529d4c3f1f39dd7afa7ac8f8bb4 100644 (file)
@@ -168,7 +168,8 @@ public class MyNewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts (feature1)\n" +
+      .startsWith("Project: Struts\n" +
+        "Branch: feature1\n" +
         "Version: 52.0\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
@@ -200,7 +201,8 @@ public class MyNewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts (feature1)\n" +
+      .startsWith("Project: Struts\n" +
+        "Branch: feature1\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
         "\n" +
index e429096b27362edaa98d2a069b1cdb04c5e8760b..daa72e4a0e3c379e524fe9db2f400d7873289f87 100644 (file)
@@ -182,7 +182,8 @@ public class NewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts (feature1)\n" +
+      .startsWith("Project: Struts\n" +
+        "Branch: feature1\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
         "\n" +
@@ -202,7 +203,8 @@ public class NewIssuesEmailTemplateTest {
 
     // TODO datetime to be completed when test is isolated from JVM timezone
     assertThat(message.getMessage())
-      .startsWith("Project: Struts (feature1)\n" +
+      .startsWith("Project: Struts\n" +
+        "Branch: feature1\n" +
         "Version: 42.1.1\n" +
         "\n" +
         "32 new issues (new debt: 1d3h)\n" +
index bfbe4129e6b5115001cadfeb423eb66ae9a7649d..0384a8f574703e204cc2efb2025592ea75a9e525 100644 (file)
@@ -77,7 +77,8 @@ public class AlertsEmailTemplateTest {
     assertThat(message.getMessageId(), is("alerts/45"));
     assertThat(message.getSubject(), is("Quality gate status changed on \"Foo (feature)\""));
     assertThat(message.getMessage(), is("" +
-      "Project: Foo (feature)\n" +
+      "Project: Foo\n" +
+      "Branch: feature\n" +
       "Version: V1-SNAP\n" +
       "Quality gate status: Orange (was Red)\n" +
       "\n" +
@@ -150,7 +151,8 @@ public class AlertsEmailTemplateTest {
     assertThat(message.getMessageId(), is("alerts/45"));
     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\""));
     assertThat(message.getMessage(), is("" +
-      "Project: Foo (feature)\n" +
+      "Project: Foo\n" +
+      "Branch: feature\n" +
       "Version: V1-SNAP\n" +
       "Quality gate status: Orange (was Red)\n" +
       "\n" +
@@ -184,7 +186,8 @@ public class AlertsEmailTemplateTest {
     assertThat(message.getMessageId(), is("alerts/45"));
     assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green"));
     assertThat(message.getMessage(), is("" +
-      "Project: Foo (feature)\n" +
+      "Project: Foo\n" +
+      "Branch: feature\n" +
       "Version: V1-SNAP\n" +
       "Quality gate status: Green (was Red)\n" +
       "\n" +