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 org.apache.commons.lang.StringUtils;
23 import org.sonar.api.config.EmailSettings;
24 import org.sonar.api.notifications.Notification;
25 import org.sonar.api.user.User;
26 import org.sonar.api.user.UserFinder;
27 import org.sonar.plugins.emailnotifications.api.EmailMessage;
28 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
30 import javax.annotation.Nullable;
33 * Creates email message for notification "issue-changes".
37 public class IssueChangesEmailTemplate extends EmailTemplate {
39 private final EmailSettings settings;
40 private final UserFinder userFinder;
42 public IssueChangesEmailTemplate(EmailSettings settings, UserFinder userFinder) {
43 this.settings = settings;
44 this.userFinder = userFinder;
48 public EmailMessage format(Notification notif) {
49 if (!"issue-changes".equals(notif.getType())) {
52 String issueKey = notif.getFieldValue("key");
53 String author = notif.getFieldValue("changeAuthor");
55 StringBuilder sb = new StringBuilder();
56 String projectName = notif.getFieldValue("projectName");
57 appendField(sb, "Project", null, projectName);
58 appendField(sb, "Component", null, StringUtils.defaultString(notif.getFieldValue("componentName"), notif.getFieldValue("componentKey")));
59 appendField(sb, "Rule", null, notif.getFieldValue("ruleName"));
60 appendField(sb, "Message", null, notif.getFieldValue("message"));
61 appendField(sb, "Comment", null, notif.getFieldValue("comment"));
64 appendField(sb, "Assignee", notif.getFieldValue("old.assignee"), notif.getFieldValue("new.assignee"));
65 appendField(sb, "Severity", notif.getFieldValue("old.severity"), notif.getFieldValue("new.severity"));
66 appendField(sb, "Resolution", notif.getFieldValue("old.resolution"), notif.getFieldValue("new.resolution"));
67 appendField(sb, "Status", notif.getFieldValue("old.status"), notif.getFieldValue("new.status"));
68 appendField(sb, "Message", notif.getFieldValue("old.message"), notif.getFieldValue("new.message"));
70 appendFooter(sb, notif);
72 EmailMessage message = new EmailMessage()
73 .setMessageId("issue-changes/" + issueKey)
74 .setSubject("Project "+ projectName +", change on issue #" + issueKey)
75 .setMessage(sb.toString());
77 message.setFrom(getUserFullName(author));
82 private void appendField(StringBuilder sb, String name, @Nullable String oldValue, @Nullable String newValue) {
83 if (oldValue != null || newValue != null) {
84 sb.append(name).append(": ");
85 if (newValue != null) {
88 if (oldValue != null) {
89 sb.append(" (was ").append(oldValue).append(")");
95 private void appendFooter(StringBuilder sb, Notification notification) {
96 String issueKey = notification.getFieldValue("key");
98 .append("See it in SonarQube: ").append(settings.getServerBaseURL()).append("/issue/show/").append(issueKey).append('\n');
101 private String getUserFullName(@Nullable String login) {
105 User user = userFinder.findByLogin(login);
107 // most probably user was deleted
110 return StringUtils.defaultIfBlank(user.name(), login);