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.util.Comparator;
23 import org.sonar.api.notifications.Notification;
24 import org.sonar.plugins.emailnotifications.api.EmailMessage;
25 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
26 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
28 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES;
30 public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate {
33 public EmailMessage format(Notification notification) {
34 if (!BUILT_IN_QUALITY_PROFILES.equals(notification.getType())) {
38 BuiltInQualityProfilesNotification profilesNotification = BuiltInQualityProfilesNotification.parse(notification);
40 StringBuilder message = new StringBuilder("Built-in quality profiles have been updated:\n");
41 profilesNotification.getProfiles().stream()
42 .sorted(Comparator.comparing(Profile::getLanguage).thenComparing(Profile::getProfileName))
45 .append(profile.getProfileName()).append("\" - ")
46 .append(profile.getLanguage()).append("\n");
47 int newRules = profile.getNewRules();
49 message.append(" ").append(newRules).append(" new rules\n");
51 int updatedRules = profile.getUpdatedRules();
52 if (updatedRules > 0) {
53 message.append(" ").append(updatedRules).append(" rules have been updated\n");
55 int removedRules = profile.getRemovedRules();
56 if (removedRules > 0) {
57 message.append(" ").append(removedRules).append(" rules removed\n");
62 "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
64 // And finally return the email that will be sent
65 return new EmailMessage()
66 .setMessageId(BUILT_IN_QUALITY_PROFILES)
68 .setMessage(message.toString());