From: Simon Brandhof Date: Sun, 12 May 2019 18:31:55 +0000 (+0200) Subject: SONAR-11950 do not accept empty string for revision in CE X-Git-Tag: 7.8~169 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2ea053692eef94289ec0246d58fdf077b092d8c2;p=sonarqube.git SONAR-11950 do not accept empty string for revision in CE --- 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 a8506333762..80930de3c35 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 @@ -148,7 +148,7 @@ public interface AnalysisMetadataHolder { Map getScannerPluginsByKey(); /** - * Scm Revision id of the analysed code + * Scm Revision of the analysed code */ - Optional getScmRevisionId(); + Optional getScmRevision(); } 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 c34d91788c8..190364b96f2 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 @@ -31,6 +31,7 @@ import org.sonar.server.qualityprofile.QualityProfile; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder { private static final String BRANCH_NOT_SET = "Branch has not been set"; @@ -46,7 +47,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<>(); + private final InitializedProperty scmRevision = new InitializedProperty<>(); @Override public MutableAnalysisMetadataHolder setOrganizationsEnabled(boolean isOrganizationsEnabled) { @@ -219,18 +220,18 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder } @Override - public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) { - checkState(!this.scmRevisionId.isInitialized(), "ScmRevisionId has already been set"); - this.scmRevisionId.setProperty(scmRevisionId); + public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) { + checkState(!this.scmRevision.isInitialized(), "ScmRevision has already been set"); + this.scmRevision.setProperty(defaultIfBlank(s, null)); return this; } @Override - public Optional getScmRevisionId() { - if (!scmRevisionId.isInitialized()) { + public Optional getScmRevision() { + if (!scmRevision.isInitialized()) { return Optional.empty(); } - return Optional.of(scmRevisionId.getProperty()); + return Optional.ofNullable(scmRevision.getProperty()); } @Override 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 ece1a0752bd..c4623a13c5c 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 @@ -89,5 +89,5 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder { /** * @throws IllegalStateException if scm revision id has already been set */ - MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId); + MutableAnalysisMetadataHolder setScmRevision(String scmRevisionId); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 1857c6205dd..bbd30706588 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -142,7 +142,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor Long analysisDate = getAnalysisDate(); if (analysisDate != null) { - return of(new AnalysisImpl(analysisMetadataHolder.getUuid(), analysisDate, analysisMetadataHolder.getScmRevisionId())); + return of(new AnalysisImpl(analysisMetadataHolder.getUuid(), analysisDate, analysisMetadataHolder.getScmRevision())); } return empty(); } 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 80be483174a..a0765550a60 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,7 +97,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { analysisMetadata.setAnalysisDate(reportMetadata.getAnalysisDate()); analysisMetadata.setRootComponentRef(reportMetadata.getRootComponentRef()); analysisMetadata.setCrossProjectDuplicationEnabled(reportMetadata.getCrossProjectDuplicationActivated()); - analysisMetadata.setScmRevisionId(reportMetadata.getScmRevisionId()); + analysisMetadata.setScmRevision(reportMetadata.getScmRevisionId()); } /** diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index b303628de23..71d2f6ad617 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -19,7 +19,6 @@ */ package org.sonar.ce.task.projectanalysis.analysis; -import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -383,26 +382,40 @@ public class AnalysisMetadataHolderImplTest { } @Test - public void setScmRevisionId_throws_ISE_when_called_twice() { + public void setScmRevision_throws_ISE_when_called_twice() { AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); - underTest.setScmRevisionId("scm_revision_id1"); + underTest.setScmRevision("bd56dab"); expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("ScmRevisionId has already been set"); - underTest.setScmRevisionId("scm_revision_id1"); + expectedException.expectMessage("ScmRevision has already been set"); + underTest.setScmRevision("bd56dab"); } @Test - public void getScmRevisionId_returns_empty_if_scmRevisionId_is_not_initialized() { + public void getScmRevision_returns_empty_if_scmRevision_is_not_initialized() { AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); - assertThat(underTest.getScmRevisionId()).isNotPresent(); + assertThat(underTest.getScmRevision()).isNotPresent(); } @Test - public void getScmRevisionId_returns_scmRevisionId_if_scmRevisionId_is_initialized() { + public void getScmRevision_returns_scmRevision_if_scmRevision_is_initialized() { AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); - underTest.setScmRevisionId("scm_revision_id"); - assertThat(underTest.getScmRevisionId()).isEqualTo(Optional.of("scm_revision_id")); + underTest.setScmRevision("bd56dab"); + assertThat(underTest.getScmRevision()).hasValue("bd56dab"); + } + + @Test + public void getScmRevision_does_not_return_empty_string() { + AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); + underTest.setScmRevision(""); + assertThat(underTest.getScmRevision()).isEmpty(); + } + + @Test + public void getScmRevision_does_not_return_blank_string() { + AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); + underTest.setScmRevision(" "); + assertThat(underTest.getScmRevision()).isEmpty(); } } 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 5f7dfa12008..34a2e0f3657 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 @@ -34,6 +34,7 @@ import org.sonar.server.qualityprofile.QualityProfile; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder { @@ -61,7 +62,7 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta private final InitializedProperty> pluginsByKey = new InitializedProperty<>(); - private final InitializedProperty scmRevisionId = new InitializedProperty<>(); + private final InitializedProperty scmRevision = new InitializedProperty<>(); @Override public AnalysisMetadataHolderRule setOrganizationsEnabled(boolean isOrganizationsEnabled) { @@ -235,18 +236,18 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta } @Override - public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) { - checkState(!this.scmRevisionId.isInitialized(), "ScmRevisionId has already been set"); - this.scmRevisionId.setProperty(scmRevisionId); + public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) { + checkState(!this.scmRevision.isInitialized(), "ScmRevisionId has already been set"); + this.scmRevision.setProperty(defaultIfBlank(s, null)); return this; } @Override - public Optional getScmRevisionId() { - if (!scmRevisionId.isInitialized()) { + public Optional getScmRevision() { + if (!scmRevision.isInitialized()) { return Optional.empty(); } - return Optional.of(scmRevisionId.getProperty()); + return Optional.ofNullable(scmRevision.getProperty()); } @Override 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 d08e8d30647..d59bc7df7e7 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 @@ -180,14 +180,14 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen @Override - public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) { - delegate.setScmRevisionId(scmRevisionId); + public MutableAnalysisMetadataHolder setScmRevision(String scmRevisionId) { + delegate.setScmRevision(scmRevisionId); return this; } @Override - public Optional getScmRevisionId() { - return delegate.getScmRevisionId(); + public Optional getScmRevision() { + return delegate.getScmRevision(); } @Override 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 956fbc1d4e4..2c54b1aaa68 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 @@ -75,7 +75,7 @@ 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)); + when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID)); underTest.execute(new TestComputationStepContext()); @@ -98,7 +98,7 @@ public class PersistAnalysisPropertiesStepTest { @Test public void persist_filtering_of_properties_is_case_sensitive() { - when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID)); + when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID)); when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of( newContextProperty("sonar.ANALYSIS.foo", "foo"), newContextProperty("sonar.anaLysis.bar", "bar"), @@ -115,7 +115,7 @@ public class PersistAnalysisPropertiesStepTest { @Test public void persist_should_store_nothing_if_there_are_no_context_properties() { - when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID)); + when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID)); when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator()); when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepTest.java index 68bca2c288f..4354cb751e9 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepTest.java @@ -78,7 +78,7 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime(); analysisMetadataHolder.setUuid(ANALYSIS_UUID); analysisMetadataHolder.setAnalysisDate(analysisDate); - analysisMetadataHolder.setScmRevisionId(REVISION_ID); + analysisMetadataHolder.setScmRevision(REVISION_ID); dbIdsRepository = new DbIdsRepositoryImpl(); now = DateUtils.parseDateQuietly("2015-06-02").getTime(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java index e71857cd332..1cda6a1b9f9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java @@ -127,6 +127,7 @@ public interface PostProjectAnalysisTask { * @deprecated in 7.8, replaced by {@code Analysis#getRevision()} * @see #getAnalysis() */ + @Deprecated String getScmRevisionId(); } }