]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11437 Store the SHA1 read from scanner report (#926)
authorBenoit <43733395+benoit-sns@users.noreply.github.com>
Mon, 12 Nov 2018 11:56:56 +0000 (11:56 +0000)
committerSonarTech <sonartech@sonarsource.com>
Tue, 8 Jan 2019 19:21:04 +0000 (20:21 +0100)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolder.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java

index a8652453d8cab8920ab8dfae2ac28215c857f7d5..7c787c74207578dfb74076ff56139d67116a38b8 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.ce.task.projectanalysis.analysis;
 
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import org.sonar.server.project.Project;
 import org.sonar.server.qualityprofile.QualityProfile;
@@ -136,4 +137,9 @@ public interface AnalysisMetadataHolder {
    * Plugins used during the analysis on scanner side
    */
   Map<String, ScannerPlugin> getScannerPluginsByKey();
+
+  /**
+   * Scm Revision id of the analysed code
+   */
+  Optional<String> getScmRevisionId();
 }
index ac477e1e4686b057b95ac43c510de9813f41203c..c34d91788c827bc090292c6f72bae68b6d15ba57 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.ce.task.projectanalysis.analysis;
 
 import com.google.common.collect.ImmutableMap;
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.ce.task.util.InitializedProperty;
@@ -45,6 +46,7 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
   private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
   private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
+  private final InitializedProperty<String> scmRevisionId = new InitializedProperty<>();
 
   @Override
   public MutableAnalysisMetadataHolder setOrganizationsEnabled(boolean isOrganizationsEnabled) {
@@ -216,6 +218,21 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
     return pluginsByKey.getProperty();
   }
 
+  @Override
+  public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) {
+    checkState(!this.scmRevisionId.isInitialized(), "ScmRevisionId has already been set");
+    this.scmRevisionId.setProperty(scmRevisionId);
+    return this;
+  }
+
+  @Override
+  public Optional<String> getScmRevisionId() {
+    if (!scmRevisionId.isInitialized()) {
+      return Optional.empty();
+    }
+    return Optional.of(scmRevisionId.getProperty());
+  }
+
   @Override
   public boolean isShortLivingBranch() {
     checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
index 27a8ab3b34b25b625eaaaf92804357e4c7e1803f..ece1a0752bdae9f1607bc8018ea6861c259b096c 100644 (file)
@@ -85,4 +85,9 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
    * @throws IllegalStateException if Plugins by key has already been set
    */
   MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey);
+
+  /**
+   * @throws IllegalStateException if scm revision id has already been set
+   */
+  MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId);
 }
index 9a1eece0d5719b88ddfbed4bbfca86472b4826fe..80be483174af116bd2b4b85acb1e66f2b79ee0bb 100644 (file)
@@ -97,6 +97,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
     analysisMetadata.setAnalysisDate(reportMetadata.getAnalysisDate());
     analysisMetadata.setRootComponentRef(reportMetadata.getRootComponentRef());
     analysisMetadata.setCrossProjectDuplicationEnabled(reportMetadata.getCrossProjectDuplicationActivated());
