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.sonar.api.config.EmailSettings;
23 import org.sonar.api.notifications.Notification;
24 import org.sonar.api.utils.DateUtils;
25 import org.sonar.plugins.emailnotifications.api.EmailMessage;
26 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
28 import java.io.UnsupportedEncodingException;
29 import java.net.URLEncoder;
30 import java.util.Date;
33 * Creates email message for notification "new-issues".
37 public class NewIssuesEmailTemplate extends EmailTemplate {
39 private final EmailSettings settings;
41 public NewIssuesEmailTemplate(EmailSettings settings) {
42 this.settings = settings;
46 public EmailMessage format(Notification notification) {
47 if (!"new-issues".equals(notification.getType())) {
50 String projectName = notification.getFieldValue("projectName");
51 String violationsCount = notification.getFieldValue("count");
53 StringBuilder sb = new StringBuilder();
54 sb.append("Project: ").append(projectName).append('\n');
55 sb.append(violationsCount).append(" new issues").append('\n');
56 appendFooter(sb, notification);
58 EmailMessage message = new EmailMessage()
59 .setMessageId("new-issues/" + notification.getFieldValue("projectKey"))
60 .setSubject("Project " + projectName + ", new issues")
61 .setMessage(sb.toString());
66 private void appendFooter(StringBuilder sb, Notification notification) {
67 String projectKey = notification.getFieldValue("projectKey");
68 String dateString = notification.getFieldValue("projectDate");
69 if (projectKey != null && dateString != null) {
70 Date date = DateUtils.parseDateTime(dateString);
71 String url = String.format("%s/issues/search?componentRoots=%s&createdAfter=%s",
72 settings.getServerBaseURL(), encode(projectKey), encode(DateUtils.formatDateTime(date)));
73 sb.append("\n").append("See it in SonarQube: ").append(url).append("\n");
77 public static String encode(String toEncode) {
79 return URLEncoder.encode(toEncode, "UTF-8");
80 } catch (UnsupportedEncodingException e) {
81 throw new IllegalStateException(e);