diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-08-03 16:57:05 +0200 |
---|---|---|
committer | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-09-12 10:55:10 +0200 |
commit | 49309a28357a3056c1a8f481af4c5daae37676f9 (patch) | |
tree | 0c3e0dbe2ad525a6a75b0c976f02cee637ce0daa /sonar-scanner-engine | |
parent | bd0860b8fcf21d0dc7b394028a763f562fcbee41 (diff) | |
download | sonarqube-49309a28357a3056c1a8f481af4c5daae37676f9.tar.gz sonarqube-49309a28357a3056c1a8f481af4c5daae37676f9.zip |
SONAR-9616 new branch param (#2321)
Diffstat (limited to 'sonar-scanner-engine')
6 files changed, 59 insertions, 20 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java index f58536c2654..f1265ebc421 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java @@ -53,6 +53,7 @@ import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsResponse; +import static org.sonar.core.config.ScannerProperties.BRANCH_NAME; import static org.sonar.core.config.ScannerProperties.ORGANIZATION; import static org.sonar.core.util.FileUtils.deleteQuietly; @@ -177,6 +178,8 @@ public class ReportPublisher implements Startable { post.setParam("characteristic", "incremental=true"); } + settings.get(BRANCH_NAME).ifPresent(b -> post.setParam("characteristic", "branch=" + b)); + WsResponse response; try { response = wsClient.call(post).failIfNotSuccessful(); @@ -208,6 +211,7 @@ public class ReportPublisher implements Startable { metadata.put("projectKey", effectiveKey); metadata.put("serverUrl", publicUrl); metadata.put("serverVersion", server.getVersion()); + settings.get(BRANCH_NAME).ifPresent(branch -> metadata.put("branch", branch)); URL dashboardUrl = httpUrl.newBuilder() .addPathSegment("dashboard").addPathSegment("index").addPathSegment(effectiveKey) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java index e69eb31916e..0cbe8cc9e88 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java @@ -19,29 +19,29 @@ */ package org.sonar.scanner.scan; +import com.google.common.base.Joiner; import java.util.ArrayList; import java.util.List; - import javax.annotation.Nullable; - import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.MessageException; import org.sonar.core.component.ComponentKeys; +import org.sonar.core.config.ScannerProperties; import org.sonar.scanner.analysis.DefaultAnalysisMode; -import com.google.common.base.Joiner; - /** * This class aims at validating project reactor * @since 3.6 */ public class ProjectReactorValidator { private final DefaultAnalysisMode mode; + private final ProjectSettings settings; - public ProjectReactorValidator(DefaultAnalysisMode mode) { + public ProjectReactorValidator(DefaultAnalysisMode mode, ProjectSettings settings) { this.mode = mode; + this.settings = settings; } public void validate(ProjectReactor reactor) { @@ -57,6 +57,8 @@ public class ProjectReactorValidator { } } + validateBranchParams(validationMessages, branch, settings.get(ScannerProperties.BRANCH_NAME).orElse(null)); + validateBranch(validationMessages, branch); if (!validationMessages.isEmpty()) { @@ -85,4 +87,10 @@ public class ProjectReactorValidator { } } + private static void validateBranchParams(List<String> validationMessages, @Nullable String deprecatedBranch, @Nullable String branchName) { + if (StringUtils.isNotEmpty(deprecatedBranch) && StringUtils.isNotEmpty(branchName)) { + validationMessages.add(String.format("The %s parameter must not be used together with the deprecated sonar.branch parameter", ScannerProperties.BRANCH_NAME)); + } + } + } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index d4ff556e8ae..94af9476673 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -31,6 +31,7 @@ import org.sonar.api.resources.ResourceTypes; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.core.config.ScannerProperties; import org.sonar.core.metric.ScannerMetrics; import org.sonar.core.platform.ComponentContainer; import org.sonar.scanner.ProjectAnalysisInfo; @@ -232,7 +233,12 @@ public class ProjectScanContainer extends ComponentContainer { } String branch = tree.root().definition().getBranch(); if (branch != null) { - LOG.info("Branch key: {}", branch); + LOG.info("Branch key (deprecated): {}", branch); + } + + String branchName = props.property(ScannerProperties.BRANCH_NAME); + if (branchName != null) { + LOG.info("Branch name: {}", branchName); } LOG.debug("Start recursive analysis of project modules"); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index a48b94bf2a1..0ace33edc7a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -163,7 +163,7 @@ public class FileSystemMediumTest { .execute(); assertThat(logs.getAllAsString()).contains("Project key: com.foo.project"); - assertThat(logs.getAllAsString()).contains("Branch key: my-branch"); + assertThat(logs.getAllAsString()).contains("Branch key (deprecated): my-branch"); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java index 9138d06f8c4..fd868541970 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java @@ -225,15 +225,19 @@ public class ReportPublisherTest { entry("organization", "MyOrg"), entry("projectKey", "struts")); } - + @Test public void test_send_characteristics() throws Exception { ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0]); - + when(mode.isIncremental()).thenReturn(true); - settings.setProperty(ScannerProperties.ORGANIZATION, "MyOrg"); - + String orgName = "MyOrg"; + settings.setProperty(ScannerProperties.ORGANIZATION, orgName); + + String branchName = "feature"; + settings.setProperty(ScannerProperties.BRANCH_NAME, branchName); + WsResponse response = mock(WsResponse.class); PipedOutputStream out = new PipedOutputStream(); @@ -251,10 +255,11 @@ public class ReportPublisherTest { verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); - assertThat(wsRequest.getParams()).containsOnly( - entry("organization", "MyOrg"), - entry("projectKey", "struts"), - entry("characteristic", "incremental=true")); + assertThat(wsRequest.getParameters().getKeys()).hasSize(3); + assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName); + assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("struts"); + assertThat(wsRequest.getParameters().getValues("characteristic")) + .containsExactly("incremental=true", "branch=" + branchName); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java index 1b59e69bd18..740bdd24818 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java @@ -19,6 +19,11 @@ */ package org.sonar.scanner.scan; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Optional; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -27,23 +32,26 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.MessageException; +import org.sonar.core.config.ScannerProperties; import org.sonar.scanner.analysis.DefaultAnalysisMode; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class ProjectReactorValidatorTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private ProjectReactorValidator validator; private DefaultAnalysisMode mode; + private ProjectSettings settings; + private ProjectReactorValidator validator; @Before public void prepare() { mode = mock(DefaultAnalysisMode.class); - validator = new ProjectReactorValidator(mode); + + settings = mock(ProjectSettings.class); + when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.empty()); + + validator = new ProjectReactorValidator(mode, settings); } @Test @@ -153,6 +161,14 @@ public class ProjectReactorValidatorTest { validator.validate(reactor); } + @Test + public void should_fail_when_both_old_and_new_branch_property_present() { + thrown.expect(MessageException.class); + thrown.expectMessage("The sonar.branch.name parameter must not be used together with the deprecated sonar.branch parameter"); + when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.of("feature")); + validator.validate(createProjectReactor("foo", "branch1")); + } + private ProjectReactor createProjectReactor(String projectKey) { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey); ProjectReactor reactor = new ProjectReactor(def); |