You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreateAction.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.qualitygate.ws;
  21. import org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.organization.OrganizationDto;
  27. import org.sonar.db.permission.OrganizationPermission;
  28. import org.sonar.db.qualitygate.QualityGateDto;
  29. import org.sonar.server.qualitygate.QualityGateUpdater;
  30. import org.sonar.server.user.UserSession;
  31. import org.sonarqube.ws.Qualitygates.CreateResponse;
  32. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_CREATE;
  33. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
  34. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  35. public class CreateAction implements QualityGatesWsAction {
  36. public static final int NAME_MAXIMUM_LENGTH = 100;
  37. private final DbClient dbClient;
  38. private final UserSession userSession;
  39. private final QualityGateUpdater qualityGateUpdater;
  40. private final QualityGatesWsSupport wsSupport;
  41. public CreateAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater,
  42. QualityGatesWsSupport wsSupport) {
  43. this.dbClient = dbClient;
  44. this.userSession = userSession;
  45. this.qualityGateUpdater = qualityGateUpdater;
  46. this.wsSupport = wsSupport;
  47. }
  48. @Override
  49. public void define(WebService.NewController controller) {
  50. WebService.NewAction action = controller.createAction(ACTION_CREATE)
  51. .setPost(true)
  52. .setDescription("Create a Quality Gate.<br>" +
  53. "Requires the 'Administer Quality Gates' permission.")
  54. .setSince("4.3")
  55. .setResponseExample(getClass().getResource("create-example.json"))
  56. .setHandler(this);
  57. action.createParam(PARAM_NAME)
  58. .setRequired(true)
  59. .setMaximumLength(NAME_MAXIMUM_LENGTH)
  60. .setDescription("The name of the quality gate to create")
  61. .setExampleValue("My Quality Gate");
  62. wsSupport.createOrganizationParam(action);
  63. }
  64. @Override
  65. public void handle(Request request, Response response) {
  66. try (DbSession dbSession = dbClient.openSession(false)) {
  67. OrganizationDto organizationDto = wsSupport.getOrganization(dbSession, request);
  68. userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationDto.getUuid());
  69. String name = request.mandatoryParam(PARAM_NAME);
  70. QualityGateDto newQualityGate = qualityGateUpdater.create(dbSession, organizationDto, name);
  71. CreateResponse.Builder createResponse = CreateResponse.newBuilder()
  72. .setId(newQualityGate.getId())
  73. .setName(newQualityGate.getName());
  74. dbSession.commit();
  75. writeProtobuf(createResponse.build(), request, response);
  76. }
  77. }
  78. }