3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.server.issue.notification;
22 import com.google.common.base.Strings;
23 import java.io.UnsupportedEncodingException;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.api.config.EmailSettings;
28 import org.sonar.api.notifications.Notification;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
35 import static java.net.URLEncoder.encode;
38 * Creates email message for notification "issue-changes".
40 public class IssueChangesEmailTemplate extends EmailTemplate {
42 private static final char NEW_LINE = '\n';
43 private final DbClient dbClient;
44 private final EmailSettings settings;
46 public IssueChangesEmailTemplate(DbClient dbClient, EmailSettings settings) {
47 this.dbClient = dbClient;
48 this.settings = settings;
52 public EmailMessage format(Notification notif) {
53 if (!IssueChangeNotification.TYPE.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 static void appendChanges(Notification notif, StringBuilder sb) {
79 appendField(sb, "Comment", null, notif.getFieldValue("comment"));
80 appendFieldWithoutHistory(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, "Type", notif.getFieldValue("old.type"), notif.getFieldValue("new.type"));
83 appendField(sb, "Resolution", notif.getFieldValue("old.resolution"), notif.getFieldValue("new.resolution"));
84 appendField(sb, "Status", notif.getFieldValue("old.status"), notif.getFieldValue("new.status"));
85 appendField(sb, "Message", notif.getFieldValue("old.message"), notif.getFieldValue("new.message"));
86 appendField(sb, "Author", notif.getFieldValue("old.author"), notif.getFieldValue("new.author"));
87 appendFieldWithoutHistory(sb, "Action Plan", notif.getFieldValue("old.actionPlan"), notif.getFieldValue("new.actionPlan"));
88 appendField(sb, "Tags", formatTagChange(notif.getFieldValue("old.tags")), formatTagChange(notif.getFieldValue("new.tags")));
92 private static String formatTagChange(@Nullable String tags) {
96 return "[" + tags + "]";
100 private static void appendHeader(Notification notif, StringBuilder sb) {
101 appendLine(sb, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
102 appendField(sb, "Rule", null, notif.getFieldValue("ruleName"));
103 appendField(sb, "Message", null, notif.getFieldValue("message"));
106 private void appendFooter(StringBuilder sb, Notification notification){
107 String issueKey = notification.getFieldValue("key");
109 sb.append("See it in SonarQube: ").append(settings.getServerBaseURL())
110 .append("/project/issues?id=").append(encode(notification.getFieldValue("projectKey"), "UTF-8"))
111 .append("&issues=").append(issueKey)
112 .append("&open=").append(issueKey)
114 } catch (UnsupportedEncodingException e) {
115 throw new IllegalStateException("Encoding not supported", e);
119 private static void appendLine(StringBuilder sb, @Nullable String line) {
120 if (!Strings.isNullOrEmpty(line)) {
121 sb.append(line).append(NEW_LINE);
125 private static void appendField(StringBuilder sb, String name, @Nullable String oldValue, @Nullable String newValue) {
126 if (oldValue != null || newValue != null) {
127 sb.append(name).append(": ");
128 if (newValue != null) {
131 if (oldValue != null) {
132 sb.append(" (was ").append(oldValue).append(")");
138 private static void appendFieldWithoutHistory(StringBuilder sb, String name, @Nullable String oldValue, @Nullable String newValue) {
139 if (oldValue != null || newValue != null) {
141 if (newValue != null) {
142 sb.append(" changed to ");
145 sb.append(" removed");
151 private String getUserFullName(@Nullable String login) {
155 try (DbSession dbSession = dbClient.openSession(false)) {
156 UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
157 if (userDto == null || !userDto.isActive()) {
158 // most probably user was deleted
161 return StringUtils.defaultIfBlank(userDto.getName(), login);