+    analysisMetadata.setScmRevisionId(reportMetadata.getScmRevisionId());
   }
 
   /**
index 0888b1d387978f851fef557fb488a208e5dfac0a..6e4a35ee587234ed98b98b480545fc595649fe06 100644 (file)
@@ -38,6 +38,7 @@ import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS;
 public class PersistAnalysisPropertiesStep implements ComputationStep {
 
   private static final String SONAR_PULL_REQUEST = "sonar.pullrequest.";
+  private static final String SCM_REVISION_ID = "sonar.analysis.scm_revision_id";
 
   private final DbClient dbClient;
   private final AnalysisMetadataHolder analysisMetadataHolder;
@@ -67,6 +68,12 @@ public class PersistAnalysisPropertiesStep implements ComputationStep {
         }
       });
 
+    analysisMetadataHolder.getScmRevisionId().ifPresent(scmRevisionId -> analysisPropertyDtos.add(new AnalysisPropertyDto()
+      .setUuid(uuidFactory.create())
+      .setKey(SCM_REVISION_ID)
+      .setValue(scmRevisionId)
+      .setSnapshotUuid(analysisMetadataHolder.getUuid())));
+
     if (analysisPropertyDtos.isEmpty()) {
       return;
     }
index 3e77b72e1d4aa461fc274e1ebf5da40964bd60e8..5f7dfa12008976349c183787645750f5073375dd 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.ce.task.projectanalysis.analysis;
 
 import java.util.Date;
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.junit.rules.ExternalResource;
@@ -60,6 +61,8 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta
 
   private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
 
+  private final InitializedProperty<String> scmRevisionId = new InitializedProperty<>();
+
   @Override
   public AnalysisMetadataHolderRule setOrganizationsEnabled(boolean isOrganizationsEnabled) {
     this.organizationsEnabled.setProperty(isOrganizationsEnabled);
@@ -231,6 +234,21 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta
     return pluginsByKey.getProperty();
   }
 
+  @Override
+  public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) {
+    checkState(!this.scmRevisionId.isInitialized(), "ScmRevisionId has already been set");
+    this.scmRevisionId.setProperty(scmRevisionId);
+    return this;
+  }
+
+  @Override
+  public Optional<String> getScmRevisionId() {
+    if (!scmRevisionId.isInitialized()) {
+      return Optional.empty();
+    }
+    return Optional.of(scmRevisionId.getProperty());
+  }
+
   @Override
   public boolean isShortLivingBranch() {
     Branch property = this.branch.getProperty();
index 341e278272f68cca67a80e1484509d6ab5f1f23e..d08e8d30647a6ab8854e925b87ccfe82ef0556d5 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.ce.task.projectanalysis.analysis;
 
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.junit.rules.ExternalResource;
@@ -176,6 +177,19 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
     return delegate.getScannerPluginsByKey();
   }
 
+
+
+  @Override
+  public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) {
+    delegate.setScmRevisionId(scmRevisionId);
+    return this;
+  }
+
+  @Override
+  public Optional<String> getScmRevisionId() {
+    return delegate.getScmRevisionId();
+  }
+
   @Override
   public boolean isShortLivingBranch() {
     return delegate.isShortLivingBranch();
index 67fe41265df12b728d3e14e8ca96406cc8a9eac7..d4d4222a878dd839c61242095bad1baff497975f 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.step;
 import com.google.common.collect.ImmutableList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
@@ -60,6 +61,7 @@ public class PersistAnalysisPropertiesStepTest {
     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));
+  private static final String SCM_REV_ID = "sha1";
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
@@ -73,6 +75,33 @@ public class PersistAnalysisPropertiesStepTest {
   public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() {
     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
+    when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
+
+    underTest.execute(new TestComputationStepContext());
+
+    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(9);
+    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_VALUE2),
+        tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
+        tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
+        tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
+        tuple(SNAPSHOT_UUID, "sonar.analysis.scm_revision_id", SCM_REV_ID),
+        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_should_not_stores_sonarDotAnalysisDotscm_revision_id_properties_when_its_not_available_in_report_metada() {
+    when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
+    when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
+    when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.empty());
 
     underTest.execute(new TestComputationStepContext());
 
@@ -95,6 +124,7 @@ public class PersistAnalysisPropertiesStepTest {
 
   @Test
   public void persist_filtering_of_properties_is_case_sensitive() {
+    when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
       newContextProperty("sonar.ANALYSIS.foo", "foo"),
       newContextProperty("sonar.anaLysis.bar", "bar"),
@@ -106,17 +136,24 @@ public class PersistAnalysisPropertiesStepTest {
 
     underTest.execute(new TestComputationStepContext());
 
-    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0);
+    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(1);
   }
 
   @Test
-  public void persist_should_not_store_anything_if_there_is_no_context_properties() {
+  public void persist_should_only_store_scmRevisionId_if_there_is_no_context_properties() {
+    when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
 
     underTest.execute(new TestComputationStepContext());
 
-    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0);
+    assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(1);
+    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.scm_revision_id", SCM_REV_ID));
   }
 
   @Test