diff options
3 files changed, 37 insertions, 21 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java index 2d2ecee9df9..d91e28783f4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java @@ -26,15 +26,16 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.organization.DefaultOrganizationProvider; +import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.user.UserSession; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.server.qualitygate.ws.QualityGatesWs.parseId; -import static org.sonar.server.qualitygate.ws.QualityGatesWs.writeQualityGate; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; -import static org.sonar.server.ws.WsUtils.checkFound; +import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.Qualitygates.QualityGate.newBuilder; public class CopyAction implements QualityGatesWsAction { @@ -42,12 +43,15 @@ public class CopyAction implements QualityGatesWsAction { private final UserSession userSession; private final DefaultOrganizationProvider organizationProvider; private final QualityGateUpdater qualityGateUpdater; + private final QualityGateFinder qualityGateFinder; - public CopyAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider, QualityGateUpdater qualityGateUpdater) { + public CopyAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider, + QualityGateUpdater qualityGateUpdater, QualityGateFinder qualityGateFinder) { this.dbClient = dbClient; this.userSession = userSession; this.organizationProvider = organizationProvider; this.qualityGateUpdater = qualityGateUpdater; + this.qualityGateFinder = qualityGateFinder; } @Override @@ -77,15 +81,21 @@ public class CopyAction implements QualityGatesWsAction { userSession.checkPermission(ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); - QualityGateDto result; + QualityGateDto qualityGateDto = doCopy(id, destinationName); + + writeProtobuf(newBuilder() + .setId(qualityGateDto.getId()) + .setName(qualityGateDto.getName()) + .build(), request, response); + } + + private QualityGateDto doCopy(Long id, String destinationName) { try (DbSession dbSession = dbClient.openSession(false)) { - QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id); - checkFound(qualityGateDto, "No quality gate has been found for id %s", (long) id); - result = qualityGateUpdater.copy(dbSession, qualityGateDto, destinationName); + QualityGateDto qualityGateDto = qualityGateFinder.getById(dbSession, id); + QualityGateDto result = qualityGateUpdater.copy(dbSession, qualityGateDto, destinationName); dbSession.commit(); + return result; } - - writeQualityGate(result, response.newJsonWriter()).close(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java index dbdd165d5c8..c3eb5c64063 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java @@ -22,17 +22,13 @@ package org.sonar.server.qualitygate.ws; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.qualitygate.QualityGateConditionDto; -import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.ws.RemovedWebServiceHandler; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.CONTROLLER_QUALITY_GATES; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR; -import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC; -import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PERIOD; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_WARNING; @@ -112,11 +108,4 @@ public class QualityGatesWs implements WebService { } } - static JsonWriter writeQualityGate(QualityGateDto qualityGate, JsonWriter writer) { - return writer.beginObject() - .prop(PARAM_ID, qualityGate.getId()) - .prop(PARAM_NAME, qualityGate.getName()) - .endObject(); - } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java index 8c6ecdfb068..1a8d8c1158c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java @@ -35,9 +35,11 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; +import org.sonarqube.ws.Qualitygates.QualityGate; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.tuple; @@ -61,8 +63,9 @@ public class CopyActionTest { private DbSession dbSession = db.getSession(); private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()); + private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); - private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater); + private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater, qualityGateFinder); private WsActionTester ws = new WsActionTester(underTest); @Test @@ -131,6 +134,20 @@ public class CopyActionTest { } @Test + public void response_contains_quality_gate() { + userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + + QualityGate result = ws.newRequest() + .setParam(PARAM_ID, qualityGate.getId().toString()) + .setParam(PARAM_NAME, "new-name") + .executeProtobuf(QualityGate.class); + + assertThat(result.getId()).isNotEqualTo(qualityGate.getId()); + assertThat(result.getName()).isEqualTo("new-name"); + } + + @Test public void fail_when_id_parameter_is_missing() { userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); |