From a92d72fb2520c402a5765f09a6b7c9730d8d3330 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 7 Dec 2017 16:05:25 +0100 Subject: [PATCH] SONAR-10134 Add organization parameter in rename action --- .../server/qualitygate/QualityGateFinder.java | 11 +-- .../qualitygate/ws/QualityGatesWsSupport.java | 11 +-- .../server/qualitygate/ws/RenameAction.java | 41 +++++----- .../qualitygate/ws/RenameActionTest.java | 75 +++++++++++++------ 4 files changed, 80 insertions(+), 58 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java index 6a914c6cc14..7bafdce2aa4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java @@ -49,7 +49,8 @@ public class QualityGateFinder { public Optional getQualityGate(DbSession dbSession, long componentId) { Optional qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, componentId); if (qualityGateId.isPresent()) { - return Optional.of(new QualityGateData(getById(dbSession, qualityGateId.get()), false)); + QualityGateDto qualityGate = checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId.get()), "No quality gate has been found for id %s", qualityGateId); + return Optional.of(new QualityGateData(qualityGate, false)); } else { Optional defaultQualityGate = getDefault(dbSession); if (!defaultQualityGate.isPresent()) { @@ -59,14 +60,6 @@ public class QualityGateFinder { } } - /** - * @deprecated Use {@link #getByOrganizationAndId(DbSession, OrganizationDto, long)} - */ - @Deprecated - public QualityGateDto getById(DbSession dbSession, long qualityGateId) { - return checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId), "No quality gate has been found for id %s", qualityGateId); - } - public QGateWithOrgDto getByOrganizationAndId(DbSession dbSession, OrganizationDto organization, long qualityGateId) { return checkFound(dbClient.qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGateId), "No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java index efade3cdd90..2b49421ecd7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java @@ -98,15 +98,6 @@ public class QualityGatesWsSupport { return checkFoundWithOptional(organizationDto, "No organization with key '%s'", organizationKey); } - /** - * @deprecated use {@link #checkCanEdit(QGateWithOrgDto)} instead - */ - @Deprecated - void checkCanEdit(QualityGateDto qualityGate) { - checkNotBuiltIn(qualityGate); - userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); - } - void checkCanEdit(QGateWithOrgDto qualityGate) { checkNotBuiltIn(qualityGate); userSession.checkPermission(ADMINISTER_QUALITY_GATES, qualityGate.getOrganizationUuid()); @@ -120,7 +111,7 @@ public class QualityGatesWsSupport { throw insufficientPrivilegesException(); } - void checkProjectBelongsToOrganization(OrganizationDto organization, ComponentDto project){ + void checkProjectBelongsToOrganization(OrganizationDto organization, ComponentDto project) { if (project.getOrganizationUuid().equals(organization.getUuid())) { return; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java index 212330cc98f..f0ed2f2253e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java @@ -24,6 +24,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; import org.sonarqube.ws.Qualitygates.QualityGate; @@ -67,33 +69,36 @@ public class RenameAction implements QualityGatesWsAction { .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("New name of the quality gate") .setExampleValue("My Quality Gate"); + + wsSupport.createOrganizationParam(action); } @Override public void handle(Request request, Response response) { long id = QualityGatesWs.parseId(request, PARAM_ID); - QualityGateDto qualityGate = rename(id, request.mandatoryParam(PARAM_NAME)); - writeProtobuf(QualityGate.newBuilder() - .setId(qualityGate.getId()) - .setName(qualityGate.getName()) - .build(), request, response); - } - - private QualityGateDto rename(long id, String name) { try (DbSession dbSession = dbClient.openSession(false)) { - QualityGateDto qualityGate = qualityGateFinder.getById(dbSession, id); - wsSupport.checkCanEdit(qualityGate); - checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name"); - checkNotAlreadyExists(dbSession, qualityGate, name); - qualityGate.setName(name); - dbClient.qualityGateDao().update(qualityGate, dbSession); - dbSession.commit(); - return qualityGate; + OrganizationDto organization = wsSupport.getOrganization(dbSession, request); + QualityGateDto qualityGate = rename(dbSession, organization, id, request.mandatoryParam(PARAM_NAME)); + writeProtobuf(QualityGate.newBuilder() + .setId(qualityGate.getId()) + .setName(qualityGate.getName()) + .build(), request, response); } } - private void checkNotAlreadyExists(DbSession dbSession, QualityGateDto qualityGate, String name) { - QualityGateDto existingQgate = dbClient.qualityGateDao().selectByName(dbSession, name); + private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, long id, String name) { + QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); + wsSupport.checkCanEdit(qualityGate); + checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name"); + checkNotAlreadyExists(dbSession, organization, qualityGate, name); + qualityGate.setName(name); + dbClient.qualityGateDao().update(qualityGate, dbSession); + dbSession.commit(); + return qualityGate; + } + + private void checkNotAlreadyExists(DbSession dbSession, OrganizationDto organization, QualityGateDto qualityGate, String name) { + QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name); boolean isModifyingCurrentQgate = existingQgate == null || existingQgate.getId().equals(qualityGate.getId()); checkArgument(isModifyingCurrentQgate, "Name '%s' has already been taken", name); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java index f1405906ccb..adddf7c3185 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java @@ -26,7 +26,8 @@ import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; -import org.sonar.db.qualitygate.QualityGateDto; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.DefaultOrganizationProvider; @@ -64,17 +65,22 @@ public class RenameActionTest { assertThat(action.changelog()).isEmpty(); assertThat(action.params()) .extracting(WebService.Param::key, WebService.Param::isRequired) - .containsExactlyInAnyOrder(tuple("id", true), tuple("name", true)); + .containsExactlyInAnyOrder( + tuple("id", true), + tuple("name", true), + tuple("organization", false)); } @Test public void rename() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name")); + OrganizationDto organization = db.organizations().insert(); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name")); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "new name") + .setParam("organization", organization.getKey()) .execute(); assertThat(db.getDbClient().qualityGateDao().selectById(db.getSession(), qualityGate.getId()).getName()).isEqualTo("new name"); @@ -82,12 +88,14 @@ public class RenameActionTest { @Test public void response_contains_quality_gate() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name")); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name")); QualityGate result = ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "new name") + .setParam("organization", organization.getKey()) .executeProtobuf(QualityGate.class); assertThat(result.getId()).isEqualTo(qualityGate.getId()); @@ -96,21 +104,40 @@ public class RenameActionTest { @Test public void rename_with_same_name() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("name")); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("name")); ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "name") + .setParam("organization", organization.getKey()) .execute(); assertThat(db.getDbClient().qualityGateDao().selectById(db.getSession(), qualityGate.getId()).getName()).isEqualTo("name"); } + @Test + public void default_organization_is_used_when_no_organization_parameter() { + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); + OrganizationDto otherOrganization = db.organizations().insert(); + QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + + QualityGate result = ws.newRequest() + .setParam("id", qualityGate.getId().toString()) + .setParam("name", "new name") + .executeProtobuf(QualityGate.class); + + assertThat(result.getId()).isEqualTo(qualityGate.getId()); + assertThat(result.getName()).isEqualTo("new name"); + } + @Test public void fail_on_built_in_quality_gate() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").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())); @@ -118,13 +145,15 @@ public class RenameActionTest { ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "name") + .setParam("organization", organization.getKey()) .execute(); } @Test public void fail_on_empty_name() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Name can't be empty"); @@ -132,14 +161,16 @@ public class RenameActionTest { ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "") + .setParam("organization", organization.getKey()) .execute(); } @Test public void fail_when_using_existing_name() { - logAsQualityGateAdminister(); - QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(); - QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization); + QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(format("Name '%s' has already been taken", qualityGate2.getName())); @@ -147,35 +178,37 @@ public class RenameActionTest { ws.newRequest() .setParam("id", qualityGate1.getId().toString()) .setParam("name", qualityGate2.getName()) + .setParam("organization", organization.getKey()) .execute(); } @Test public void fail_on_unknown_quality_gate() { - logAsQualityGateAdminister(); + OrganizationDto organization = db.organizations().insert(); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); expectedException.expect(NotFoundException.class); ws.newRequest() .setParam("id", "123") .setParam("name", "new name") + .setParam("organization", organization.getKey()) .execute(); } @Test public void fail_when_not_quality_gates_administer() { - userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid()); - 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, qg -> qg.setName("old name")); expectedException.expect(ForbiddenException.class); ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "new name") + .setParam("organization", organization.getKey()) .execute(); } - private void logAsQualityGateAdminister() { - userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); - } } -- 2.39.5