]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 new branch param (#2321)
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Thu, 3 Aug 2017 14:57:05 +0000 (16:57 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 08:55:10 +0000 (10:55 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java

index f58536c26548cfb1abb1e7e345d3b0f0272707ec..f1265ebc4210136eef34e835f85c931344243a27 100644 (file)
@@ -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)
index e69eb31916e26bcf5575a3791dae904611a4d997..0cbe8cc9e88ed1959b5a0ba5c1d1594256a690fe 100644 (file)
  */
 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));
+    }
+  }
+
 }
index d4ff556e8ae80dc9f74069110db444aa24d44f9e..94af94766731f7834953ef3eccd6e076bf12cdd9 100644 (file)
@@ -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");
index a48b94bf2a138af6244463a546448ce983dd981d..0ace33edc7afe72fc652f8ed852f0b94a57a2e53 100644 (file)
@@ -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
index 9138d06f8c452f4223d1163f2978eefd74b8f5bf..fd868541970de079ba81a618723335ad3409bce6 100644 (file)
@@ -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);
   }
 
 }
index 1b59e69bd187420207ae928c7f154d154dd9eafd..740bdd248180f788d6c6c361a8904980cb2cb3d0 100644 (file)
  */
 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);