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 java.io.UnsupportedEncodingException;
23 import java.net.URLEncoder;
24 import java.util.Arrays;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.Locale;
28 import javax.annotation.Nullable;
29 import org.sonar.api.config.EmailSettings;
30 import org.sonar.api.i18n.I18n;
31 import org.sonar.api.notifications.Notification;
32 import org.sonar.api.rules.RuleType;
33 import org.sonar.api.utils.DateUtils;
34 import org.sonar.plugins.emailnotifications.api.EmailMessage;
35 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
36 import org.sonar.server.issue.notification.NewIssuesStatistics.Metric;
38 import static com.google.common.base.Preconditions.checkNotNull;
41 * Base class to create emails for new issues
43 public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
45 protected static final char NEW_LINE = '\n';
46 protected static final String TAB = " ";
47 protected static final String DOT = ".";
48 protected static final String COUNT = DOT + "count";
49 protected static final String LABEL = DOT + "label";
51 static final String FIELD_PROJECT_NAME = "projectName";
52 static final String FIELD_PROJECT_KEY = "projectKey";
53 static final String FIELD_PROJECT_DATE = "projectDate";
54 static final String FIELD_PROJECT_UUID = "projectUuid";
55 static final String FIELD_PROJECT_VERSION = "projectVersion";
56 static final String FIELD_ASSIGNEE = "assignee";
57 static final String FIELD_BRANCH = "branch";
59 protected final EmailSettings settings;
60 protected final I18n i18n;
62 public AbstractNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
63 this.settings = settings;
67 public static String encode(String toEncode) {
69 return URLEncoder.encode(toEncode, "UTF-8");
70 } catch (UnsupportedEncodingException e) {
71 throw new IllegalStateException("Encoding not supported", e);
76 public EmailMessage format(Notification notification) {
77 if (shouldNotFormat(notification)) {
80 String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));
81 String branchName = notification.getFieldValue(FIELD_BRANCH);
82 String fullProjectName = computeFullProjectName(projectName, branchName);
84 StringBuilder message = new StringBuilder();
85 message.append("Project: ").append(fullProjectName).append(NEW_LINE);
86 String version = notification.getFieldValue(FIELD_PROJECT_VERSION);
87 if (version != null) {
88 message.append("Version: ").append(version).append(NEW_LINE);
90 message.append(NEW_LINE);
91 appendRuleType(message, notification);
92 appendAssignees(message, notification);
93 appendRules(message, notification);
94 appendTags(message, notification);
95 appendComponents(message, notification);
96 appendFooter(message, notification);
98 return new EmailMessage()
99 .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
100 .setSubject(subject(notification, fullProjectName))
101 .setMessage(message.toString());
104 private static String computeFullProjectName(String projectName, @Nullable String branchName) {
105 if (branchName == null || branchName.isEmpty()) {
108 return String.format("%s (%s)", projectName, branchName);
111 protected abstract boolean shouldNotFormat(Notification notification);
113 protected String subject(Notification notification, String fullProjectName) {
114 int issueCount = Integer.parseInt(notification.getFieldValue(Metric.RULE_TYPE + COUNT));
115 return String.format("%s: %s new issue%s (new debt: %s)",
118 issueCount > 1 ? "s" : "",
119 notification.getFieldValue(Metric.EFFORT + COUNT));
122 private static boolean doNotHaveValue(Notification notification, Metric metric) {
123 return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
126 private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
127 if (doNotHaveValue(notification, metric)) {
136 while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
137 String name = notification.getFieldValue(metric + DOT + i + LABEL);
139 .append(TAB).append(TAB)
142 .append(notification.getFieldValue(metric + DOT + i + COUNT))
147 message.append(NEW_LINE);
150 protected void appendAssignees(StringBuilder message, Notification notification) {
151 genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
154 protected void appendComponents(StringBuilder message, Notification notification) {
155 genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
158 protected void appendTags(StringBuilder message, Notification notification) {
159 genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
162 protected void appendRules(StringBuilder message, Notification notification) {
163 genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
166 protected void appendRuleType(StringBuilder message, Notification notification) {
167 String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
169 .append(String.format("%s new issue%s (new debt: %s)",
171 Integer.valueOf(count) > 1 ? "s" : "",
172 notification.getFieldValue(Metric.EFFORT + COUNT)))
173 .append(NEW_LINE).append(NEW_LINE)
180 for (Iterator<RuleType> ruleTypeIterator = Arrays.asList(RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL).iterator(); ruleTypeIterator.hasNext();) {
181 RuleType ruleType = ruleTypeIterator.next();
182 String ruleTypeLabel = i18n.message(getLocale(), "issue.type." + ruleType, ruleType.name());
183 message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
184 if (ruleTypeIterator.hasNext()) {
194 protected void appendFooter(StringBuilder message, Notification notification) {
195 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
196 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
197 if (projectKey != null && dateString != null) {
198 Date date = DateUtils.parseDateTime(dateString);
199 String url = String.format("%s/project/issues?id=%s",
200 settings.getServerBaseURL(), encode(projectKey));
201 String branchName = notification.getFieldValue(FIELD_BRANCH);
202 if (branchName != null) {
203 url += "&branch=" + encode(branchName);
205 url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
207 .append("More details at: ")
213 private static Locale getLocale() {
214 return Locale.ENGLISH;