package org.sonar.server.qualitygate.ws;
import java.util.Optional;
+import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.ComponentFinder.ParamNames;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsQualityGates.GetByProjectWsResponse;
-import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.ACTION_GET_BY_PROJECT;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_ID;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_KEY;
public class GetByProjectAction implements QualityGatesWsAction {
+ private static final String PARAM_PROJECT = "project";
+
private final UserSession userSession;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION_GET_BY_PROJECT)
- .setInternal(true)
+ .setInternal(false)
.setSince("6.1")
- .setDescription("Get the quality gate of a project.<br> " +
- "Either project id or project key must be provided, not both.")
+ .setDescription("Get the quality gate of a project.<br />" +
+ "Requires one of the following permissions:" +
+ "<ul>" +
+ "<li>'Administer System'</li>" +
+ "<li>'Administer' rights on the specified project</li>" +
+ "<li>'Browse' on the specified project</li>" +
+ "</ul>")
.setResponseExample(getClass().getResource("get_by_project-example.json"))
- .setHandler(this);
-
- action.createParam(PARAM_PROJECT_ID)
- .setDescription("Project id")
- .setExampleValue(UUID_EXAMPLE_01);
-
- action.createParam(PARAM_PROJECT_KEY)
+ .setHandler(this)
+ .setChangelog(
+ new Change("6.6", "The parameter 'projectId' has been removed"),
+ new Change("6.6", "The parameter 'projectKey' has been renamed to 'project'"),
+ new Change("6.6", "This webservice is now part of the public API")
+ );
+
+ action.createParam(PARAM_PROJECT)
.setDescription("Project key")
- .setExampleValue(KEY_PROJECT_EXAMPLE_001);
+ .setExampleValue(KEY_PROJECT_EXAMPLE_001)
+ .setRequired(true);
}
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- ComponentDto project = getProject(dbSession, request.param(PARAM_PROJECT_ID), request.param(PARAM_PROJECT_KEY));
- Optional<QualityGateData> data = qualityGateFinder.getQualityGate(dbSession, project.getId());
+ ComponentDto project = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_PROJECT));
- writeProtobuf(buildResponse(data), request, response);
- }
- }
+ if (!userSession.hasComponentPermission(UserRole.USER, project) &&
+ !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
+ throw insufficientPrivilegesException();
+ }
- private ComponentDto getProject(DbSession dbSession, String projectUuid, String projectKey) {
- ComponentDto project = componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, ParamNames.PROJECT_ID_AND_KEY);
+ Optional<QualityGateData> data = qualityGateFinder.getQualityGate(dbSession, project.getId());
- if (!userSession.hasComponentPermission(UserRole.USER, project) &&
- !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
- throw insufficientPrivilegesException();
+ writeProtobuf(buildResponse(data), request, response);
}
-
- return project;
}
private static GetByProjectWsResponse buildResponse(Optional<QualityGateData> data) {
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.test.JsonAssert.assertJson;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_ID;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_KEY;
public class GetByProjectActionTest {
@Rule
public void definition() {
WebService.Action def = ws.getDef();
assertThat(def.description()).isNotEmpty();
- assertThat(def.isInternal()).isTrue();
+ assertThat(def.isInternal()).isFalse();
assertThat(def.since()).isEqualTo("6.1");
- assertThat(def.changelog()).isEmpty();
- assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("projectId", "projectKey");
-
- WebService.Param projectKey = def.param("projectKey");
+ assertThat(def.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder(
+ tuple("6.6", "The parameter 'projectId' has been removed"),
+ tuple("6.6", "The parameter 'projectKey' has been renamed to 'project'"),
+ tuple("6.6", "This webservice is now part of the public API")
+ );
+ assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project");
+
+ WebService.Param projectKey = def.param("project");
assertThat(projectKey.description()).isNotEmpty();
- assertThat(projectKey.isRequired()).isFalse();
+ assertThat(projectKey.isRequired()).isTrue();
assertThat(projectKey.exampleValue()).isNotEmpty();
}
associateProjectToQualityGate(project.getId(), qualityGate.getId());
logInAsProjectUser(project);
- String result = ws.newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).execute().getInput();
+ String result = ws.newRequest().setParam("project", project.getKey()).execute().getInput();
assertJson(result)
.ignoreFields("id")
insertQualityGate("Another QG");
logInAsProjectUser(project);
- String result = ws.newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).execute().getInput();
+ String result = ws.newRequest().setParam("project", project.getKey()).execute().getInput();
assertThat(result).isEqualToIgnoringWhitespace("{}");
}
setDefaultQualityGate(dbQualityGate.getId());
logInAsProjectUser(project);
- GetByProjectWsResponse result = callByUuid(project.uuid());
+ GetByProjectWsResponse result = callByKey(project.getKey());
WsQualityGates.QualityGate qualityGate = result.getQualityGate();
assertThat(Long.valueOf(qualityGate.getId())).isEqualTo(dbQualityGate.getId());
associateProjectToQualityGate(project.getId(), dbQualityGate.getId());
logInAsProjectUser(project);
- GetByProjectWsResponse result = callByUuid(project.uuid());
+ GetByProjectWsResponse result = callByKey(project.getKey());
WsQualityGates.QualityGate qualityGate = result.getQualityGate();
assertThat(qualityGate.getName()).isEqualTo(dbQualityGate.getName());
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());
- GetByProjectWsResponse result = callByUuid(project.uuid());
+ GetByProjectWsResponse result = callByKey(project.getKey());
assertThat(result.getQualityGate().getName()).isEqualTo(dbQualityGate.getName());
}
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());
- GetByProjectWsResponse result = callByUuid(project.uuid());
+ GetByProjectWsResponse result = callByKey(project.getKey());
assertThat(result.getQualityGate().getName()).isEqualTo(dbQualityGate.getName());
}
expectedException.expect(ForbiddenException.class);
- callByUuid(project.uuid());
+ callByKey(project.getKey());
}
@Test
public void fail_when_project_does_not_exist() {
expectedException.expect(NotFoundException.class);
- callByUuid("Unknown");
+ callByKey("Unknown");
}
@Test
public void fail_when_no_parameter() {
expectedException.expect(IllegalArgumentException.class);
- call(null, null);
- }
-
- @Test
- public void fail_when_project_uuid_and_key_provided() {
- expectedException.expect(IllegalArgumentException.class);
-
- call("uuid", "key");
+ call(null);
}
@Test
expectedException.expect(NotFoundException.class);
expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
- call(null, branch.getDbKey());
- }
-
- @Test
- public void fail_when_using_branch_uuid() throws Exception {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertMainBranch(organization);
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- ComponentDto branch = db.components().insertProjectBranch(project);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
-
- call(branch.uuid(), null);
- }
-
- private GetByProjectWsResponse callByUuid(String projectUuid) {
- return call(projectUuid, null);
+ call(branch.getDbKey());
}
private GetByProjectWsResponse callByKey(String projectKey) {
- return call(null, projectKey);
+ return call(projectKey);
}
- private GetByProjectWsResponse call(@Nullable String projectUuid, @Nullable String projectKey) {
+ private GetByProjectWsResponse call(@Nullable String projectKey) {
TestRequest request = ws.newRequest();
-
- if (projectUuid != null) {
- request.setParam(PARAM_PROJECT_ID, projectUuid);
- }
-
if (projectKey != null) {
- request.setParam(PARAM_PROJECT_KEY, projectKey);
+ request.setParam("project", projectKey);
}
-
return request.executeProtobuf(GetByProjectWsResponse.class);
}