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 return String.format("%s: %s new issues (new debt: %s)",
116 notification.getFieldValue(Metric.RULE_TYPE + COUNT),
117 notification.getFieldValue(Metric.EFFORT + COUNT));
120 private static boolean doNotHaveValue(Notification notification, Metric metric) {
121 return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
124 private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
125 if (doNotHaveValue(notification, metric)) {
134 while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
135 String name = notification.getFieldValue(metric + DOT + i + LABEL);
137 .append(TAB).append(TAB)
140 .append(notification.getFieldValue(metric + DOT + i + COUNT))
145 message.append(NEW_LINE);
148 protected void appendAssignees(StringBuilder message, Notification notification) {
149 genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
152 protected void appendComponents(StringBuilder message, Notification notification) {
153 genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
156 protected void appendTags(StringBuilder message, Notification notification) {
157 genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
160 protected void appendRules(StringBuilder message, Notification notification) {
161 genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
164 protected void appendRuleType(StringBuilder message, Notification notification) {
165 String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
167 .append(String.format("%s new issue%s (new debt: %s)",
169 Integer.valueOf(count) > 1 ? "s" : "",
170 notification.getFieldValue(Metric.EFFORT + COUNT)))
171 .append(NEW_LINE).append(NEW_LINE)
178 for (Iterator<RuleType> ruleTypeIterator = Arrays.asList(RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL).iterator(); ruleTypeIterator.hasNext();) {
179 RuleType ruleType = ruleTypeIterator.next();
180 String ruleTypeLabel = i18n.message(getLocale(), "rule_type." + ruleType, ruleType.name());
181 message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
182 if (ruleTypeIterator.hasNext()) {
192 protected void appendFooter(StringBuilder message, Notification notification) {
193 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
194 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
195 if (projectKey != null && dateString != null) {
196 Date date = DateUtils.parseDateTime(dateString);
197 String url = String.format("%s/project/issues?id=%s",
198 settings.getServerBaseURL(), encode(projectKey));
199 String branchName = notification.getFieldValue(FIELD_BRANCH);
200 if (branchName != null) {
201 url += "&branch=" + encode(branchName);
203 url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
205 .append("More details at: ")
211 private static Locale getLocale() {
212 return Locale.ENGLISH;