From: Benoit <43733395+benoit-sns@users.noreply.github.com> Date: Mon, 12 Nov 2018 11:56:56 +0000 (+0000) Subject: SONAR-11437 Store the SHA1 read from scanner report (#926) X-Git-Tag: 7.6~178 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ae5d1e787dc020c1842330479b769b565343cdb6;p=sonarqube.git SONAR-11437 Store the SHA1 read from scanner report (#926) --- diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolder.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolder.java index a8652453d8c..7c787c74207 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolder.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolder.java @@ -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 getScannerPluginsByKey(); + + /** + * Scm Revision id of the analysed code + */ + Optional getScmRevisionId(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java index ac477e1e468..c34d91788c8 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java @@ -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 rootComponentRef = new InitializedProperty<>(); private final InitializedProperty> qProfilesPerLanguage = new InitializedProperty<>(); private final InitializedProperty> pluginsByKey = new InitializedProperty<>(); + private final InitializedProperty 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 getScmRevisionId() { + if (!scmRevisionId.isInitialized()) { + return Optional.empty(); + } + return Optional.of(scmRevisionId.getProperty()); + } + @Override public boolean isShortLivingBranch() { checkState(this.branch.isInitialized(), BRANCH_NOT_SET); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java index 27a8ab3b34b..ece1a0752bd 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java @@ -85,4 +85,9 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder { * @throws IllegalStateException if Plugins by key has already been set */ MutableAnalysisMetadataHolder setScannerPluginsByKey(Map pluginsByKey); + + /** + * @throws IllegalStateException if scm revision id has already been set + */ + MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java index 9a1eece0d57..80be483174a 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java @@ -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()); } /** diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStep.java index 0888b1d3879..6e4a35ee587 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStep.java @@ -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; } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java index 3e77b72e1d4..5f7dfa12008 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java @@ -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> pluginsByKey = new InitializedProperty<>(); + private final InitializedProperty 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 getScmRevisionId() { + if (!scmRevisionId.isInitialized()) { + return Optional.empty(); + } + return Optional.of(scmRevisionId.getProperty()); + } + @Override public boolean isShortLivingBranch() { Branch property = this.branch.getProperty(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java index 341e278272f..d08e8d30647 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java @@ -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 getScmRevisionId() { + return delegate.getScmRevisionId(); + } + @Override public boolean isShortLivingBranch() { return delegate.isShortLivingBranch(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java index 67fe41265df..d4d4222a878 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java @@ -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 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 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