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
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;
private Date analysisDate;
private String projectVersion;
private String codePeriodVersion;
+ private String buildString;
public ProjectInfo(Configuration settings, Clock clock) {
this.settings = settings;
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()) {
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;
};
.setRootComponentRef(rootProject.scannerId());
projectInfo.getProjectVersion().ifPresent(builder::setProjectVersion);
projectInfo.getCodePeriodVersion().ifPresent(builder::setCodePeriodVersion);
+ projectInfo.getBuildString().ifPresent(builder::setBuildString);
properties.organizationKey().ifPresent(builder::setOrganizationKey);
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) {
};
}
+ @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";
};
}
+ @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));
string projectVersion = 16;
string codePeriodVersion = 17;
+ string buildString = 18;
message QProfile {
string key = 1;