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.util.Comparator;
25 import java.util.Date;
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 java.nio.charset.StandardCharsets.UTF_8;
32 import static org.sonar.api.utils.DateUtils.formatDate;
33 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
34 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.parse;
35 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES;
37 public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate {
39 private final Server server;
41 public BuiltInQualityProfilesNotificationTemplate(Server server) {
46 public EmailMessage format(Notification notification) {
47 if (!BUILT_IN_QUALITY_PROFILES.equals(notification.getType())) {
51 BuiltInQualityProfilesNotification profilesNotification = parse(notification);
53 StringBuilder message = new StringBuilder("Built-in quality profiles have been updated:\n");
54 profilesNotification.getProfiles().stream()
55 .sorted(Comparator.comparing(Profile::getLanguageName).thenComparing(Profile::getProfileName))
58 .append(profile.getProfileName())
60 .append(profile.getLanguageName())
62 .append(server.getPublicRootUrl()).append("/profiles/changelog?language=")
63 .append(profile.getLanguageKey())
65 .append(encode(profile.getProfileName()))
67 .append(formatDate(new Date(profile.getStartDate())))
69 .append(formatDate(new Date(profile.getEndDate())))
71 int newRules = profile.getNewRules();
73 message.append(" ").append(newRules).append(" new rules\n");
75 int updatedRules = profile.getUpdatedRules();
76 if (updatedRules > 0) {
77 message.append(" ").append(updatedRules).append(" rules have been updated\n");
79 int removedRules = profile.getRemovedRules();
80 if (removedRules > 0) {
81 message.append(" ").append(removedRules).append(" rules removed\n");
85 message.append("This is a good time to review your quality profiles and update them to benefit from the latest evolutions: ");
86 message.append(server.getPublicRootUrl()).append("/profiles");
88 // And finally return the email that will be sent
89 return new EmailMessage()
90 .setMessageId(BUILT_IN_QUALITY_PROFILES)
91 .setSubject("Built-in quality profiles have been updated")
92 .setMessage(message.toString());
95 public String encode(String text) {
97 return URLEncoder.encode(text, UTF_8.name());
98 } catch (UnsupportedEncodingException e) {
99 throw new IllegalStateException(String.format("Cannot encode %s", text), e);