3 * Copyright (C) 2009-2021 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.qualitygate.notification;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nullable;
24 import org.apache.commons.lang.StringUtils;
25 import org.sonar.api.config.EmailSettings;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.api.notifications.Notification;
28 import org.sonar.server.issue.notification.EmailMessage;
29 import org.sonar.server.issue.notification.EmailTemplate;
30 import org.sonar.server.measure.Rating;
32 import java.util.regex.Pattern;
35 * Creates email message for notification "alerts".
39 public class QGChangeEmailTemplate implements EmailTemplate {
41 private static final Pattern alertRatingRegex = Pattern.compile(".*>\\s\\d$");
43 private final EmailSettings configuration;
45 public QGChangeEmailTemplate(EmailSettings configuration) {
46 this.configuration = configuration;
51 public EmailMessage format(Notification notification) {
52 if (!"alerts".equals(notification.getType())) {
56 // Retrieve useful values
57 String projectId = notification.getFieldValue("projectId");
58 String projectKey = notification.getFieldValue("projectKey");
59 String projectName = notification.getFieldValue("projectName");
60 String projectVersion = notification.getFieldValue("projectVersion");
61 String branchName = notification.getFieldValue("branch");
62 String alertName = notification.getFieldValue("alertName");
63 String alertText = notification.getFieldValue("alertText");
64 String alertLevel = notification.getFieldValue("alertLevel");
65 boolean isNewAlert = Boolean.parseBoolean(notification.getFieldValue("isNewAlert"));
66 String fullProjectName = computeFullProjectName(projectName, branchName);
69 String subject = generateSubject(fullProjectName, alertLevel, isNewAlert);
70 String messageBody = generateMessageBody(projectName, projectKey, projectVersion, branchName, alertName, alertText, isNewAlert);
72 // And finally return the email that will be sent
73 return new EmailMessage()
74 .setMessageId("alerts/" + projectId)
76 .setPlainTextMessage(messageBody);
79 private static String computeFullProjectName(String projectName, @Nullable String branchName) {
80 if (branchName == null || branchName.isEmpty()) {
83 return String.format("%s (%s)", projectName, branchName);
86 private static String generateSubject(String fullProjectName, String alertLevel, boolean isNewAlert) {
87 StringBuilder subjectBuilder = new StringBuilder();
88 if (Metric.Level.OK.toString().equals(alertLevel)) {
89 subjectBuilder.append("\"").append(fullProjectName).append("\" is back to green");
90 } else if (isNewAlert) {
91 subjectBuilder.append("New quality gate threshold reached on \"").append(fullProjectName).append("\"");
93 subjectBuilder.append("Quality gate status changed on \"").append(fullProjectName).append("\"");
95 return subjectBuilder.toString();
98 private String generateMessageBody(String projectName, String projectKey,
99 @Nullable String projectVersion, @Nullable String branchName,
100 String alertName, String alertText, boolean isNewAlert) {
101 StringBuilder messageBody = new StringBuilder();
102 messageBody.append("Project: ").append(projectName).append("\n");
103 if (branchName != null) {
104 messageBody.append("Branch: ").append(branchName).append("\n");
106 if (projectVersion != null) {
107 messageBody.append("Version: ").append(projectVersion).append("\n");
109 messageBody.append("Quality gate status: ").append(alertName).append("\n\n");
111 String[] alerts = StringUtils.split(alertText, ",");
112 if (alerts.length > 0) {
114 messageBody.append("New quality gate threshold");
116 messageBody.append("Quality gate threshold");
118 if (alerts.length == 1) {
119 messageBody.append(": ").append(formatRating(alerts[0].trim())).append("\n");
121 messageBody.append("s:\n");
122 for (String alert : alerts) {
123 messageBody.append(" - ").append(formatRating(alert.trim())).append("\n");
128 messageBody.append("\n").append("More details at: ").append(configuration.getServerBaseURL()).append("/dashboard?id=").append(projectKey);
129 if (branchName != null) {
130 messageBody.append("&branch=").append(branchName);
133 return messageBody.toString();
137 * Converts the ratings from digits to a rating letter {@see org.sonar.server.measure.Rating}, based on the
138 * raw text passed to this template.
141 * Reliability rating > 4 will be converted to Reliability rating worse than D
142 * Security rating on New Code > 1 will be converted to Security rating on New Code worse than A
143 * Code Coverage < 50% will not be converted and returned as is.
146 * @return full raw alert with converted ratings
148 private static String formatRating(String alert) {
149 if(!alertRatingRegex.matcher(alert).matches()) {
153 StringBuilder builder = new StringBuilder();
154 builder.append(alert, 0, alert.length() - 3);
155 builder.append("worse than ");
156 String rating = alert.substring(alert.length() - 1);
157 builder.append(Rating.valueOf(Integer.parseInt(rating)).name());
158 return builder.toString();