2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.issue.notification;
22 import com.google.common.base.Objects;
23 import com.google.common.base.Strings;
24 import org.apache.commons.lang.StringUtils;
25 import org.sonar.api.config.EmailSettings;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.user.User;
28 import org.sonar.api.user.UserFinder;
29 import org.sonar.plugins.emailnotifications.api.EmailMessage;
30 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
32 import javax.annotation.Nullable;
35 * Creates email message for notification "issue-changes".
39 public class IssueChangesEmailTemplate extends EmailTemplate {
41 private static final char NEW_LINE = '\n';
43 private final EmailSettings settings;
44 private final UserFinder userFinder;
46 public IssueChangesEmailTemplate(EmailSettings settings, UserFinder userFinder) {
47 this.settings = settings;
48 this.userFinder = userFinder;
52 public EmailMessage format(Notification notif) {
53 if (!"issue-changes".equals(notif.getType())) {
57 StringBuilder sb = new StringBuilder();
58 appendHeader(notif, sb);
60 appendChanges(notif, sb);
62 appendFooter(sb, notif);
64 String projectName = notif.getFieldValue("projectName");
65 String issueKey = notif.getFieldValue("key");
66 String author = notif.getFieldValue("changeAuthor");
68 EmailMessage message = new EmailMessage()
69 .setMessageId("issue-changes/" + issueKey)
70 .setSubject(projectName + ", change on issue #" + issueKey)
71 .setMessage(sb.toString());
73 message.setFrom(getUserFullName(author));
78 private void appendChanges(Notification notif, StringBuilder sb) {
79 appendField(sb, "Comment", null, notif.getFieldValue("comment"));
80 appendField(sb, "Assignee", notif.getFieldValue("old.assignee"), notif.getFieldValue("new.assignee"));
81 appendField(sb, "Severity", notif.getFieldValue("old.severity"), notif.getFieldValue("new.severity"));
82 appendField(sb, "Resolution", notif.getFieldValue("old.resolution"), notif.getFieldValue("new.resolution"));
83 appendField(sb, "Status", notif.getFieldValue("old.status"), notif.getFieldValue("new.status"));
84 appendField(sb, "Message", notif.getFieldValue("old.message"), notif.getFieldValue("new.message"));
85 appendField(sb, "Author", notif.getFieldValue("old.author"), notif.getFieldValue("new.author"));
86 appendField(sb, "Action Plan", notif.getFieldValue("old.actionPlanKey"), notif.getFieldValue("new.actionPlanKey"));
89 private void appendHeader(Notification notif, StringBuilder sb) {
90 appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
91 appendField(sb, "Rule", null, notif.getFieldValue("ruleName"));
92 appendField(sb, "Message", null, notif.getFieldValue("message"));
95 private void appendFooter(StringBuilder sb, Notification notification) {
96 String issueKey = notification.getFieldValue("key");
97 sb.append("See it in SonarQube: ").append(settings.getServerBaseURL()).append("/issue/show/").append(issueKey).append(NEW_LINE);
100 private void appendLine(StringBuilder sb, @Nullable String line) {
101 if (!Strings.isNullOrEmpty(line)) {
102 sb.append(line).append(NEW_LINE);
106 private void appendField(StringBuilder sb, String name, @Nullable String oldValue, @Nullable String newValue) {
107 if (oldValue != null || newValue != null) {
108 sb.append(name).append(": ");
109 if (newValue != null) {
112 if (oldValue != null) {
113 sb.append(" (was ").append(oldValue).append(")");
119 private String getUserFullName(@Nullable String login) {
123 User user = userFinder.findByLogin(login);
125 // most probably user was deleted
128 return StringUtils.defaultIfBlank(user.name(), login);