From b142ba0fc238b540c31aa79ce30c04997abd0e9e Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 5 Aug 2016 17:11:03 +0200 Subject: [PATCH] SONAR-7924 WS api/qualitygates/select projectId parameter handles project id and uuid --- .../server/qualitygate/ws/SelectAction.java | 30 +++++++++++++++---- .../qualitygate/ws/SelectActionTest.java | 24 +++++++++++++-- .../client/qualitygate/SelectWsRequest.java | 6 ++-- .../qualitygate/QualityGatesServiceTest.java | 2 +- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java index 5e162479244..e60d1b73d61 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java @@ -19,12 +19,14 @@ */ 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.
" + "The '%s' or '%s' must be provided.
" + + "Project id as a numeric value is deprecated since 6.1. Please use the id similar to '%s'.
" + "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 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); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java index 3cfc31a05e9..de044146f2f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java @@ -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); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java index a3dc530f7b7..a81f5e3f253 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java @@ -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; } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java index 3000be63863..38ef7ba77c2 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java @@ -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; -- 2.39.5