From: Julien Lancelot Date: Thu, 7 Dec 2017 08:46:16 +0000 (+0100) Subject: SONAR-10134 Add organization parameter in api/qualitygates/destroy X-Git-Tag: 7.0-RC1~129 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4a0c3a9d916533a9811452e5c7854bde34e469a4;p=sonarqube.git SONAR-10134 Add organization parameter in api/qualitygates/destroy --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java index 614320715ef..5e226c64537 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java @@ -72,7 +72,8 @@ public class QualityGateDao implements Dao { } public void delete(QualityGateDto qGate, DbSession session) { - mapper(session).delete(qGate.getId()); + mapper(session).delete(qGate.getUuid()); + mapper(session).deleteOrgQualityGatesByQualityGateUuid(qGate.getUuid()); } public void update(QualityGateDto qGate, DbSession session) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java index 533be402260..b804b904f0e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java @@ -44,7 +44,9 @@ public interface QualityGateMapper { QualityGateDto selectBuiltIn(); - void delete(long id); + void delete(String uuid); + + void deleteOrgQualityGatesByQualityGateUuid(String uuid); void update(QualityGateDto qGate); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml index aeddd3947e4..89142897865 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml @@ -99,8 +99,12 @@ INNER JOIN organizations o ON o.default_quality_gate_uuid = qg.uuid AND o.uuid=#{organizationUuid, jdbcType=VARCHAR} - - delete from quality_gates where id=#{id} + + delete from quality_gates where uuid=#{uuid} + + + + delete from org_quality_gates where quality_gate_uuid=#{uuid} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java index 99e93b43054..03705830256 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java @@ -28,6 +28,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.organization.OrganizationDto; +import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; public class QualityGateDaoTest { @@ -168,7 +169,9 @@ public class QualityGateDaoTest { dbSession.commit(); assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid())).isNull(); + assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", qualityGate.getUuid()))).isZero(); assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, otherQualityGate.getUuid())).isNotNull(); + assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", otherQualityGate.getUuid()))).isEqualTo(1); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java index 1f5f70a3467..959ecb787d8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java @@ -25,6 +25,8 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.qualitygate.QualityGateFinder; @@ -55,20 +57,21 @@ public class DestroyAction implements QualityGatesWsAction { .setDescription("ID of the quality gate to delete") .setRequired(true) .setExampleValue("1"); + + wsSupport.createOrganizationParam(action); } @Override public void handle(Request request, Response response) { long qualityGateId = request.mandatoryParamAsLong(QualityGatesWsParameters.PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { - - QualityGateDto qualityGate = finder.getById(dbSession, qualityGateId); + OrganizationDto organization = wsSupport.getOrganization(dbSession, request); + QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId); Optional defaultQualityGate = finder.getDefault(dbSession); checkArgument(!defaultQualityGate.isPresent() || !defaultQualityGate.get().getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); wsSupport.checkCanEdit(qualityGate); dbClient.qualityGateDao().delete(qualityGate, dbSession); - dbSession.commit(); response.noContent(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java index 7579f39d0af..496eabce710 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java @@ -27,6 +27,8 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -39,9 +41,11 @@ import static java.lang.String.format; import static java.lang.String.valueOf; import static org.apache.commons.lang.StringUtils.EMPTY; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; +import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION; public class DestroyActionTest { @@ -61,68 +65,71 @@ public class DestroyActionTest { private DestroyAction underTest = new DestroyAction(dbClient, wsSupport, qualityGateFinder); private WsActionTester ws = new WsActionTester(underTest); - @Test - public void definition() { - WebService.Action definition = ws.getDef(); - - assertThat(definition.since()).isEqualTo("4.3"); - assertThat(definition.isPost()).isTrue(); - assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("id"); - - WebService.Param id = definition.param("id"); - assertThat(id.isRequired()).isTrue(); - } - @Test public void delete_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); - Long qualityGateId = qualityGate.getId(); - assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); ws.newRequest() - .setParam(PARAM_ID, valueOf(qualityGateId)) + .setParam(PARAM_ID, qualityGate.getId().toString()) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); - assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNull(); + assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull(); + assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero(); } @Test public void delete_quality_gate_if_non_default_when_a_default_exist() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete")); - Long toDeleteQualityGateId = qualityGate.getId(); - assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNotNull(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default")); + QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(); db.qualityGates().setDefaultQualityGate(defaultQualityGate); ws.newRequest() - .setParam(PARAM_ID, valueOf(toDeleteQualityGateId)) + .setParam(PARAM_ID, valueOf(qualityGate.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); - assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNull(); + assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull(); } @Test public void does_not_delete_built_in_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); - Long qualityGateId = qualityGate.getId(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName())); ws.newRequest() - .setParam(PARAM_ID, valueOf(qualityGateId)) + .setParam(PARAM_ID, valueOf(qualityGate.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); + } + + @Test + public void default_organization_is_used_when_no_organization_parameter() { + userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); + OrganizationDto otherOrganization = db.organizations().insert(); + QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); - assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull(); + ws.newRequest() + .setParam(PARAM_ID, valueOf(qualityGate.getId())) + .execute(); + + assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, db.getDefaultOrganization(), qualityGate.getId())).isNull(); } @Test public void fail_when_invalid_id() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); String invalidId = "invalid-id"; @@ -131,56 +138,78 @@ public class DestroyActionTest { ws.newRequest() .setParam(PARAM_ID, valueOf(invalidId)) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } @Test public void fail_when_missing_id() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); expectedException.expect(IllegalArgumentException.class); ws.newRequest() .setParam(PARAM_ID, valueOf(EMPTY)) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } @Test public void fail_to_delete_default_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete")); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); db.qualityGates().setDefaultQualityGate(qualityGate); - Long qualityGateId = qualityGate.getId(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The default quality gate cannot be removed"); ws.newRequest() - .setParam(PARAM_ID, valueOf(qualityGateId)) + .setParam(PARAM_ID, valueOf(qualityGate.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } @Test public void fail_on_unknown_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); expectedException.expect(NotFoundException.class); ws.newRequest() .setParam(PARAM_ID, "123") + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } @Test public void fail_when_not_quality_gates_administer() { - userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name")); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); expectedException.expect(ForbiddenException.class); ws.newRequest() .setParam(PARAM_ID, qualityGate.getId().toString()) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } + @Test + public void definition() { + WebService.Action action = ws.getDef(); + + assertThat(action.since()).isEqualTo("4.3"); + assertThat(action.isPost()).isTrue(); + + assertThat(action.params()) + .extracting(WebService.Param::key, WebService.Param::isRequired) + .containsExactlyInAnyOrder( + tuple("id", true), + tuple("organization", false)); + } + }