]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11950 do not accept empty string for revision in CE
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 12 May 2019 18:31:55 +0000 (20:31 +0200)
committerSonarTech <sonartech@sonarsource.com>
Wed, 22 May 2019 18:21:19 +0000 (20:21 +0200)
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/api/posttask/PostProjectAnalysisTasksExecutor.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.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
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepTest.java
sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java

index a850633376241f1dea2a671d684485c00cb7bdec..80930de3c355006b5c8df82ad933e80c62ce870d 100644 (file)
@@ -148,7 +148,7 @@ public interface AnalysisMetadataHolder {
   Map<String, ScannerPlugin> getScannerPluginsByKey();
 
   /**
-   * Scm Revision id of the analysed code
+   * Scm Revision of the analysed code
    */
-  Optional<String> getScmRevisionId();
+  Optional<String> getScmRevision();
 }
index c34d91788c827bc090292c6f72bae68b6d15ba57..190364b96f26351fd78f0031320307e2e0b31829 100644 (file)
@@ -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<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<>();
+  private final InitializedProperty<String> 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<String> getScmRevisionId() {
-    if (!scmRevisionId.isInitialized()) {
+  public Optional<String> getScmRevision() {
+    if (!scmRevision.isInitialized()) {
       return Optional.empty();
     }
-    return Optional.of(scmRevisionId.getProperty());
+    return Optional.ofNullable(scmRevision.getProperty());
   }
 
   @Override
index ece1a0752bdae9f1607bc8018ea6861c259b096c..c4623a13c5ca84d02307422d18094a4257768ecf 100644 (file)
@@ -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);
 }
index 1857c6205dd0e70747755d9f8cb9e3c0ff7f318c..bbd307065886418982173f5d25854f94f4753085 100644 (file)
@@ -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();
   }
index 80be483174af116bd2b4b85acb1e66f2b79ee0bb..a0765550a606eb06bd36b8e80c879531c93894b4 100644 (file)
@@ -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());
   }
 
   /**
index b303628de238e1b40ad1a9346cd221ce5b6a251e..71d2f6ad6176edf7cb9f7ae5d677b90000c94d38 100644 (file)
@@ -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();
   }
 }
index 5f7dfa12008976349c183787645750f5073375dd..34a2e0f3657adf3ca5f9c845094c34f47ccbaf77 100644 (file)
@@ -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<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
 
-  private final InitializedProperty<String> scmRevisionId = new InitializedProperty<>();
+  private final InitializedProperty<String> 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<String> getScmRevisionId() {
-    if (!scmRevisionId.isInitialized()) {
+  public Optional<String> getScmRevision() {
+    if (!scmRevision.isInitialized()) {
       return Optional.empty();
     }
-    return Optional.of(scmRevisionId.getProperty());
+    return Optional.ofNullable(scmRevision.getProperty());
   }
 
   @Override
index d08e8d30647a6ab8854e925b87ccfe82ef0556d5..d59bc7df7e7aa832a1053077a299b9a7a6e077bf 100644 (file)
@@ -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<String> getScmRevisionId() {
-    return delegate.getScmRevisionId();
+  public Optional<String> getScmRevision() {
+    return delegate.getScmRevision();
   }
 
   @Override
index 956fbc1d4e430347e843e9cd17b058b0e1f33af6..2c54b1aaa68b884f0620c79e97abf1e896189e62 100644 (file)
@@ -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);
 
index 68bca2c288f33d99df84fe20d9ebe31d8fcc878b..4354cb751e959c7f81bd462148d1294f1f40fb57 100644 (file)
@@ -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();
index e71857cd3324c29d4749cf7255a1b39b9f58934b..1cda6a1b9f9b69ca06c8f35722b302e9ec732d4a 100644 (file)
@@ -127,6 +127,7 @@ public interface PostProjectAnalysisTask {
      * @deprecated in 7.8, replaced by {@code Analysis#getRevision()}
      * @see #getAnalysis()
      */
+    @Deprecated
     String getScmRevisionId();
   }
 }