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.qualityprofile;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLEncoder;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Comparator;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.platform.Server;
28 import org.sonar.plugins.emailnotifications.api.EmailMessage;
29 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
31 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
32 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.parse;
33 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES;
35 public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate {
37 private final Server server;
39 public BuiltInQualityProfilesNotificationTemplate(Server server) {
44 public EmailMessage format(Notification notification) {
45 if (!BUILT_IN_QUALITY_PROFILES.equals(notification.getType())) {
49 BuiltInQualityProfilesNotification profilesNotification = parse(notification);
51 StringBuilder message = new StringBuilder("Built-in quality profiles have been updated:\n");
52 profilesNotification.getProfiles().stream()
53 .sorted(Comparator.comparing(Profile::getLanguageName).thenComparing(Profile::getProfileName))
56 .append(profile.getProfileName())
58 .append(profile.getLanguageName())
60 .append(server.getPublicRootUrl()).append("/profiles/changelog?language=")
61 .append(profile.getLanguageKey())
63 .append(encode(profile.getProfileName()))
65 int newRules = profile.getNewRules();
67 message.append(" ").append(newRules).append(" new rules\n");
69 int updatedRules = profile.getUpdatedRules();
70 if (updatedRules > 0) {
71 message.append(" ").append(updatedRules).append(" rules have been updated\n");
73 int removedRules = profile.getRemovedRules();
74 if (removedRules > 0) {
75 message.append(" ").append(removedRules).append(" rules removed\n");
79 message.append("This is a good time to review your quality profiles and update them to benefit from the latest evolutions. ");
80 message.append(server.getPublicRootUrl()).append("/profiles");
82 // And finally return the email that will be sent
83 return new EmailMessage()
84 .setMessageId(BUILT_IN_QUALITY_PROFILES)
86 .setMessage(message.toString());
89 public String encode(String text) {
91 return URLEncoder.encode(text, StandardCharsets.UTF_8.name());
92 } catch (UnsupportedEncodingException e) {
93 throw new IllegalStateException(String.format("Cannot encode %s", text), e);