*/
package org.sonar.plugins.core.issue.notification;
+import com.google.common.base.Objects;
+import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.config.EmailSettings;
import org.sonar.api.notifications.Notification;
*/
public class IssueChangesEmailTemplate extends EmailTemplate {
+ private static final char NEW_LINE = '\n';
+
private final EmailSettings settings;
private final UserFinder userFinder;
if (!"issue-changes".equals(notif.getType())) {
return null;
}
- String issueKey = notif.getFieldValue("key");
- String author = notif.getFieldValue("changeAuthor");
StringBuilder sb = new StringBuilder();
+ appendHeader(notif, sb);
+ sb.append(NEW_LINE);
+ appendChanges(notif, sb);
+ sb.append(NEW_LINE);
+ appendFooter(sb, notif);
+
String projectName = notif.getFieldValue("projectName");
- appendField(sb, "Project", null, projectName);
- appendField(sb, "Component", null, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
- appendField(sb, "Rule", null, notif.getFieldValue("ruleName"));
- appendField(sb, "Message", null, notif.getFieldValue("message"));
- appendField(sb, "Comment", null, notif.getFieldValue("comment"));
- sb.append('\n');
+ String issueKey = notif.getFieldValue("key");
+ String author = notif.getFieldValue("changeAuthor");
+ EmailMessage message = new EmailMessage()
+ .setMessageId("issue-changes/" + issueKey)
+ .setSubject(projectName + ", change on issue #" + issueKey)
+ .setMessage(sb.toString());
+ if (author != null) {
+ message.setFrom(getUserFullName(author));
+ }
+ return message;
+ }
+
+ private void appendChanges(Notification notif, StringBuilder sb) {
+ appendField(sb, "Comment", null, notif.getFieldValue("comment"));
appendField(sb, "Assignee", notif.getFieldValue("old.assignee"), notif.getFieldValue("new.assignee"));
appendField(sb, "Severity", notif.getFieldValue("old.severity"), notif.getFieldValue("new.severity"));
appendField(sb, "Resolution", notif.getFieldValue("old.resolution"), notif.getFieldValue("new.resolution"));
appendField(sb, "Status", notif.getFieldValue("old.status"), notif.getFieldValue("new.status"));
appendField(sb, "Message", notif.getFieldValue("old.message"), notif.getFieldValue("new.message"));
+ }
- appendFooter(sb, notif);
+ private void appendHeader(Notification notif, StringBuilder sb) {
+ String ruleName = notif.getFieldValue("ruleName");
+ String issueMessage = notif.getFieldValue("message");
- EmailMessage message = new EmailMessage()
- .setMessageId("issue-changes/" + issueKey)
- .setSubject("Project "+ projectName +", change on issue #" + issueKey)
- .setMessage(sb.toString());
- if (author != null) {
- message.setFrom(getUserFullName(author));
+ appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
+ appendLine(sb, ruleName);
+ if (!Objects.equal(ruleName, issueMessage)) {
+ appendLine(sb, issueMessage);
+ }
+ }
+
+ private void appendFooter(StringBuilder sb, Notification notification) {
+ String issueKey = notification.getFieldValue("key");
+ sb.append("See it in SonarQube: ").append(settings.getServerBaseURL()).append("/issue/show/").append(issueKey).append(NEW_LINE);
+ }
+
+ private void appendLine(StringBuilder sb, @Nullable String line) {
+ if (!Strings.isNullOrEmpty(line)) {
+ sb.append(line).append(NEW_LINE);
}
- return message;
}
private void appendField(StringBuilder sb, String name, @Nullable String oldValue, @Nullable String newValue) {
if (oldValue != null) {
sb.append(" (was ").append(oldValue).append(")");
}
- sb.append('\n');
+ sb.append(NEW_LINE);
}
}
- private void appendFooter(StringBuilder sb, Notification notification) {
- String issueKey = notification.getFieldValue("key");
- sb.append("\n")
- .append("See it in SonarQube: ").append(settings.getServerBaseURL()).append("/issue/show/").append(issueKey).append('\n');
- }
-
private String getUserFullName(@Nullable String login) {
if (login == null) {
return null;
import org.sonar.api.user.User;
import org.sonar.api.user.UserFinder;
import org.sonar.plugins.emailnotifications.api.EmailMessage;
+import org.sonar.test.TestUtils;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
}
@Test
- public void should_format_change() {
+ public void test_email_with_changes() {
Notification notification = new Notification("issue-changes")
.setFieldValue("projectName", "Struts")
.setFieldValue("projectKey", "org.apache:struts")
+ .setFieldValue("componentName", "org.apache.struts.Action")
.setFieldValue("key", "ABCDE")
.setFieldValue("ruleName", "Avoid Cycles")
.setFieldValue("message", "Has 3 cycles")
.setFieldValue("new.resolution", "FALSE-POSITIVE")
.setFieldValue("new.status", "RESOLVED");
- EmailMessage message = template.format(notification);
- assertThat(message.getMessageId()).isEqualTo("issue-changes/ABCDE");
- assertThat(message.getSubject()).isEqualTo("Project Struts, change on issue #ABCDE");
- assertThat(message.getMessage()).contains("Rule: Avoid Cycles");
- assertThat(message.getMessage()).contains("Message: Has 3 cycles");
- assertThat(message.getMessage()).contains("Comment: How to fix it?");
- assertThat(message.getMessage()).contains("Assignee: louis (was simon)");
- assertThat(message.getMessage()).contains("Resolution: FALSE-POSITIVE");
- assertThat(message.getMessage()).contains("See it in SonarQube: http://nemo.sonarsource.org/issue/show/ABCDE");
- assertThat(message.getFrom()).isNull();
+ EmailMessage email = template.format(notification);
+ assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
+ assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
+
+ String message = email.getMessage();
+ String expectedMessage = TestUtils.getResourceContent("/org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_changes.txt");
+ assertThat(message).isEqualTo(expectedMessage);
+ assertThat(email.getFrom()).isNull();
+ }
+
+ @Test
+ public void test_email_with_issue_message_same_than_rule_name() {
+ Notification notification = new Notification("issue-changes")
+ .setFieldValue("projectName", "Struts")
+ .setFieldValue("projectKey", "org.apache:struts")
+ .setFieldValue("componentName", "org.apache.struts.Action")
+ .setFieldValue("key", "ABCDE")
+ .setFieldValue("new.assignee", "louis")
+
+ // same rule name and issue msg -> display once
+ .setFieldValue("ruleName", "Avoid Cycles")
+ .setFieldValue("message", "Avoid Cycles");
+
+ EmailMessage email = template.format(notification);
+ String message = email.getMessage();
+ String expectedMessage = TestUtils.getResourceContent("/org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_message_same_than_rule_name.txt");
+ assertThat(message).isEqualTo(expectedMessage);
}
@Test