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.

SetAsDefaultAction.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.Change;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.property.PropertyDto;
  28. import org.sonar.db.qualitygate.QualityGateDto;
  29. import org.sonar.server.user.UserSession;
  30. import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
  31. import static org.sonar.server.qualitygate.ws.CreateAction.NAME_MAXIMUM_LENGTH;
  32. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
  33. public class SetAsDefaultAction implements QualityGatesWsAction {
  34. private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default";
  35. private final DbClient dbClient;
  36. private final UserSession userSession;
  37. private final QualityGatesWsSupport wsSupport;
  38. public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGatesWsSupport qualityGatesWsSupport) {
  39. this.dbClient = dbClient;
  40. this.userSession = userSession;
  41. this.wsSupport = qualityGatesWsSupport;
  42. }
  43. @Override
  44. public void define(WebService.NewController controller) {
  45. WebService.NewAction action = controller.createAction("set_as_default")
  46. .setDescription("Set a quality gate as the default quality gate.<br>" +
  47. "Either 'id' or 'name' must be specified. Requires the 'Administer Quality Gates' permission.")
  48. .setSince("4.3")
  49. .setChangelog(
  50. new Change("10.0", "Parameter 'id' is removed. Use 'name' instead."),
  51. new Change("8.4", "Parameter 'name' added"),
  52. new Change("8.4", "Parameter 'id' is deprecated. Format changes from integer to string. Use 'name' instead."))
  53. .setPost(true)
  54. .setHandler(this);
  55. action.createParam(PARAM_NAME)
  56. .setDescription("Name of the quality gate to set as default")
  57. .setRequired(true)
  58. .setMaximumLength(NAME_MAXIMUM_LENGTH)
  59. .setSince("8.4")
  60. .setExampleValue("SonarSource Way");
  61. }
  62. @Override
  63. public void handle(Request request, Response response) {
  64. String name = request.mandatoryParam(PARAM_NAME);
  65. try (DbSession dbSession = dbClient.openSession(false)) {
  66. userSession.checkPermission(ADMINISTER_QUALITY_GATES);
  67. QualityGateDto qualityGate;
  68. qualityGate = wsSupport.getByName(dbSession, name);
  69. dbClient.propertiesDao().saveProperty(new PropertyDto().setKey(DEFAULT_QUALITY_GATE_PROPERTY_NAME).setValue(qualityGate.getUuid()));
  70. dbSession.commit();
  71. }
  72. response.noContent();
  73. }
  74. }