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.

BuiltInQPChangeNotificationTemplate.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.qualityprofile;
  21. import java.io.UnsupportedEncodingException;
  22. import java.net.URLEncoder;
  23. import java.util.Comparator;
  24. import java.util.Date;
  25. import javax.annotation.CheckForNull;
  26. import org.sonar.api.notifications.Notification;
  27. import org.sonar.api.platform.Server;
  28. import org.sonar.server.issue.notification.EmailMessage;
  29. import org.sonar.server.issue.notification.EmailTemplate;
  30. import static java.nio.charset.StandardCharsets.UTF_8;
  31. import static org.sonar.api.utils.DateUtils.formatDate;
  32. import static org.sonar.server.qualityprofile.BuiltInQPChangeNotificationBuilder.Profile;
  33. import static org.sonar.server.qualityprofile.BuiltInQPChangeNotificationBuilder.parse;
  34. public class BuiltInQPChangeNotificationTemplate implements EmailTemplate {
  35. private final Server server;
  36. public BuiltInQPChangeNotificationTemplate(Server server) {
  37. this.server = server;
  38. }
  39. @Override
  40. @CheckForNull
  41. public EmailMessage format(Notification notification) {
  42. if (!BuiltInQPChangeNotification.TYPE.equals(notification.getType())) {
  43. return null;
  44. }
  45. BuiltInQPChangeNotificationBuilder profilesNotification = parse(notification);
  46. StringBuilder message = new StringBuilder("The following built-in profiles have been updated:\n\n");
  47. profilesNotification.getProfiles().stream()
  48. .sorted(Comparator.comparing(Profile::getLanguageName).thenComparing(Profile::getProfileName))
  49. .forEach(profile -> {
  50. message.append("\"")
  51. .append(profile.getProfileName())
  52. .append("\" - ")
  53. .append(profile.getLanguageName())
  54. .append(": ")
  55. .append(server.getPublicRootUrl()).append("/profiles/changelog?language=")
  56. .append(profile.getLanguageKey())
  57. .append("&name=")
  58. .append(encode(profile.getProfileName()))
  59. .append("&since=")
  60. .append(formatDate(new Date(profile.getStartDate())))
  61. .append("&to=")
  62. .append(formatDate(new Date(profile.getEndDate())))
  63. .append("\n");
  64. int newRules = profile.getNewRules();
  65. if (newRules > 0) {
  66. message.append(" ").append(newRules).append(" new rule")
  67. .append(plural(newRules))
  68. .append('\n');
  69. }
  70. int updatedRules = profile.getUpdatedRules();
  71. if (updatedRules > 0) {
  72. message.append(" ").append(updatedRules).append(" rule")
  73. .append(updatedRules > 1 ? "s have been updated" : " has been updated")
  74. .append("\n");
  75. }
  76. int removedRules = profile.getRemovedRules();
  77. if (removedRules > 0) {
  78. message.append(" ").append(removedRules).append(" rule")
  79. .append(plural(removedRules))
  80. .append(" removed\n");
  81. }
  82. message.append("\n");
  83. });
  84. message.append("This is a good time to review your quality profiles and update them to benefit from the latest evolutions: ");
  85. message.append(server.getPublicRootUrl()).append("/profiles");
  86. // And finally return the email that will be sent
  87. return new EmailMessage()
  88. .setMessageId(BuiltInQPChangeNotification.TYPE)
  89. .setSubject("Built-in quality profiles have been updated")
  90. .setPlainTextMessage(message.toString());
  91. }
  92. private static String plural(int count) {
  93. return count > 1 ? "s" : "";
  94. }
  95. public String encode(String text) {
  96. try {
  97. return URLEncoder.encode(text, UTF_8.name());
  98. } catch (UnsupportedEncodingException e) {
  99. throw new IllegalStateException(String.format("Cannot encode %s", text), e);
  100. }
  101. }
  102. }