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_VERSION = "projectVersion";
55 static final String FIELD_ASSIGNEE = "assignee";
56 static final String FIELD_BRANCH = "branch";
58 protected final EmailSettings settings;
59 protected final I18n i18n;
61 public AbstractNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
62 this.settings = settings;
66 public static String encode(String toEncode) {
68 return URLEncoder.encode(toEncode, "UTF-8");
69 } catch (UnsupportedEncodingException e) {
70 throw new IllegalStateException("Encoding not supported", e);
75 public EmailMessage format(Notification notification) {
76 if (shouldNotFormat(notification)) {
79 String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));
80 String branchName = notification.getFieldValue(FIELD_BRANCH);
81 String fullProjectName = computeFullProjectName(projectName, branchName);
83 StringBuilder message = new StringBuilder();
84 message.append("Project: ").append(fullProjectName).append(NEW_LINE);
85 String version = notification.getFieldValue(FIELD_PROJECT_VERSION);
86 if (version != null) {
87 message.append("Version: ").append(version).append(NEW_LINE);
89 message.append(NEW_LINE);
90 appendRuleType(message, notification);
91 appendAssignees(message, notification);
92 appendRules(message, notification);
93 appendTags(message, notification);
94 appendComponents(message, notification);
95 appendFooter(message, notification);
97 return new EmailMessage()
98 .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
99 .setSubject(subject(notification, fullProjectName))
100 .setMessage(message.toString());
103 private static String computeFullProjectName(String projectName, @Nullable String branchName) {
104 if (branchName == null || branchName.isEmpty()) {
107 return String.format("%s (%s)", projectName, branchName);
110 protected abstract boolean shouldNotFormat(Notification notification);
112 protected String subject(Notification notification, String fullProjectName) {
113 int issueCount = Integer.parseInt(notification.getFieldValue(Metric.RULE_TYPE + COUNT));
114 return String.format("%s: %s new issue%s (new debt: %s)",
117 issueCount > 1 ? "s" : "",
118 notification.getFieldValue(Metric.EFFORT + COUNT));
121 private static boolean doNotHaveValue(Notification notification, Metric metric) {
122 return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
125 private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
126 if (doNotHaveValue(notification, metric)) {
135 while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
136 String name = notification.getFieldValue(metric + DOT + i + LABEL);
138 .append(TAB).append(TAB)
141 .append(notification.getFieldValue(metric + DOT + i + COUNT))
146 message.append(NEW_LINE);
149 protected void appendAssignees(StringBuilder message, Notification notification) {
150 genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
153 protected void appendComponents(StringBuilder message, Notification notification) {
154 genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
157 protected void appendTags(StringBuilder message, Notification notification) {
158 genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
161 protected void appendRules(StringBuilder message, Notification notification) {
162 genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
165 protected void appendRuleType(StringBuilder message, Notification notification) {
166 String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
168 .append(String.format("%s new issue%s (new debt: %s)",
170 Integer.valueOf(count) > 1 ? "s" : "",
171 notification.getFieldValue(Metric.EFFORT + COUNT)))
172 .append(NEW_LINE).append(NEW_LINE)
179 for (Iterator<RuleType> ruleTypeIterator = Arrays.asList(RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL).iterator(); ruleTypeIterator.hasNext();) {
180 RuleType ruleType = ruleTypeIterator.next();
181 String ruleTypeLabel = i18n.message(getLocale(), "issue.type." + ruleType, ruleType.name());
182 message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
183 if (ruleTypeIterator.hasNext()) {
193 protected void appendFooter(StringBuilder message, Notification notification) {
194 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
195 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
196 if (projectKey != null && dateString != null) {
197 Date date = DateUtils.parseDateTime(dateString);
198 String url = String.format("%s/project/issues?id=%s",
199 settings.getServerBaseURL(), encode(projectKey));
200 String branchName = notification.getFieldValue(FIELD_BRANCH);
201 if (branchName != null) {
202 url += "&branch=" + encode(branchName);
204 url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
206 .append("More details at: ")
212 private static Locale getLocale() {
213 return Locale.ENGLISH;