String alertText = notification.getFieldValue("alertText");
String alertLevel = notification.getFieldValue("alertLevel");
boolean isNewAlert = Boolean.parseBoolean(notification.getFieldValue("isNewAlert"));
+ String fullProjectName = computeFullProjectName(projectName, branchName);
// Generate text
- String subject = generateSubject(projectName, alertLevel, isNewAlert);
- String messageBody = generateMessageBody(projectName, projectKey, branchName, alertName, alertText, isNewAlert);
+ String subject = generateSubject(fullProjectName, alertLevel, isNewAlert);
+ String messageBody = generateMessageBody(fullProjectName, projectKey, branchName, alertName, alertText, isNewAlert);
// And finally return the email that will be sent
return new EmailMessage()
.setMessage(messageBody);
}
- private static String generateSubject(String projectName, String alertLevel, boolean isNewAlert) {
+ private static String computeFullProjectName(String projectName, @Nullable String branchName) {
+ if (branchName == null || branchName.isEmpty()) {
+ return projectName;
+ }
+ return String.format("%s (%s)", projectName, branchName);
+ }
+
+ private static String generateSubject(String fullProjectName, String alertLevel, boolean isNewAlert) {
StringBuilder subjectBuilder = new StringBuilder();
if (Metric.Level.OK.toString().equals(alertLevel)) {
- subjectBuilder.append("\"").append(projectName).append("\" is back to green");
+ subjectBuilder.append("\"").append(fullProjectName).append("\" is back to green");
} else if (isNewAlert) {
- subjectBuilder.append("New quality gate threshold reached on \"").append(projectName).append("\"");
+ subjectBuilder.append("New quality gate threshold reached on \"").append(fullProjectName).append("\"");
} else {
- subjectBuilder.append("Quality gate status changed on \"").append(projectName).append("\"");
+ subjectBuilder.append("Quality gate status changed on \"").append(fullProjectName).append("\"");
}
return subjectBuilder.toString();
}
- private String generateMessageBody(String projectName, String projectKey, @Nullable String branchName, String alertName, String alertText, boolean isNewAlert) {
+ private String generateMessageBody(String fullProjectName, String projectKey, @Nullable String branchName, String alertName, String alertText, boolean isNewAlert) {
StringBuilder messageBody = new StringBuilder();
- messageBody.append("Project: ").append(projectName).append("\n");
+ messageBody.append("Project: ").append(fullProjectName).append("\n");
messageBody.append("Quality gate status: ").append(alertName).append("\n\n");
String[] alerts = StringUtils.split(alertText, ",");
"See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
}
+ @Test
+ public void shouldFormatAlertWithSeveralMessagesOnBranch() {
+ Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "false")
+ .setFieldValue("branch", "feature");
+
+ EmailMessage message = template.format(notification);
+ assertThat(message.getMessageId(), is("alerts/45"));
+ assertThat(message.getSubject(), is("Quality gate status changed on \"Foo (feature)\""));
+ assertThat(message.getMessage(), is("" +
+ "Project: Foo (feature)\n" +
+ "Quality gate status: Orange (was Red)\n" +
+ "\n" +
+ "Quality gate thresholds:\n" +
+ " - violations > 4\n" +
+ " - coverage < 75%\n" +
+ "\n" +
+ "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
+ }
+
@Test
public void shouldFormatNewAlertWithSeveralMessages() {
Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "true");
EmailMessage message = template.format(notification);
assertThat(message.getMessageId(), is("alerts/45"));
- assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
+ assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\""));
assertThat(message.getMessage(), is("" +
- "Project: Foo\n" +
+ "Project: Foo (feature)\n" +
"Quality gate status: Orange (was Red)\n" +
"\n" +
"New quality gate threshold: violations > 4\n" +
"See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
}
+ @Test
+ public void shouldFormatBackToGreenMessageOnBranch() {
+ Notification notification = createNotification("Green (was Red)", "", "OK", "false")
+ .setFieldValue("branch", "feature");
+
+ EmailMessage message = template.format(notification);
+ assertThat(message.getMessageId(), is("alerts/45"));
+ assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green"));
+ assertThat(message.getMessage(), is("" +
+ "Project: Foo (feature)\n" +
+ "Quality gate status: Green (was Red)\n" +
+ "\n" +
+ "\n" +
+ "See it in SonarQube: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
+ }
+
private Notification createNotification(String alertName, String alertText, String alertLevel, String isNewAlert) {
Notification notification = new Notification("alerts")
.setFieldValue("projectName", "Foo")