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_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).append(NEW_LINE);
85 appendRuleType(message, notification);
86 appendAssignees(message, notification);
87 appendRules(message, notification);
88 appendTags(message, notification);
89 appendComponents(message, notification);
90 appendFooter(message, notification);
92 return new EmailMessage()
93 .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
94 .setSubject(subject(notification, fullProjectName))
95 .setMessage(message.toString());
98 private static String computeFullProjectName(String projectName, @Nullable String branchName) {
99 if (branchName == null || branchName.isEmpty()) {
102 return String.format("%s (%s)", projectName, branchName);
105 protected abstract boolean shouldNotFormat(Notification notification);
107 protected String subject(Notification notification, String fullProjectName) {
108 return String.format("%s: %s new issues (new debt: %s)",
110 notification.getFieldValue(Metric.RULE_TYPE + COUNT),
111 notification.getFieldValue(Metric.EFFORT + COUNT));
114 private static boolean doNotHaveValue(Notification notification, Metric metric) {
115 return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
118 private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
119 if (doNotHaveValue(notification, metric)) {
128 while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
129 String name = notification.getFieldValue(metric + DOT + i + LABEL);
131 .append(TAB).append(TAB)
134 .append(notification.getFieldValue(metric + DOT + i + COUNT))
139 message.append(NEW_LINE);
142 protected void appendAssignees(StringBuilder message, Notification notification) {
143 genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
146 protected void appendComponents(StringBuilder message, Notification notification) {
147 genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
150 protected void appendTags(StringBuilder message, Notification notification) {
151 genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
154 protected void appendRules(StringBuilder message, Notification notification) {
155 genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
158 protected void appendRuleType(StringBuilder message, Notification notification) {
159 String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
161 .append(String.format("%s new issue%s (new debt: %s)",
163 Integer.valueOf(count) > 1 ? "s" : "",
164 notification.getFieldValue(Metric.EFFORT + COUNT)))
165 .append(NEW_LINE).append(NEW_LINE)
172 for (Iterator<RuleType> ruleTypeIterator = Arrays.asList(RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL).iterator(); ruleTypeIterator.hasNext();) {
173 RuleType ruleType = ruleTypeIterator.next();
174 String ruleTypeLabel = i18n.message(getLocale(), "rule_type." + ruleType, ruleType.name());
175 message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
176 if (ruleTypeIterator.hasNext()) {
186 protected void appendFooter(StringBuilder message, Notification notification) {
187 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
188 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
189 if (projectKey != null && dateString != null) {
190 Date date = DateUtils.parseDateTime(dateString);
191 String url = String.format("%s/project/issues?id=%s",
192 settings.getServerBaseURL(), encode(projectKey));
193 String branchName = notification.getFieldValue(FIELD_BRANCH);
194 if (branchName != null) {
195 url += "&branch=" + encode(branchName);
197 url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
199 .append("See it in SonarQube: ")
205 private static Locale getLocale() {
206 return Locale.ENGLISH;