]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9552 add version to QG alert notification
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 28 Sep 2017 10:25:30 +0000 (12:25 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 2 Oct 2017 11:03:35 +0000 (13:03 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java
server/sonar-server/src/main/java/org/sonar/server/notification/email/AlertsEmailTemplate.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java

index 7d3a5f13c35a9189151f4de647d4d993a302a104..0718f982640253bc27c497ba315c9098bdb555ae 100644 (file)
@@ -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())
index d00e7600396cd69fe03cd21d5b341ea005c08cfc..2338d675258b87314b77fcab98f551c520c41209 100644 (file)
@@ -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, ",");
index 11ed77dee8d0021e7268beed9e9680dbc0158671..f0d821eb93b74d75b50fc8b019ef8b9411f15bb3 100644 (file)
@@ -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);
index bf35696f17c16399a3854c2eaed828d174fac5a3..bfbe4129e6b5115001cadfeb423eb66ae9a7649d 100644 (file)
@@ -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)