2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.emailnotifications.templates.alerts;
22 import org.apache.commons.lang.StringUtils;
23 import org.sonar.api.config.EmailSettings;
24 import org.sonar.api.measures.Metric;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.plugins.emailnotifications.api.EmailMessage;
27 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
30 * Creates email message for notification "alerts".
34 public class AlertsEmailTemplate extends EmailTemplate {
36 private EmailSettings configuration;
38 public AlertsEmailTemplate(EmailSettings configuration) {
39 this.configuration = configuration;
43 public EmailMessage format(Notification notification) {
44 if (!"alerts".equals(notification.getType())) {
48 // Retrieve useful values
49 String projectId = notification.getFieldValue("projectId");
50 String projectKey = notification.getFieldValue("projectKey");
51 String projectName = notification.getFieldValue("projectName");
52 String alertName = notification.getFieldValue("alertName");
53 String alertText = notification.getFieldValue("alertText");
54 String alertLevel = notification.getFieldValue("alertLevel");
55 boolean isNewAlert = Boolean.valueOf(notification.getFieldValue("isNewAlert"));
58 String subject = generateSubject(projectName, alertLevel, isNewAlert);
59 String messageBody = generateMessageBody(projectName, projectKey, alertName, alertText, isNewAlert);
61 // And finally return the email that will be sent
62 return new EmailMessage()
63 .setMessageId("alerts/" + projectId)
65 .setMessage(messageBody);
68 private String generateSubject(String projectName, String alertLevel, boolean isNewAlert) {
69 StringBuilder subjectBuilder = new StringBuilder();
70 if (Metric.Level.OK.toString().equals(alertLevel)) {
71 subjectBuilder.append("\"").append(projectName).append("\" is back to green");
72 } else if (isNewAlert) {
73 subjectBuilder.append("New alert on \"").append(projectName).append("\"");
75 subjectBuilder.append("Alert level changed on \"").append(projectName).append("\"");
77 return subjectBuilder.toString();
80 private String generateMessageBody(String projectName, String projectKey, String alertName, String alertText, boolean isNewAlert) {
81 StringBuilder messageBody = new StringBuilder();
82 messageBody.append("Project: ").append(projectName).append("\n");
83 messageBody.append("Alert level: ").append(alertName).append("\n\n");
85 String[] alerts = StringUtils.split(alertText, ",");
86 if (alerts.length > 0) {
88 messageBody.append("New alert");
90 messageBody.append("Alert");
92 if (alerts.length == 1) {
93 messageBody.append(": ").append(alerts[0].trim()).append("\n");
95 messageBody.append("s:\n");
96 for (String alert : alerts) {
97 messageBody.append(" - ").append(alert.trim()).append("\n");
102 messageBody.append("\n").append("See it in SonarQube: ").append(configuration.getServerBaseURL()).append("/dashboard/index/").append(projectKey);
104 return messageBody.toString();