]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7924 WS api/qualitygates/select projectId parameter handles project id and...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 5 Aug 2016 15:11:03 +0000 (17:11 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 5 Aug 2016 15:11:03 +0000 (17:11 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java
sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java

index 5e16247924413b8ea9ce354f20da6ae96a657e5c..e60d1b73d61bfd805ff4bc95dbf541c785e6a9db 100644 (file)
  */
 package org.sonar.server.qualitygate.ws;
 
+import com.google.common.base.Optional;
 import org.elasticsearch.common.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -58,8 +60,10 @@ public class SelectAction implements QualityGatesWsAction {
     WebService.NewAction action = controller.createAction("select")
       .setDescription("Associate a project to a quality gate.<br>" +
         "The '%s' or '%s' must be provided.<br>" +
+        "Project id as a numeric value is deprecated since 6.1. Please use the id similar to '%s'.<br>" +
         "Require Administer Quality Gates permission.",
-        PARAM_PROJECT_ID, PARAM_PROJECT_KEY)
+        PARAM_PROJECT_ID, PARAM_PROJECT_KEY,
+        Uuids.UUID_EXAMPLE_02)
       .setPost(true)
       .setSince("4.3")
       .setHandler(this);
@@ -70,8 +74,8 @@ public class SelectAction implements QualityGatesWsAction {
       .setExampleValue("1");
 
     action.createParam(PARAM_PROJECT_ID)
-      .setDescription("Project id")
-      .setExampleValue("12")
+      .setDescription("Project id. Project id as an numeric value is deprecated since 6.1")
+      .setExampleValue(Uuids.UUID_EXAMPLE_01)
       .setDeprecatedSince("6.1");
 
     action.createParam(PARAM_PROJECT_KEY)
@@ -106,12 +110,13 @@ public class SelectAction implements QualityGatesWsAction {
   private static SelectWsRequest toSelectWsRequest(Request request) {
     return new SelectWsRequest()
       .setGateId(request.mandatoryParamAsLong(PARAM_GATE_ID))
-      .setProjectId(request.paramAsLong(PARAM_PROJECT_ID))
+      .setProjectId(request.param(PARAM_PROJECT_ID))
       .setProjectKey(request.param(PARAM_PROJECT_KEY));
   }
 
-  private ComponentDto getProject(DbSession dbSession, @Nullable Long projectId, @Nullable String projectKey) {
-    ComponentDto project = componentFinder.getByIdOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY);
+  private ComponentDto getProject(DbSession dbSession, @Nullable String projectId, @Nullable String projectKey) {
+    ComponentDto project = selectProjectById(dbSession, projectId)
+      .or(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY));
 
     if (!userSession.hasPermission(GlobalPermissions.QUALITY_GATE_ADMIN) &&
       !userSession.hasComponentUuidPermission(UserRole.ADMIN, project.uuid())) {
@@ -121,6 +126,19 @@ public class SelectAction implements QualityGatesWsAction {
     return project;
   }
 
+  private Optional<ComponentDto> selectProjectById(DbSession dbSession, @Nullable String projectId) {
+    if (projectId == null) {
+      return Optional.absent();
+    }
+
+    try {
+      long dbId = Long.parseLong(projectId);
+      return dbClient.componentDao().selectById(dbSession, dbId);
+    } catch (NumberFormatException e) {
+      return Optional.absent();
+    }
+  }
+
   private static void checkQualityGate(DbClient dbClient, long id) {
     checkFound(dbClient.qualityGateDao().selectById(id), "There is no quality gate with id=" + id);
   }
index 3cfc31a05e939d5b2b173560e2a7ea600d465f91..de044146f2f3f2a291a4d3889db7c629713bb266 100644 (file)
@@ -78,6 +78,18 @@ public class SelectActionTest {
     String gateId = String.valueOf(gate.getId());
 
     callById(gateId, project.getId());
+
+    assertSelected(gateId, project.getId());
+  }
+
+  @Test
+  public void select_by_uuid() throws Exception {
+    ComponentDto project = insertProject();
+    QualityGateDto gate = insertQualityGate();
+    String gateId = String.valueOf(gate.getId());
+
+    callByUuid(gateId, project.uuid());
+
     assertSelected(gateId, project.getId());
   }
 
@@ -112,7 +124,8 @@ public class SelectActionTest {
     userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
 
     callByKey(gateId, project.getKey());
-    assertSelected(gateId, project.getId());;
+    assertSelected(gateId, project.getId());
+    ;
   }
 
   @Test
@@ -190,7 +203,6 @@ public class SelectActionTest {
 
   private void callByKey(String gateId, String projectKey) {
     ws.newRequest()
-      .setMethod("POST")
       .setParam("gateId", String.valueOf(gateId))
       .setParam("projectKey", projectKey)
       .execute();
@@ -198,12 +210,18 @@ public class SelectActionTest {
 
   private void callById(String gateId, Long projectId) {
     ws.newRequest()
-      .setMethod("POST")
       .setParam("gateId", String.valueOf(gateId))
       .setParam("projectId", String.valueOf(projectId))
       .execute();
   }
 
+  private void callByUuid(String gateId, String projectUuid) {
+    ws.newRequest()
+      .setParam("gateId", String.valueOf(gateId))
+      .setParam("projectId", projectUuid)
+      .execute();
+  }
+
   private void assertSelected(String gateId, Long projectId) {
     assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(gateId);
   }
index a3dc530f7b77891686872698dd6ec0bb7c9d7d76..a81f5e3f2535085cd2f2ce066dc7ee0383d4398e 100644 (file)
@@ -23,7 +23,7 @@ import javax.annotation.CheckForNull;
 
 public class SelectWsRequest {
   private long gateId;
-  private Long projectId;
+  private String projectId;
   private String projectKey;
 
   public long getGateId() {
@@ -36,11 +36,11 @@ public class SelectWsRequest {
   }
 
   @CheckForNull
-  public Long getProjectId() {
+  public String getProjectId() {
     return projectId;
   }
 
-  public SelectWsRequest setProjectId(Long projectId) {
+  public SelectWsRequest setProjectId(String projectId) {
     this.projectId = projectId;
     return this;
   }
index 3000be63863f5e5ed268ac23f133cecfe59a5b54..38ef7ba77c2af66d0c78bbaecf286276ed1128fc 100644 (file)
@@ -33,7 +33,7 @@ import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM
 import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_GATE_ID;
 
 public class QualityGatesServiceTest {
-  private static final Long PROJECT_ID_VALUE = 195L;
+  private static final String PROJECT_ID_VALUE = "195";
   private static final String PROJECT_KEY_VALUE = "project_key_value";
   private static final Long GATE_ID_VALUE = 243L;