From 73e7535a17ea6a7673fd250a4d3e204809c7a192 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 28 Sep 2017 12:25:30 +0200 Subject: [PATCH] SONAR-9552 add version to QG alert notification --- .../step/QualityGateEventsStep.java | 1 + .../email/AlertsEmailTemplate.java | 10 ++++++-- .../step/QualityGateEventsStepTest.java | 6 ++++- .../email/AlertsEmailTemplateTest.java | 25 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java index 7d3a5f13c35..0718f982640 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java @@ -128,6 +128,7 @@ public class QualityGateEventsStep implements ComputationStep { .setFieldValue("projectName", project.getName()) .setFieldValue("projectKey", project.getPublicKey()) .setFieldValue("projectUuid", project.getUuid()) + .setFieldValue("projectVersion", project.getReportAttributes().getVersion()) .setFieldValue("alertName", label) .setFieldValue("alertText", rawStatus.getText()) .setFieldValue("alertLevel", rawStatus.getStatus().toString()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java index d00e7600396..2338d675258 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java @@ -50,6 +50,7 @@ public class AlertsEmailTemplate extends EmailTemplate { String projectId = notification.getFieldValue("projectId"); String projectKey = notification.getFieldValue("projectKey"); String projectName = notification.getFieldValue("projectName"); + String projectVersion = notification.getFieldValue("projectVersion"); String branchName = notification.getFieldValue("branch"); String alertName = notification.getFieldValue("alertName"); String alertText = notification.getFieldValue("alertText"); @@ -59,7 +60,7 @@ public class AlertsEmailTemplate extends EmailTemplate { // Generate text String subject = generateSubject(fullProjectName, alertLevel, isNewAlert); - String messageBody = generateMessageBody(fullProjectName, projectKey, branchName, alertName, alertText, isNewAlert); + String messageBody = generateMessageBody(fullProjectName, projectKey, projectVersion, branchName, alertName, alertText, isNewAlert); // And finally return the email that will be sent return new EmailMessage() @@ -87,9 +88,14 @@ public class AlertsEmailTemplate extends EmailTemplate { return subjectBuilder.toString(); } - private String generateMessageBody(String fullProjectName, String projectKey, @Nullable String branchName, String alertName, String alertText, boolean isNewAlert) { + private String generateMessageBody(String fullProjectName, String projectKey, + @Nullable String projectVersion, @Nullable String branchName, + String alertName, String alertText, boolean isNewAlert) { StringBuilder messageBody = new StringBuilder(); messageBody.append("Project: ").append(fullProjectName).append("\n"); + if (projectVersion != null) { + messageBody.append("Version: ").append(projectVersion).append("\n"); + } messageBody.append("Quality gate status: ").append(alertName).append("\n\n"); String[] alerts = StringUtils.split(alertText, ","); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java index 11ed77dee8d..f0d821eb93b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java @@ -54,7 +54,7 @@ import static org.sonar.server.computation.task.projectanalysis.measure.Measure. public class QualityGateEventsStepTest { private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("uuid 1").setKey("key 1") - .addChildren(ReportComponent.builder(Component.Type.MODULE, 2).build()) + .addChildren(ReportComponent.builder(Component.Type.MODULE, 2).setVersion("V1.9").build()) .build(); private static final String INVALID_ALERT_STATUS = "trololo"; private static final String ALERT_TEXT = "alert text"; @@ -190,6 +190,7 @@ public class QualityGateEventsStepTest { assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey()); assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid()); assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); + assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion()); assertThat(notification.getFieldValue("branch")).isNull(); assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name()); assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel); @@ -242,6 +243,7 @@ public class QualityGateEventsStepTest { assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey()); assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid()); assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); + assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion()); assertThat(notification.getFieldValue("branch")).isNull(); assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name()); assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel); @@ -271,6 +273,7 @@ public class QualityGateEventsStepTest { assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey()); assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid()); assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); + assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion()); assertThat(notification.getFieldValue("branch")).isEqualTo(branchName); reset(measureRepository, eventRepository, notificationService); @@ -292,6 +295,7 @@ public class QualityGateEventsStepTest { assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey()); assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid()); assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); + assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion()); assertThat(notification.getFieldValue("branch")).isEqualTo(null); reset(measureRepository, eventRepository, notificationService); diff --git a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java index bf35696f17c..bfbe4129e6b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java @@ -58,6 +58,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("Quality gate status changed on \"Foo\"")); assertThat(message.getMessage(), is("" + "Project: Foo\n" + + "Version: V1-SNAP\n" + "Quality gate status: Orange (was Red)\n" + "\n" + "Quality gate thresholds:\n" + @@ -77,6 +78,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("Quality gate status changed on \"Foo (feature)\"")); assertThat(message.getMessage(), is("" + "Project: Foo (feature)\n" + + "Version: V1-SNAP\n" + "Quality gate status: Orange (was Red)\n" + "\n" + "Quality gate thresholds:\n" + @@ -95,6 +97,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\"")); assertThat(message.getMessage(), is("" + "Project: Foo\n" + + "Version: V1-SNAP\n" + "Quality gate status: Orange (was Red)\n" + "\n" + "New quality gate thresholds:\n" + @@ -108,6 +111,24 @@ public class AlertsEmailTemplateTest { public void shouldFormatNewAlertWithOneMessage() { Notification notification = createNotification("Orange (was Red)", "violations > 4", "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.getMessage(), is("" + + "Project: Foo\n" + + "Version: V1-SNAP\n" + + "Quality gate status: Orange (was Red)\n" + + "\n" + + "New quality gate threshold: violations > 4\n" + + "\n" + + "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo")); + } + + @Test + public void shouldFormatNewAlertWithoutVersion() { + Notification notification = createNotification("Orange (was Red)", "violations > 4", "WARN", "true") + .setFieldValue("projectVersion", null); + EmailMessage message = template.format(notification); assertThat(message.getMessageId(), is("alerts/45")); assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\"")); @@ -130,6 +151,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\"")); assertThat(message.getMessage(), is("" + "Project: Foo (feature)\n" + + "Version: V1-SNAP\n" + "Quality gate status: Orange (was Red)\n" + "\n" + "New quality gate threshold: violations > 4\n" + @@ -146,6 +168,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("\"Foo\" is back to green")); assertThat(message.getMessage(), is("" + "Project: Foo\n" + + "Version: V1-SNAP\n" + "Quality gate status: Green (was Red)\n" + "\n" + "\n" + @@ -162,6 +185,7 @@ public class AlertsEmailTemplateTest { assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green")); assertThat(message.getMessage(), is("" + "Project: Foo (feature)\n" + + "Version: V1-SNAP\n" + "Quality gate status: Green (was Red)\n" + "\n" + "\n" + @@ -173,6 +197,7 @@ public class AlertsEmailTemplateTest { .setFieldValue("projectName", "Foo") .setFieldValue("projectKey", "org.sonar.foo:foo") .setFieldValue("projectId", "45") + .setFieldValue("projectVersion", "V1-SNAP") .setFieldValue("alertName", alertName) .setFieldValue("alertText", alertText) .setFieldValue("alertLevel", alertLevel) -- 2.39.5