From b559dca7ccbab7833a8b3c43fd56468453797926 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 14 Sep 2017 11:37:11 +0200 Subject: [PATCH] SONAR-9552 support branch name in subject of new issue notifications --- .../AbstractNewIssuesEmailTemplate.java | 20 ++++++++++++++----- .../MyNewIssuesEmailTemplate.java | 7 +++---- .../MyNewIssuesEmailTemplateTest.java | 12 ++++++++++- .../NewIssuesEmailTemplateTest.java | 12 ++++++++++- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/AbstractNewIssuesEmailTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/AbstractNewIssuesEmailTemplate.java index 039f7ec9f7b..46c7ef956f7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/AbstractNewIssuesEmailTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/AbstractNewIssuesEmailTemplate.java @@ -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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplate.java index c112a77d6a9..5d079155a50 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplate.java @@ -19,14 +19,13 @@ */ 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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplateTest.java index d87c1ce541b..587ecd3c92a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesEmailTemplateTest.java @@ -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" + diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesEmailTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesEmailTemplateTest.java index 41cdb6bf380..80455bb8205 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesEmailTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesEmailTemplateTest.java @@ -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" + -- 2.39.5