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.

CreateConditionAction.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.qualitygate.QualityGateConditionDto;
  28. import org.sonar.db.qualitygate.QualityGateDto;
  29. import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
  30. import org.sonarqube.ws.Qualitygates.CreateConditionResponse;
  31. import static org.sonar.server.qualitygate.ws.QualityGatesWs.addConditionParams;
  32. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_CREATE_CONDITION;
  33. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR;
  34. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_NAME;
  35. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
  36. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
  37. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  38. public class CreateConditionAction implements QualityGatesWsAction {
  39. private final DbClient dbClient;
  40. private final QualityGateConditionsUpdater qualityGateConditionsUpdater;
  41. private final QualityGatesWsSupport wsSupport;
  42. public CreateConditionAction(DbClient dbClient, QualityGateConditionsUpdater qualityGateConditionsUpdater, QualityGatesWsSupport wsSupport) {
  43. this.dbClient = dbClient;
  44. this.qualityGateConditionsUpdater = qualityGateConditionsUpdater;
  45. this.wsSupport = wsSupport;
  46. }
  47. @Override
  48. public void define(WebService.NewController controller) {
  49. WebService.NewAction createCondition = controller.createAction(ACTION_CREATE_CONDITION)
  50. .setPost(true)
  51. .setDescription("Add a new condition to a quality gate.<br>" +
  52. "Either 'gateId' or 'gateName' must be provided. Requires the 'Administer Quality Gates' permission.")
  53. .setSince("4.3")
  54. .setResponseExample(getClass().getResource("create-condition-example.json"))
  55. .setChangelog(
  56. new Change("7.6", "Removed optional 'warning' and 'period' parameters"),
  57. new Change("7.6", "Made 'error' parameter mandatory"),
  58. new Change("7.6", "Reduced the possible values of 'op' parameter to LT and GT"),
  59. new Change("8.4", "Parameter 'gateName' added"),
  60. new Change("8.4", "Parameter 'gateId' is deprecated. Use 'gateName' instead."),
  61. new Change("10.0", "Parameter 'gateId' is removed. Use 'gateName' instead."))
  62. .setHandler(this);
  63. createCondition
  64. .createParam(PARAM_GATE_NAME)
  65. .setRequired(true)
  66. .setDescription("Name of the quality gate")
  67. .setExampleValue("SonarSource way");
  68. addConditionParams(createCondition);
  69. }
  70. @Override
  71. public void handle(Request request, Response response) {
  72. String gateName = request.mandatoryParam(PARAM_GATE_NAME);
  73. String metric = request.mandatoryParam(PARAM_METRIC);
  74. String operator = request.mandatoryParam(PARAM_OPERATOR);
  75. String error = request.mandatoryParam(PARAM_ERROR);
  76. try (DbSession dbSession = dbClient.openSession(false)) {
  77. QualityGateDto qualityGate = wsSupport.getByName(dbSession, gateName);
  78. wsSupport.checkCanLimitedEdit(dbSession, qualityGate);
  79. QualityGateConditionDto condition = qualityGateConditionsUpdater.createCondition(dbSession, qualityGate, metric, operator, error);
  80. CreateConditionResponse.Builder createConditionResponse = CreateConditionResponse.newBuilder()
  81. .setId(condition.getUuid())
  82. .setMetric(condition.getMetricKey())
  83. .setError(condition.getErrorThreshold())
  84. .setOp(condition.getOperator());
  85. writeProtobuf(createConditionResponse.build(), request, response);
  86. dbSession.commit();
  87. }
  88. }
  89. }