From: Julien Lancelot Date: Thu, 19 Oct 2017 13:44:55 +0000 (+0200) Subject: SONAR-9917 Improve notification messages on branches X-Git-Tag: 6.7-RC1~140 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7de0fd0dc2ed69a76bcaf795eaf11764d766a9e5;p=sonarqube.git SONAR-9917 Improve notification messages on branches --- 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 48673c575d4..cc49d5cbabe 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 @@ -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()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/IssueChangesEmailTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/IssueChangesEmailTemplate.java index c8e06691815..dbf967d43f3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/IssueChangesEmailTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/IssueChangesEmailTemplate.java @@ -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()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java index 2338d675258..74b87ac6dff 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java @@ -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"); } 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 f6ebece6a68..44e748f476e 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 @@ -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" + 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 e429096b273..daa72e4a0e3 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 @@ -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" + diff --git a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java index bfbe4129e6b..0384a8f5747 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java @@ -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" + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt b/server/sonar-server/src/test/resources/org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt index 27b7a1db23a..c6b6499849b 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt @@ -1,4 +1,5 @@ Action +Branch: feature1 Rule: Avoid Cycles Message: Has 3 cycles