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 {
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
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();
}
}
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;
}
}
- static JsonWriter writeQualityGate(QualityGateDto qualityGate, JsonWriter writer) {
- return writer.beginObject()
- .prop(PARAM_ID, qualityGate.getId())
- .prop(PARAM_NAME, qualityGate.getName())
- .endObject();
- }
-
}
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;
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
assertThat(actual.isBuiltIn()).isFalse();
}
+ @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());