]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10065 persist properties sonar.pullRequest.* from analysis report
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 16 Nov 2017 10:45:04 +0000 (11:45 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 24 Nov 2017 08:23:58 +0000 (09:23 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java

index c85504e3755427c6b9d35ded21be40031c97ca74..0347875b1a3f29e38edab7c080b7f6ea4484cd5e 100644 (file)
@@ -31,11 +31,12 @@ import org.sonar.server.computation.task.step.ComputationStep;
 
 /**
  * Persist analysis properties
- * Only properties starting with "sonar.analysis" will be persisted in database
+ * Only properties starting with "sonar.analysis" or "sonar.pullrequest" will be persisted in database
  */
 public class PersistAnalysisPropertiesStep implements ComputationStep {
 
   private static final String SONAR_ANALYSIS = "sonar.analysis.";
+  private static final String SONAR_PULL_REQUEST = "sonar.pullrequest.";
 
   private final DbClient dbClient;
   private final AnalysisMetadataHolder analysisMetadataHolder;
@@ -55,10 +56,11 @@ public class PersistAnalysisPropertiesStep implements ComputationStep {
     final List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>();
     reportReader.readContextProperties().forEachRemaining(
       contextProperty -> {
-        if (contextProperty.getKey().startsWith(SONAR_ANALYSIS)) {
+        String propertyKey = contextProperty.getKey();
+        if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST)) {
           analysisPropertyDtos.add(new AnalysisPropertyDto()
             .setUuid(uuidFactory.create())
-            .setKey(contextProperty.getKey())
+            .setKey(propertyKey)
             .setValue(contextProperty.getValue())
             .setSnapshotUuid(analysisMetadataHolder.getUuid()));
         }
index 26069ea64a879f763213a898f196bc1d31e53148..5793ede23d05ab95712411b9d79b35243d753bb8 100644 (file)
@@ -20,9 +20,9 @@
 
 package org.sonar.server.computation.task.projectanalysis.step;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Arrays;
 import java.util.List;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
@@ -44,45 +44,73 @@ public class PersistAnalysisPropertiesStepTest {
   private static final String SNAPSHOT_UUID = randomAlphanumeric(40);
   private static final String SMALL_VALUE1 = randomAlphanumeric(50);
   private static final String SMALL_VALUE2 = randomAlphanumeric(50);
+  private static final String SMALL_VALUE3 = randomAlphanumeric(50);
   private static final String BIG_VALUE = randomAlphanumeric(5000);
+  private static final String VALUE_PREFIX_FOR_PR_PROPERTIES = "pr_";
   private static final List<ScannerReport.ContextProperty> PROPERTIES = Arrays.asList(
     newContextProperty("key1", "value1"),
     newContextProperty("key2", "value1"),
     newContextProperty("sonar.analysis", SMALL_VALUE1),
-    newContextProperty("sonar.analysis.branch", SMALL_VALUE1),
+    newContextProperty("sonar.analysis.branch", SMALL_VALUE2),
     newContextProperty("sonar.analysis.empty_string", ""),
     newContextProperty("sonar.analysis.big_value", BIG_VALUE),
-    newContextProperty("sonar.analysis.", SMALL_VALUE2));
+    newContextProperty("sonar.analysis.", SMALL_VALUE3),
+    newContextProperty("sonar.pullrequest", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE1),
+    newContextProperty("sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
+    newContextProperty("sonar.pullrequest.empty_string", ""),
+    newContextProperty("sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
+    newContextProperty("sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
   private BatchReportReader batchReportReader = mock(BatchReportReader.class);
   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
-  private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader, UuidFactoryFast.getInstance());
+  private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader,
+    UuidFactoryFast.getInstance());
 
   @Test
-  public void persist_should_store_only_sonarDotAnalysis_properties() {
+  public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() {
     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
+
     underTest.execute();
-    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(4);
 
+    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8);
     List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
       .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
 
     assertThat(propertyDtos)
       .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
       .containsExactlyInAnyOrder(
-        tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE1),
+        tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2),
         tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
         tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
-        tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE2)
-      );
+        tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
+        tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
+        tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
+        tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
+        tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
+  }
+
+  @Test
+  public void persist_filtering_of_properties_is_case_sensitive() {
+    when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
+      newContextProperty("sonar.ANALYSIS.foo", "foo"),
+      newContextProperty("sonar.anaLysis.bar", "bar"),
+      newContextProperty("sonar.anaLYSIS.doo", "doh"),
+      newContextProperty("sonar.PULLREQUEST.foo", "foo"),
+      newContextProperty("sonar.pullRequest.bar", "bar"),
+      newContextProperty("sonar.pullREQUEST.doo", "doh")).iterator()));
+    when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
+
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0);
   }
 
   @Test
-  public void persist_should_not_store_anything_if_there_is_no_sonarDotAnalysis_properties() {
+  public void persist_should_not_store_anything_if_there_is_no_context_properties() {
     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);