]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6834 support project branches
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 09:27:06 +0000 (11:27 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 21:49:44 +0000 (23:49 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/ReportSubmitter.java
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeSubmitWsAction.java
server/sonar-server/src/test/java/org/sonar/server/computation/ReportSubmitterTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeSubmitWsActionTest.java
sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java

index 1fe382e9b51b701e22c5c85e8913b3be096514a5..6bb18ed6ae18924119470147b8f474a1b22f0d98 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ServerSide;
+import org.sonar.core.component.ComponentKeys;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
@@ -47,13 +48,15 @@ public class ReportSubmitter {
     this.componentService = componentService;
   }
 
-  public CeTask submit(String projectKey, @Nullable String projectName, InputStream reportInput) {
+  public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
     userSession.checkGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
 
-    ComponentDto project = componentService.getNullableByKey(projectKey);
+    String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
+    ComponentDto project = componentService.getNullableByKey(effectiveProjectKey);
     if (project == null) {
       // the project does not exist -> requires to provision it
       NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
+      newProject.setBranch(projectBranch);
       newProject.setQualifier(Qualifiers.PROJECT);
       // no need to verify the permission "provisionning" as it's already handled by componentService
       project = componentService.create(newProject);
index 86c25391ce90533aa0191da9da044bc640a81ce1..d230c2d0cbbc78a3436fdb88b4d62cc571664d36 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.ws;
 
+import com.google.common.base.Strings;
 import java.io.InputStream;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.server.ws.Request;
@@ -37,6 +38,7 @@ import org.sonarqube.ws.WsCe;
 public class CeSubmitWsAction implements CeWsAction {
 
   public static final String PARAM_PROJECT_KEY = "projectKey";
+  public static final String PARAM_PROJECT_BRANCH = "projectBranch";
   public static final String PARAM_PROJECT_NAME = "projectName";
   public static final String PARAM_REPORT_DATA = "report";
 
@@ -63,6 +65,11 @@ public class CeSubmitWsAction implements CeWsAction {
       .setDescription("Key of project")
       .setExampleValue("my_project");
 
+    action
+      .createParam(PARAM_PROJECT_BRANCH)
+      .setDescription("Optional branch of project")
+      .setExampleValue("branch-1.x");
+
     action
       .createParam(PARAM_PROJECT_NAME)
       .setRequired(false)
@@ -78,10 +85,11 @@ public class CeSubmitWsAction implements CeWsAction {
   @Override
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY);
+    String projectBranch = Strings.emptyToNull(wsRequest.param(PARAM_PROJECT_BRANCH));
     String projectName = StringUtils.defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey);
     InputStream reportInput = wsRequest.paramAsInputStream(PARAM_REPORT_DATA);
 
-    CeTask task = reportSubmitter.submit(projectKey, projectName, reportInput);
+    CeTask task = reportSubmitter.submit(projectKey, projectBranch, projectName, reportInput);
     reportProcessingScheduler.startAnalysisTaskNow();
 
     WsCe.SubmitResponse submitResponse = WsCe.SubmitResponse.newBuilder()
index f9f3948f61f670b5b8e33f40c1588d710b4ae75e..856c691211ec77ede200d0ee692c0063c1828554 100644 (file)
@@ -28,8 +28,10 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentService;
+import org.sonar.server.component.NewComponent;
 import org.sonar.server.tester.UserSessionRule;
 
+import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -51,7 +53,7 @@ public class ReportSubmitterTest {
     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
     when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(new ComponentDto().setUuid("P1"));
 
-    underTest.submit("MY_PROJECT", "My Project", IOUtils.toInputStream("{binary}"));
+    underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
 
     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
       @Override
@@ -66,4 +68,28 @@ public class ReportSubmitterTest {
       }
     }));
   }
+
+  @Test
+  public void provision_project_if_does_not_exist() throws Exception {
+    when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit("TASK_1"));
+    userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
+    when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
+    when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1"));
+
+    underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
+
+    verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
+      @Override
+      protected boolean matchesSafely(CeTaskSubmit submit) {
+        return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
+          submit.getUuid().equals("TASK_1");
+      }
+
+      @Override
+      public void describeTo(Description description) {
+
+      }
+    }));
+
+  }
 }
