2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 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.collect.Lists;
23 import org.sonar.api.config.EmailSettings;
24 import org.sonar.api.i18n.I18n;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.api.rule.Severity;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.plugins.emailnotifications.api.EmailMessage;
29 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
31 import java.io.UnsupportedEncodingException;
32 import java.net.URLEncoder;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.Locale;
38 * Creates email message for notification "new-issues".
42 public class NewIssuesEmailTemplate extends EmailTemplate {
44 public static final String FIELD_PROJECT_NAME = "projectName";
45 public static final String FIELD_PROJECT_KEY = "projectKey";
46 public static final String FIELD_PROJECT_DATE = "projectDate";
48 private final EmailSettings settings;
49 private final I18n i18n;
51 public NewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
52 this.settings = settings;
57 public EmailMessage format(Notification notification) {
58 if (!"new-issues".equals(notification.getType())) {
61 String projectName = notification.getFieldValue(FIELD_PROJECT_NAME);
63 StringBuilder sb = new StringBuilder();
64 sb.append("Project: ").append(projectName).append("\n\n");
65 sb.append(notification.getFieldValue("count")).append(" new issues").append("\n\n");
67 for (Iterator<String> severityIterator = Lists.reverse(Severity.ALL).iterator(); severityIterator.hasNext(); ) {
68 String severity = severityIterator.next();
69 String severityLabel = i18n.message(getLocale(), "severity."+ severity, severity);
70 sb.append(severityLabel).append(": ").append(notification.getFieldValue("count-"+ severity));
71 if (severityIterator.hasNext()) {
77 appendFooter(sb, notification);
79 EmailMessage message = new EmailMessage()
80 .setMessageId("new-issues/" + notification.getFieldValue(FIELD_PROJECT_KEY))
81 .setSubject(projectName + ": new issues")
82 .setMessage(sb.toString());
87 private void appendFooter(StringBuilder sb, Notification notification) {
88 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
89 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
90 if (projectKey != null && dateString != null) {
91 Date date = DateUtils.parseDateTime(dateString);
92 String url = String.format("%s/issues/search#componentRoots=%s|createdAt=%s",
93 settings.getServerBaseURL(), encode(projectKey), encode(DateUtils.formatDateTime(date)));
94 sb.append("\n").append("See it in SonarQube: ").append(url).append("\n");
98 public static String encode(String toEncode) {
100 return URLEncoder.encode(toEncode, "UTF-8");
101 } catch (UnsupportedEncodingException e) {
102 throw new IllegalStateException("Encoding not supported", e);
106 private Locale getLocale() {
107 return Locale.ENGLISH;