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;
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) {
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);
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;
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);
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();
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;
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);
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());
@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"),
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