index deb1921077a4005bd16e45c400208767eba093b0..667df7baabb7b13a738f56299e72406299c298af 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.computation.ws;
 
 import java.io.InputStream;
 import org.junit.Test;
+import org.mockito.Matchers;
 import org.sonar.core.util.Protobuf;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.server.computation.CeTask;
@@ -49,7 +50,7 @@ public class CeSubmitWsActionTest {
   @Test
   public void submit_task_to_the_queue_and_ask_for_immediate_processing() {
     CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
-    when(reportSubmitter.submit(eq("my_project"), eq("My Project"), any(InputStream.class))).thenReturn(task);
+    when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class))).thenReturn(task);
 
     TestResponse wsResponse = tester.newRequest()
       .setParam("projectKey", "my_project")
@@ -59,7 +60,7 @@ public class CeSubmitWsActionTest {
       .setMethod("POST")
       .execute();
 
-    verify(reportSubmitter).submit(eq("my_project"), eq("My Project"), any(InputStream.class));
+    verify(reportSubmitter).submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class));
     verify(reportProcessingScheduler).startAnalysisTaskNow();
 
     // verify the protobuf response
@@ -71,7 +72,7 @@ public class CeSubmitWsActionTest {
   @Test
   public void test_response_example() {
     CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
-    when(reportSubmitter.submit(eq("my_project"), eq("My Project"), any(InputStream.class))).thenReturn(task);
+    when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class))).thenReturn(task);
 
     TestResponse wsResponse = tester.newRequest()
       .setParam("projectKey", "my_project")
@@ -90,7 +91,7 @@ public class CeSubmitWsActionTest {
   @Test
   public void project_name_is_optional() {
     CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
-    when(reportSubmitter.submit(eq("my_project"), eq("my_project"), any(InputStream.class))).thenReturn(task);
+    when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("my_project"), any(InputStream.class))).thenReturn(task);
 
     tester.newRequest()
       .setParam("projectKey", "my_project")
@@ -99,7 +100,7 @@ public class CeSubmitWsActionTest {
       .setMethod("POST")
       .execute();
 
-    verify(reportSubmitter).submit(eq("my_project"), eq("my_project"), any(InputStream.class));
+    verify(reportSubmitter).submit(eq("my_project"), Matchers.isNull(String.class), eq("my_project"), any(InputStream.class));
 
   }
 }
index 232d91d4e7e2d80a31a042f74e9735ef4a771053..7601804b56c6f85772e4ab1789db6e8982af5046 100644 (file)
@@ -138,7 +138,8 @@ public class ReportPublisher implements Startable {
   void sendOrDumpReport(File report) {
     ProjectDefinition projectDefinition = projectReactor.getRoot();
     String effectiveKey = projectDefinition.getKeyWithBranch();
-    String relativeUrl = "/api/ce/submit?projectKey=" + effectiveKey + "&projectName=" + BatchUtils.encodeForUrl(projectDefinition.getName());
+    String relativeUrl = String.format("/api/ce/submit?projectKey=%s&projectName=%s&projectBranch=%s",
+      projectDefinition.getKey(), BatchUtils.encodeForUrl(projectDefinition.getName()), BatchUtils.encodeForUrl(projectDefinition.getBranch()));
 
     String dumpDirLocation = settings.getString(DUMP_REPORT_PROP_KEY);
     if (dumpDirLocation == null) {
index 561cded1c405c3059fa3827e3d45402be9813bf2..101ad3290a2c6583f441f7dc3ff5741a7f6ab556 100644 (file)
@@ -210,7 +210,7 @@ public class ProjectDefinition {
   }
 
   @CheckForNull
-  private String getBranch() {
+  public String getBranch() {
     String branch = properties.get(CoreProperties.PROJECT_BRANCH_PROPERTY);
     if (StringUtils.isNotBlank(branch)) {
       return branch;