You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractNewIssuesEmailTemplate.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.issue.notification;
  21. import java.io.UnsupportedEncodingException;
  22. import java.net.URLEncoder;
  23. import java.util.Arrays;
  24. import java.util.Date;
  25. import java.util.Iterator;
  26. import java.util.Locale;
  27. import javax.annotation.Nullable;
  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;
  36. import static com.google.common.base.Preconditions.checkNotNull;
  37. /**
  38. * Base class to create emails for new issues
  39. */
  40. public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {
  41. protected static final char NEW_LINE = '\n';
  42. protected static final String TAB = " ";
  43. protected static final String DOT = ".";
  44. protected static final String COUNT = DOT + "count";
  45. protected static final String LABEL = DOT + "label";
  46. static final String FIELD_PROJECT_NAME = "projectName";
  47. static final String FIELD_PROJECT_KEY = "projectKey";
  48. static final String FIELD_PROJECT_DATE = "projectDate";
  49. static final String FIELD_PROJECT_VERSION = "projectVersion";
  50. static final String FIELD_ASSIGNEE = "assignee";
  51. static final String FIELD_BRANCH = "branch";
  52. static final String FIELD_PULL_REQUEST = "pullRequest";
  53. protected final EmailSettings settings;
  54. protected final I18n i18n;
  55. public AbstractNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
  56. this.settings = settings;
  57. this.i18n = i18n;
  58. }
  59. public static String encode(String toEncode) {
  60. try {
  61. return URLEncoder.encode(toEncode, "UTF-8");
  62. } catch (UnsupportedEncodingException e) {
  63. throw new IllegalStateException("Encoding not supported", e);
  64. }
  65. }
  66. @Override
  67. public EmailMessage format(Notification notification) {
  68. if (shouldNotFormat(notification)) {
  69. return null;
  70. }
  71. String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));
  72. String branchName = notification.getFieldValue(FIELD_BRANCH);
  73. String pullRequest = notification.getFieldValue(FIELD_PULL_REQUEST);
  74. StringBuilder message = new StringBuilder();
  75. message.append("Project: ").append(projectName).append(NEW_LINE);
  76. if (branchName != null) {
  77. message.append("Branch: ").append(branchName).append(NEW_LINE);
  78. }
  79. if (pullRequest!= null) {
  80. message.append("Pull request: ").append(pullRequest).append(NEW_LINE);
  81. }
  82. String version = notification.getFieldValue(FIELD_PROJECT_VERSION);
  83. if (version != null) {
  84. message.append("Version: ").append(version).append(NEW_LINE);
  85. }
  86. message.append(NEW_LINE);
  87. appendRuleType(message, notification);
  88. appendAssignees(message, notification);
  89. appendRules(message, notification);
  90. appendTags(message, notification);
  91. appendComponents(message, notification);
  92. appendFooter(message, notification);
  93. return new EmailMessage()
  94. .setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
  95. .setSubject(subject(notification, computeFullProjectName(projectName, branchName)))
  96. .setMessage(message.toString());
  97. }
  98. private static String computeFullProjectName(String projectName, @Nullable String branchName) {
  99. if (branchName == null || branchName.isEmpty()) {
  100. return projectName;
  101. }
  102. return String.format("%s (%s)", projectName, branchName);
  103. }
  104. protected abstract boolean shouldNotFormat(Notification notification);
  105. protected String subject(Notification notification, String fullProjectName) {
  106. int issueCount = Integer.parseInt(notification.getFieldValue(Metric.RULE_TYPE + COUNT));
  107. return String.format("%s: %s new issue%s (new debt: %s)",
  108. fullProjectName,
  109. issueCount,
  110. issueCount > 1 ? "s" : "",
  111. notification.getFieldValue(Metric.EFFORT + COUNT));
  112. }
  113. private static boolean doNotHaveValue(Notification notification, Metric metric) {
  114. return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
  115. }
  116. private static void genericAppendOfMetric(Metric metric, String label, StringBuilder message, Notification notification) {
  117. if (doNotHaveValue(notification, metric)) {
  118. return;
  119. }
  120. message
  121. .append(TAB)
  122. .append(label)
  123. .append(NEW_LINE);
  124. int i = 1;
  125. while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
  126. String name = notification.getFieldValue(metric + DOT + i + LABEL);
  127. message
  128. .append(TAB).append(TAB)
  129. .append(name)
  130. .append(": ")
  131. .append(notification.getFieldValue(metric + DOT + i + COUNT))
  132. .append(NEW_LINE);
  133. i += 1;
  134. }
  135. message.append(NEW_LINE);
  136. }
  137. protected void appendAssignees(StringBuilder message, Notification notification) {
  138. genericAppendOfMetric(Metric.ASSIGNEE, "Assignees", message, notification);
  139. }
  140. protected void appendComponents(StringBuilder message, Notification notification) {
  141. genericAppendOfMetric(Metric.COMPONENT, "Most impacted files", message, notification);
  142. }
  143. protected void appendTags(StringBuilder message, Notification notification) {
  144. genericAppendOfMetric(Metric.TAG, "Tags", message, notification);
  145. }
  146. protected void appendRules(StringBuilder message, Notification notification) {
  147. genericAppendOfMetric(Metric.RULE, "Rules", message, notification);
  148. }
  149. protected void appendRuleType(StringBuilder message, Notification notification) {
  150. String count = notification.getFieldValue(Metric.RULE_TYPE + COUNT);
  151. message
  152. .append(String.format("%s new issue%s (new debt: %s)",
  153. count,
  154. Integer.valueOf(count) > 1 ? "s" : "",
  155. notification.getFieldValue(Metric.EFFORT + COUNT)))
  156. .append(NEW_LINE).append(NEW_LINE)
  157. .append(TAB)
  158. .append("Type")
  159. .append(NEW_LINE)
  160. .append(TAB)
  161. .append(TAB);
  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(), "issue.type." + ruleType, ruleType.name());
  165. message.append(ruleTypeLabel).append(": ").append(notification.getFieldValue(Metric.RULE_TYPE + DOT + ruleType + COUNT));
  166. if (ruleTypeIterator.hasNext()) {
  167. message.append(TAB);
  168. }
  169. }
  170. message
  171. .append(NEW_LINE)
  172. .append(NEW_LINE);
  173. }
  174. protected void appendFooter(StringBuilder message, Notification notification) {
  175. String projectKey = notification.getFieldValue(FIELD_PROJECT_KEY);
  176. String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
  177. if (projectKey != null && dateString != null) {
  178. Date date = DateUtils.parseDateTime(dateString);
  179. String url = String.format("%s/project/issues?id=%s",
  180. settings.getServerBaseURL(), encode(projectKey));
  181. String branchName = notification.getFieldValue(FIELD_BRANCH);
  182. if (branchName != null) {
  183. url += "&branch=" + encode(branchName);
  184. }
  185. String pullRequest = notification.getFieldValue(FIELD_PULL_REQUEST);
  186. if (pullRequest != null) {
  187. url += "&pullRequest=" + encode(pullRequest);
  188. }
  189. url += "&createdAt=" + encode(DateUtils.formatDateTime(date));
  190. message
  191. .append("More details at: ")
  192. .append(url)
  193. .append(NEW_LINE);
  194. }
  195. }
  196. private static Locale getLocale() {
  197. return Locale.ENGLISH;
  198. }
  199. }