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 org.sonar.api.config.EmailSettings;
29 import org.sonar.api.i18n.I18n;
30 import org.sonar.api.notifications.Notification;
31 import org.sonar.api.rules.RuleType;
32 import org.sonar.api.utils.DateUtils;
33 import org.sonar.plugins.emailnotifications.api.EmailMessage;
34 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
35 import org.sonar.server.issue.notification.NewIssuesStatistics.Metric;
37 import static com.google.common.base.Preconditions.checkNotNull;
40 * Base class to create emails for new issues
42 public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
44 protected static final char NEW_LINE = '\n';
45 protected static final String TAB = " ";
46 protected static final String DOT = ".";
47 protected static final String COUNT = DOT + "count";
48 protected static final String LABEL = DOT + "label";
50 static final String FIELD_PROJECT_NAME = "projectName";
51 static final String FIELD_PROJECT_KEY = "projectKey";
52 static final String FIELD_PROJECT_DATE = "projectDate";
53 static final String FIELD_PROJECT_UUID = "projectUuid";
54 static final String FIELD_ASSIGNEE = "assignee";
55 static final String FIELD_BRANCH = "branch";
57 protected final EmailSettings settings;
58 protected final I18n i18n;
60 public AbstractNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
61 this.settings = settings;
65 public static String encode(String toEncode) {
67 return URLEncoder.encode(toEncode, "UTF-8");
68 } catch (UnsupportedEncodingException e) {
69 throw new IllegalStateException("Encoding not supported", e);
74 public EmailMessage format(Notification notification) {
75 if (shouldNotFormat(notification)) {
78 String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));
80 StringBuilder message = new StringBuilder();
81 message.append("Project: ").append(projectName).append(NEW_LINE).append(NEW_LINE);
82 appendRuleType(message, notification);
83 appendAssignees(message, notification);
84 appendRules(message, notification);
85 appendTags(message, notification);
86 appendComponents(message, notification);
87 appendFooter(message, notification);
89 return new EmailMessage()
90 .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
91 .setSubject(subject(notification, projectName))
92 .setMessage(message.toString());
95 protected abstract boolean shouldNotFormat(Notification notification);
97 protected String subject(Notification notification, String projectName) {
98 return String.format("%s: %s new issues (new debt: %s)",
100 notification.getFieldValue(Metric.RULE_TYPE + COUNT),
101 notification.getFieldValue(Metric.EFFORT + COUNT));
104 private static boolean doNotHaveValue(Notification notification, Metric metric) {
105 return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
108 private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
109 if (doNotHaveValue(notification, metric)) {
118 while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
119 String name = notification.getFieldValue(metric + DOT + i + LABEL);
121 .append(TAB).append(TAB)
124 .append(notification.getFieldValue(metric + DOT + i + COUNT))
129 message.append(NEW_LINE);
132 protected void appendAssignees(StringBuilder message, Notification notification) {
133 genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
136 protected void appendComponents(StringBuilder message, Notification notification) {
137 genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
140 protected void appendTags(StringBuilder message, Notification notification) {
141 genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
144 protected void appendRules(StringBuilder message, Notification notification) {
145 genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
148 protected void appendRuleType(StringBuilder message, Notification notification) {
149 String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
151 .append(String.format("%s new issue%s (new debt: %s)",
153 Integer.valueOf(count) > 1 ? "s" : "",
154 notification.getFieldValue(Metric.EFFORT + COUNT)))
155 .append(NEW_LINE).append(NEW_LINE)
162 for (Iterator<RuleType> ruleTypeIterator = Arrays.asList(RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL).iterator(); ruleTypeIterator.hasNext();) {
163 RuleType ruleType = ruleTypeIterator.next();
164 String ruleTypeLabel = i18n.message(getLocale(), "rule_type." + ruleType, ruleType.name());
165 message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
166 if (ruleTypeIterator.hasNext()) {
176 protected void appendFooter(StringBuilder message, Notification notification) {
177 String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
178 String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
179 if (projectKey != null && dateString != null) {
180 Date date = DateUtils.parseDateTime(dateString);
181 String url = String.format("%s/project/issues?id=%s",
182 settings.getServerBaseURL(), encode(projectKey));
183 String branchName = notification.getFieldValue("branch");
184 if (branchName != null) {
185 url += "&branch=" + encode(branchName);
187 url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
189 .append("See it in SonarQube: ")
195 private static Locale getLocale() {
196 return Locale.ENGLISH;