]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9552 show branch name in QP alert notifications
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 14 Sep 2017 09:48:31 +0000 (11:48 +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/notification/email/AlertsEmailTemplate.java
server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java

index 58f1b218afebf377024ab125c0e616d912d5e7db..214fd5b898514eea4938edbb2f35d8aa31563f68 100644 (file)
@@ -55,10 +55,11 @@ public class AlertsEmailTemplate extends EmailTemplate {
     String alertText = notification.getFieldValue("alertText");
     String alertLevel = notification.getFieldValue("alertLevel");
     boolean isNewAlert = Boolean.parseBoolean(notification.getFieldValue("isNewAlert"));
+    String fullProjectName = computeFullProjectName(projectName, branchName);
 
     // Generate text
-    String subject = generateSubject(projectName, alertLevel, isNewAlert);
-    String messageBody = generateMessageBody(projectName, projectKey, branchName, alertName, alertText, isNewAlert);
+    String subject = generateSubject(fullProjectName, alertLevel, isNewAlert);
+    String messageBody = generateMessageBody(fullProjectName, projectKey, branchName, alertName, alertText, isNewAlert);
 
     // And finally return the email that will be sent
     return new EmailMessage()
@@ -67,21 +68,28 @@ public class AlertsEmailTemplate extends EmailTemplate {
       .setMessage(messageBody);
   }
 
-  private static String generateSubject(String projectName, String alertLevel, boolean isNewAlert) {
+  private static String computeFullProjectName(String projectName, @Nullable String branchName) {
+    if (branchName == null || branchName.isEmpty()) {
+      return projectName;
+    }
+    return String.format("%s (%s)", projectName, branchName);
+  }
+
+  private static String generateSubject(String fullProjectName, String alertLevel, boolean isNewAlert) {
     StringBuilder subjectBuilder = new StringBuilder();
     if (Metric.Level.OK.toString().equals(alertLevel)) {
-      subjectBuilder.append("\"").append(projectName).append("\" is back to green");
+      subjectBuilder.append("\"").append(fullProjectName).append("\" is back to green");
     } else if (isNewAlert) {
-      subjectBuilder.append("New quality gate threshold reached on \"").append(projectName).append("\"");
+      subjectBuilder.append("New quality gate threshold reached on \"").append(fullProjectName).append("\"");
     } else {
-      subjectBuilder.append("Quality gate status changed on \"").append(projectName).append("\"");
+      subjectBuilder.append("Quality gate status changed on \"").append(fullProjectName).append("\"");
     }
     return subjectBuilder.toString();
   }
 
-  private String generateMessageBody(String projectName, String projectKey, @Nullable String branchName, String alertName, String alertText, boolean isNewAlert) {
+  private String generateMessageBody(String fullProjectName, String projectKey, @Nullable String branchName, String alertName, String alertText, boolean isNewAlert) {
     StringBuilder messageBody = new StringBuilder();
-    messageBody.append("Project: ").append(projectName).append("\n");
+    messageBody.append("Project: ").append(fullProjectName).append("\n");
     messageBody.append("Quality gate status: ").append(alertName).append("\n\n");
 
     String[] alerts = StringUtils.split(alertText, ",");
index 550130d2f8918e5bb8bfdbaf8943134aa0907129..af8a6e5df94388c93fea1fe7d1be9b99e140768e 100644 (file)
@@ -67,6 +67,25 @@ public class AlertsEmailTemplateTest {
       "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
   }
 
+  @Test
+  public void shouldFormatAlertWithSeveralMessagesOnBranch() {
+    Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "false")
+        .setFieldValue("branch", "feature");
+
+    EmailMessage message = template.format(notification);
+    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" +
+      "Quality gate status: Orange (was Red)\n" +
+      "\n" +
+      "Quality gate thresholds:\n" +
+      "  - violations > 4\n" +
+      "  - coverage < 75%\n" +
+      "\n" +
+      "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
+  }
+
   @Test
   public void shouldFormatNewAlertWithSeveralMessages() {
     Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "true");
@@ -108,9 +127,9 @@ public class AlertsEmailTemplateTest {
 
     EmailMessage message = template.format(notification);
     assertThat(message.getMessageId(), is("alerts/45"));
-    assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
+    assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\""));
     assertThat(message.getMessage(), is("" +
-      "Project: Foo\n" +
+      "Project: Foo (feature)\n" +
       "Quality gate status: Orange (was Red)\n" +
       "\n" +
       "New quality gate threshold: violations > 4\n" +
@@ -133,6 +152,22 @@ public class AlertsEmailTemplateTest {
       "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
   }
 
+  @Test
+  public void shouldFormatBackToGreenMessageOnBranch() {
+    Notification notification = createNotification("Green (was Red)", "", "OK", "false")
+        .setFieldValue("branch", "feature");
+
+    EmailMessage message = template.format(notification);
+    assertThat(message.getMessageId(), is("alerts/45"));
+    assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green"));
+    assertThat(message.getMessage(), is("" +
+      "Project: Foo (feature)\n" +
+      "Quality gate status: Green (was Red)\n" +
+      "\n" +
+      "\n" +
+      "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
+  }
+
   private Notification createNotification(String alertName, String alertText, String alertLevel, String isNewAlert) {
     Notification notification = new Notification("alerts")
         .setFieldValue("projectName", "Foo")