]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11631 Add sonar.buildString to scanner report
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Wed, 13 Mar 2019 14:49:24 +0000 (15:49 +0100)
committerSonarTech <sonartech@sonarsource.com>
Tue, 19 Mar 2019 19:21:27 +0000 (20:21 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/ProjectInfo.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/ProjectInfoTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java
sonar-scanner-protocol/src/main/protobuf/scanner_report.proto

index 583ccde340a5f1e47e754e9a2502df7c127b4134..368072b7eff467907a315a4ea25ace9c6880bcf3 100644 (file)
@@ -147,6 +147,7 @@ public interface CoreProperties {
   String PROJECT_BRANCH_PROPERTY = "sonar.branch";
   String PROJECT_VERSION_PROPERTY = "sonar.projectVersion";
   String CODE_PERIOD_VERSION_PROPERTY = "sonar.codePeriodVersion";
+  String BUILD_STRING_PROPERTY = "sonar.buildString";
 
   /**
    * @since 2.6
index 3ae7826629899d34864dd6709264fcf2fa576d7a..6c00301f47363081e9a441125f1414f2fe6c7950 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.MessageException;
 
 import static java.lang.String.format;
+import static org.sonar.api.CoreProperties.BUILD_STRING_PROPERTY;
 import static org.sonar.api.CoreProperties.CODE_PERIOD_VERSION_PROPERTY;
 import static org.sonar.api.CoreProperties.PROJECT_VERSION_PROPERTY;
 
@@ -46,6 +47,7 @@ public class ProjectInfo implements Startable {
   private Date analysisDate;
   private String projectVersion;
   private String codePeriodVersion;
+  private String buildString;
 
   public ProjectInfo(Configuration settings, Clock clock) {
     this.settings = settings;
@@ -64,6 +66,10 @@ public class ProjectInfo implements Startable {
     return Optional.ofNullable(codePeriodVersion);
   }
 
+  public Optional<String> getBuildString() {
+    return Optional.ofNullable(buildString);
+  }
+
   private Date loadAnalysisDate() {
     Optional<String> value = settings.get(CoreProperties.PROJECT_DATE_PROPERTY);
     if (!value.isPresent()) {
@@ -88,19 +94,23 @@ public class ProjectInfo implements Startable {
     this.analysisDate = loadAnalysisDate();
     this.projectVersion = settings.get(PROJECT_VERSION_PROPERTY)
       .map(StringUtils::trimToNull)
-      .filter(validateVersion("project"))
+      .filter(validateLengthLimit("project version"))
       .orElse(null);
     this.codePeriodVersion = settings.get(CODE_PERIOD_VERSION_PROPERTY)
       .map(StringUtils::trimToNull)
-      .filter(validateVersion("codePeriod"))
+      .filter(validateLengthLimit("codePeriod version"))
       .orElse(projectVersion);
+    this.buildString = settings.get(BUILD_STRING_PROPERTY)
+      .map(StringUtils::trimToNull)
+      .filter(validateLengthLimit("buildString"))
+      .orElse(null);
   }
 
-  private static Predicate<String> validateVersion(String versionLabel) {
-    return version -> {
-      if (version.length() > 100) {
-        throw MessageException.of(format("\"%s\" is not a valid %s version. " +
-          "The maximum length is 100 characters.", version, versionLabel));
+  private static Predicate<String> validateLengthLimit(String label) {
+    return value -> {
+      if (value.length() > 100) {
+        throw MessageException.of(format("\"%s\" is not a valid %s. " +
+          "The maximum length is 100 characters.", value, label));
       }
       return true;
     };
index c5fabaed1c943a060754803cc3ed7d66605a0251..ae97aedbf7a2d6427f8754f43fd79b9c0de20135 100644 (file)
@@ -90,6 +90,7 @@ public class MetadataPublisher implements ReportPublisherStep {
       .setRootComponentRef(rootProject.scannerId());
     projectInfo.getProjectVersion().ifPresent(builder::setProjectVersion);
     projectInfo.getCodePeriodVersion().ifPresent(builder::setCodePeriodVersion);
+    projectInfo.getBuildString().ifPresent(builder::setBuildString);
 
     properties.organizationKey().ifPresent(builder::setOrganizationKey);
 
index 7bdc75c20b75e83e2c42010c3e06a49b73a7b6bf..6786709a6b79ae9a9d4bd6bead0a2dde83b82de1 100644 (file)
@@ -98,6 +98,19 @@ public class ProjectInfoTest {
     underTest.start();
   }
 
+  @Test
+  public void fail_with_too_long_buildString() {
+    String buildString = randomAlphabetic(101);
+    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
+    settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, buildString);
+
+    expectedException.expect(MessageException.class);
+    expectedException.expectMessage("\"" + buildString + "\" is not a valid buildString. " +
+      "The maximum length is 100 characters.");
+
+    underTest.start();
+  }
+
   @Test
   @UseDataProvider("projectVersions")
   public void getCodePeriodVersion_has_value_of_projectVersion_if_property_is_unset(@Nullable String projectVersion) {
index cf46878a7dd42c92c06634344e02ebe4babe7568..75043c9997d4b38e48fc00006cd20b2665a4aecd 100644 (file)
@@ -242,6 +242,33 @@ public class MetadataPublisherTest {
     };
   }
 
+  @Test
+  @UseDataProvider("buildStrings")
+  public void write_buildString(@Nullable String buildString, String expected) throws Exception {
+    when(projectInfo.getBuildString()).thenReturn(Optional.ofNullable(buildString));
+    when(properties.organizationKey()).thenReturn(Optional.of("SonarSource"));
+
+    File outputDir = temp.newFolder();
+    ScannerReportWriter writer = new ScannerReportWriter(outputDir);
+
+    underTest.publish(writer);
+
+    ScannerReportReader reader = new ScannerReportReader(outputDir);
+    ScannerReport.Metadata metadata = reader.readMetadata();
+    assertThat(metadata.getBuildString()).isEqualTo(expected);
+  }
+
+  @DataProvider
+  public static Object[][] buildStrings() {
+    String randomBuildString = randomAlphabetic(15);
+    return new Object[][] {
+      {null, ""},
+      {"", ""},
+      {"5.6.3", "5.6.3"},
+      {randomBuildString, randomBuildString}
+    };
+  }
+
   @Test
   public void write_long_lived_branch_info() throws Exception {
     String branchName = "long-lived";
index e91ddfa31fb6f0517ba05db4d9dbf893612f8b72..90ec4d316d3ba357fa284ae4a6cdcff149c8bb6b 100644 (file)
@@ -248,6 +248,24 @@ public class ProjectReactorValidatorTest {
     };
   }
 
+  @Test
+  @UseDataProvider("validBuildStrings")
+  public void not_fail_with_valid_buildString(String validBuildString) {
+    when(projectInfo.getBuildString()).thenReturn(Optional.ofNullable(validBuildString));
+
+    underTest.validate(createProjectReactor("foo"));
+  }
+
+  @DataProvider
+  public static Object[][] validBuildStrings() {
+    return new Object[][] {
+      {null},
+      {"1.0"},
+      {"2017-10-16"},
+      {randomAscii(100)}
+    };
+  }
+
   private ProjectReactor createProjectReactor(String projectKey, String branch) {
     return createProjectReactor(projectKey, def -> def
       .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, branch));
index 19181f1faf7df7389af997f47fafa459ef76e62d..b809619222acc03773cd40e7c54c154469828458 100644 (file)
@@ -51,6 +51,7 @@ message Metadata {
 
   string projectVersion = 16;
   string codePeriodVersion = 17;
+  string buildString = 18;
 
   message QProfile {
     string key = 1;