*/
package org.sonar.server.qualitygate.ws;
-import com.google.common.base.Optional;
+import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.qualitygate.QualityGates;
-import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+import static org.sonar.server.qualitygate.QualityGateUpdater.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
+import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
public class DeselectAction implements QualityGatesWsAction {
- private final QualityGates qualityGates;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
+ private final QualityGatesWsSupport wsSupport;
- public DeselectAction(QualityGates qualityGates, DbClient dbClient, ComponentFinder componentFinder) {
- this.qualityGates = qualityGates;
+ public DeselectAction(DbClient dbClient, ComponentFinder componentFinder, QualityGatesWsSupport wsSupport) {
+ this.wsSupport = wsSupport;
this.dbClient = dbClient;
this.componentFinder = componentFinder;
}
public void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction("deselect")
.setDescription("Remove the association of a project from a quality gate.<br>" +
- "Requires the 'Administer Quality Gates' permission.")
+ "Requires one of the following permissions:" +
+ "<ul>" +
+ "<li>'Administer Quality Gates'</li>" +
+ "<li>'Administer' rights on the project</li>" +
+ "</ul>")
.setPost(true)
.setSince("4.3")
.setHandler(this)
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto project = getProject(dbSession, request.param(PARAM_PROJECT_ID), request.param(PARAM_PROJECT_KEY));
- qualityGates.dissociateProject(dbSession, project);
+ dissociateProject(dbSession, project);
response.noContent();
}
}
+ private void dissociateProject(DbSession dbSession, ComponentDto project) {
+ wsSupport.checkCanAdminProject(project);
+ dbClient.propertiesDao().deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
+ dbSession.commit();
+ }
+
private ComponentDto getProject(DbSession dbSession, @Nullable String projectId, @Nullable String projectKey) {
return selectProjectById(dbSession, projectId)
- .or(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ComponentFinder.ParamNames.PROJECT_ID_AND_KEY));
+ .orElseGet(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ComponentFinder.ParamNames.PROJECT_ID_AND_KEY));
}
private Optional<ComponentDto> selectProjectById(DbSession dbSession, @Nullable String projectId) {
if (projectId == null) {
- return Optional.absent();
+ return Optional.empty();
}
try {
long dbId = Long.parseLong(projectId);
- return dbClient.componentDao().selectById(dbSession, dbId);
+ return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, dbId).orNull());
} catch (NumberFormatException e) {
- return Optional.absent();
+ return Optional.empty();
}
}
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualitygate.QualityGates;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
- private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider, UuidFactoryFast.getInstance());
-
- private DeselectAction underTest = new DeselectAction(qualityGates, dbClient, TestComponentFinder.from(db));
+ private DeselectAction underTest = new DeselectAction(dbClient, TestComponentFinder.from(db),
+ new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider));
private WsActionTester ws = new WsActionTester(underTest);
@Test
- public void deselect_by_id() {
+ public void deselect_by_key() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
ComponentDto project = db.components().insertPrivateProject();
-
- ComponentDto anotherProject = db.components().insertPrivateProject();
- String gateId = valueOf(qualityGate.getId());
- associateProjectToQualityGate(project.getId(), gateId);
- associateProjectToQualityGate(anotherProject.getId(), gateId);
+ associateProjectToQualityGate(project, qualityGate);
ws.newRequest()
- .setParam("projectId", valueOf(project.getId()))
+ .setParam("projectKey", project.getDbKey())
.execute();
assertDeselected(project.getId());
- assertSelected(gateId, anotherProject.getId());
}
@Test
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
ComponentDto project = db.components().insertPrivateProject();
-
- String gateId = valueOf(qualityGate.getId());
- associateProjectToQualityGate(project.getId(), gateId);
+ associateProjectToQualityGate(project, qualityGate);
ws.newRequest()
.setParam("projectId", project.uuid())
}
@Test
- public void deselect_by_key() {
+ public void deselect_by_id() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
ComponentDto project = db.components().insertPrivateProject();
+ associateProjectToQualityGate(project, qualityGate);
- String gateId = valueOf(qualityGate.getId());
- associateProjectToQualityGate(project.getId(), gateId);
+ ws.newRequest()
+ .setParam("projectId", valueOf(project.getId()))
+ .execute();
+
+ assertDeselected(project.getId());
+ }
+
+ @Test
+ public void project_admin() {
+ OrganizationDto organization = db.organizations().insert();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject(organization);
+ associateProjectToQualityGate(project, qualityGate);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ws.newRequest()
.setParam("projectKey", project.getDbKey())
+ .setParam("organization", organization.getKey())
.execute();
assertDeselected(project.getId());
}
@Test
- public void project_admin() {
+ public void other_project_should_not_be_updated() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
ComponentDto project = db.components().insertPrivateProject();
- associateProjectToQualityGate(project.getId(), valueOf(qualityGate.getId()));
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ String gateId = valueOf(qualityGate.getId());
+ associateProjectToQualityGate(project, qualityGate);
+ // Another project
+ ComponentDto anotherProject = db.components().insertPrivateProject();
+ associateProjectToQualityGate(anotherProject, qualityGate);
+
+ ws.newRequest()
+ .setParam("projectKey", project.getKey())
+ .execute();
+
+ assertDeselected(project.getId());
+ assertSelected(gateId, anotherProject.getId());
+ }
+
+ @Test
+ public void default_organization_is_used_when_no_organization_parameter() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
+ associateProjectToQualityGate(project, qualityGate);
ws.newRequest()
.setParam("projectKey", project.getDbKey())
@Test
public void fail_when_anonymous() {
- userSession.anonymous();
ComponentDto project = db.components().insertPrivateProject();
+ userSession.anonymous();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
@Test
public void fail_when_using_branch_db_key() throws Exception {
- OrganizationDto organization = db.getDefaultOrganization();
- ComponentDto project = db.components().insertMainBranch(organization);
+ ComponentDto project = db.components().insertMainBranch();
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
@Test
public void fail_when_using_branch_id() {
- OrganizationDto organization = db.getDefaultOrganization();
- ComponentDto project = db.components().insertMainBranch(organization);
+ ComponentDto project = db.components().insertMainBranch(db.getDefaultOrganization());
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
assertThat(def.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactly(
tuple("6.6", "The parameter 'gateId' was removed"));
- assertThat(def.params()).extracting(WebService.Param::key)
- .containsExactlyInAnyOrder("projectId", "projectKey");
-
- WebService.Param projectId = def.param("projectId");
- assertThat(projectId.isRequired()).isFalse();
- assertThat(projectId.deprecatedSince()).isEqualTo("6.1");
- assertThat(projectId.description()).isNotEmpty();
- assertThat(projectId.exampleValue()).isNotEmpty();
-
- WebService.Param projectKey = def.param("projectKey");
- assertThat(projectKey.isRequired()).isFalse();
- assertThat(projectKey.since()).isEqualTo("6.1");
- assertThat(projectKey.description()).isNotEmpty();
- assertThat(projectKey.exampleValue()).isNotEmpty();
+ assertThat(def.params())
+ .extracting(WebService.Param::key, WebService.Param::isRequired)
+ .containsExactlyInAnyOrder(
+ tuple("projectKey", false),
+ tuple("projectId", false));
}
- private void associateProjectToQualityGate(long projectId, String gateId) {
+ private void associateProjectToQualityGate(ComponentDto project, QualityGateDto qualityGate) {
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setResourceId(projectId)
- .setValue(gateId)
+ .setResourceId(project.getId())
+ .setValue(qualityGate.getId().toString())
.setKey(SONAR_QUALITYGATE_PROPERTY));
db.commit();
}