Skip to content

Commit

Permalink
SONAR-7924 WS api/qualitygates/select projectId parameter handles pro…
Browse files Browse the repository at this point in the history
…ject id and uuid
  • Loading branch information
teryk committed Aug 5, 2016
1 parent c64d4fa commit b142ba0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -58,8 +60,10 @@ public void define(WebService.NewController controller) {
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);
Expand All @@ -70,8 +74,8 @@ public void define(WebService.NewController controller) {
.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)
Expand Down Expand Up @@ -106,12 +110,13 @@ private void doHandle(SelectWsRequest request) {
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())) {
Expand All @@ -121,6 +126,19 @@ private ComponentDto getProject(DbSession dbSession, @Nullable Long projectId, @
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);
}
Expand Down
Expand Up @@ -78,6 +78,18 @@ public void select_by_id() throws Exception {
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());
}

Expand Down Expand Up @@ -112,7 +124,8 @@ public void system_admin() throws Exception {
userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);

callByKey(gateId, project.getKey());
assertSelected(gateId, project.getId());;
assertSelected(gateId, project.getId());
;
}

@Test
Expand Down Expand Up @@ -190,20 +203,25 @@ private QualityGateDto insertQualityGate() {

private void callByKey(String gateId, String projectKey) {
ws.newRequest()
.setMethod("POST")
.setParam("gateId", String.valueOf(gateId))
.setParam("projectKey", projectKey)
.execute();
}

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);
}
Expand Down
Expand Up @@ -23,7 +23,7 @@

public class SelectWsRequest {
private long gateId;
private Long projectId;
private String projectId;
private String projectKey;

public long getGateId() {
Expand All @@ -36,11 +36,11 @@ public SelectWsRequest setGateId(long gateId) {
}

@CheckForNull
public Long getProjectId() {
public String getProjectId() {
return projectId;
}

public SelectWsRequest setProjectId(Long projectId) {
public SelectWsRequest setProjectId(String projectId) {
this.projectId = projectId;
return this;
}
Expand Down
Expand Up @@ -33,7 +33,7 @@
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;

Expand Down

0 comments on commit b142ba0

Please sign in to comment.