@Override public EmailMessage format(Notification notif) { if (!IssueChangeNotification.TYPE.equals(notif.getType())) { return null; } 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"); 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 static void appendChanges(Notification notif, StringBuilder sb) { appendField(sb, "Comment", null, notif.getFieldValue("comment")); appendFieldWithoutHistory(sb, "Assignee", notif.getFieldValue("old.assignee"), notif.getFieldValue("new.assignee")); appendField(sb, "Severity", notif.getFieldValue("old.severity"), notif.getFieldValue("new.severity")); appendField(sb, "Type", notif.getFieldValue("old.type"), notif.getFieldValue("new.type")); 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")); appendField(sb, "Author", notif.getFieldValue("old.author"), notif.getFieldValue("new.author")); appendFieldWithoutHistory(sb, "Action Plan", notif.getFieldValue("old.actionPlan"), notif.getFieldValue("new.actionPlan")); appendField(sb, "Tags", formatTagChange(notif.getFieldValue("old.tags")), formatTagChange(notif.getFieldValue("new.tags"))); }
private static void appendHeader(Notification notif, StringBuilder sb) { appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey"))); String branchName = notif.getFieldValue(FIELD_BRANCH); if (branchName != null) { appendField(sb, "Branch", null, branchName); } String pullRequest = notif.getFieldValue(FIELD_PULL_REQUEST); if (pullRequest != null) { appendField(sb, "Pull request", null, pullRequest); } appendField(sb, "Rule", null, notif.getFieldValue("ruleName")); appendField(sb, "Message", null, notif.getFieldValue("message")); }
@Test public void should_ignore_non_issue_changes() { Notification notification = new Notification("other"); EmailMessage message = underTest.format(notification); assertThat(message).isNull(); }
@Test public void display_component_key_if_no_component_name() throws Exception { Notification notification = generateNotification() .setFieldValue("componentName", null); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/display_component_key_if_no_component_name.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); }
@Override public EmailMessage format(Notification notif) { if (!IssueChangeNotification.TYPE.equals(notif.getType())) { return null; } 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"); 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 static void appendChanges(Notification notif, StringBuilder sb) { appendField(sb, "Comment", null, notif.getFieldValue("comment")); appendFieldWithoutHistory(sb, "Assignee", notif.getFieldValue("old.assignee"), notif.getFieldValue("new.assignee")); appendField(sb, "Severity", notif.getFieldValue("old.severity"), notif.getFieldValue("new.severity")); appendField(sb, "Type", notif.getFieldValue("old.type"), notif.getFieldValue("new.type")); 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")); appendField(sb, "Author", notif.getFieldValue("old.author"), notif.getFieldValue("new.author")); appendFieldWithoutHistory(sb, "Action Plan", notif.getFieldValue("old.actionPlan"), notif.getFieldValue("new.actionPlan")); appendField(sb, "Tags", formatTagChange(notif.getFieldValue("old.tags")), formatTagChange(notif.getFieldValue("new.tags"))); }
private static void appendHeader(Notification notif, StringBuilder sb) { appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey"))); String branchName = notif.getFieldValue(FIELD_BRANCH); if (branchName != null) { appendField(sb, "Branch", null, branchName); } String pullRequest = notif.getFieldValue(FIELD_PULL_REQUEST); if (pullRequest != null) { appendField(sb, "Pull request", null, pullRequest); } appendField(sb, "Rule", null, notif.getFieldValue("ruleName")); appendField(sb, "Message", null, notif.getFieldValue("message")); }
@Test public void test_email_with_issue_on_branch() throws Exception { Notification notification = generateNotification() .setFieldValue("branch", "feature1"); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); }
@Test public void email_should_display_assignee_change() throws Exception { Notification notification = generateNotification() .setFieldValue("old.assignee", "simon") .setFieldValue("new.assignee", "louis"); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_assignee_change.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); assertThat(email.getFrom()).isNull(); }
@Test public void email_should_display_plan_change() throws Exception { Notification notification = generateNotification() .setFieldValue("old.actionPlan", null) .setFieldValue("new.actionPlan", "ABC 1.0"); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_action_plan_change.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); assertThat(email.getFrom()).isNull(); }
@Test public void email_should_display_resolution_change() throws Exception { Notification notification = generateNotification() .setFieldValue("old.resolution", "FALSE-POSITIVE") .setFieldValue("new.resolution", "FIXED"); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_should_display_resolution_change.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); assertThat(email.getFrom()).isNull(); }
@Test public void notification_sender_should_be_the_author_of_change() { UserDto user = db.users().insertUser(); Notification notification = new IssueChangeNotification() .setChangeAuthor(user) .setProject("Struts", "org.apache:struts", null, null); EmailMessage message = underTest.format(notification); assertThat(message.getFrom()).isEqualTo(user.getName()); }
@Test public void notification_contains_user_login_when_user_is_removed() { UserDto user = db.users().insertDisabledUser(); Notification notification = new IssueChangeNotification() .setChangeAuthor(user) .setProject("Struts", "org.apache:struts", null, null); EmailMessage message = underTest.format(notification); assertThat(message.getFrom()).isEqualTo(user.getLogin()); }
@Test public void test_email_with_multiple_changes() throws Exception { Notification notification = generateNotification() .setFieldValue("comment", "How to fix it?") .setFieldValue("old.assignee", "simon") .setFieldValue("new.assignee", "louis") .setFieldValue("new.resolution", "FALSE-POSITIVE") .setFieldValue("new.status", "RESOLVED") .setFieldValue("new.type", "BUG") .setFieldValue("new.tags", "bug performance"); EmailMessage email = underTest.format(notification); assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE"); assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE"); String message = email.getMessage(); String expected = Resources.toString(Resources.getResource( "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_multiple_changes.txt"), StandardCharsets.UTF_8); expected = StringUtils.remove(expected, '\r'); assertThat(message).isEqualTo(expected); assertThat(email.getFrom()).isNull(); }