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.

QualityGatesWs.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 java.util.Set;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.server.qualitygate.Condition;
  26. import static org.sonar.server.qualitygate.QualityGateConditionsUpdater.INVALID_METRIC_KEYS;
  27. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.CONTROLLER_QUALITY_GATES;
  28. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR;
  29. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
  30. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
  31. public class QualityGatesWs implements WebService {
  32. private static final int CONDITION_MAX_LENGTH = 64;
  33. private final QualityGatesWsAction[] actions;
  34. public QualityGatesWs(QualityGatesWsAction... actions) {
  35. this.actions = actions;
  36. }
  37. @Override
  38. public void define(Context context) {
  39. NewController controller = context.createController(CONTROLLER_QUALITY_GATES)
  40. .setSince("4.3")
  41. .setDescription("Manage quality gates, including conditions and project association.");
  42. for (QualityGatesWsAction action : actions) {
  43. action.define(controller);
  44. }
  45. controller.done();
  46. }
  47. static void addConditionParams(NewAction action) {
  48. action
  49. .createParam(PARAM_METRIC)
  50. .setDescription("Condition metric.<br/>" +
  51. " Only metric of the following types are allowed:" +
  52. "<ul>" +
  53. "<li>INT</li>" +
  54. "<li>MILLISEC</li>" +
  55. "<li>RATING</li>" +
  56. "<li>WORK_DUR</li>" +
  57. "<li>FLOAT</li>" +
  58. "<li>PERCENT</li>" +
  59. "<li>LEVEL</li></ul>" +
  60. "Following metrics are forbidden:" +
  61. "<ul>" + getInvalidMetrics() + "</ul>")
  62. .setRequired(true)
  63. .setExampleValue("blocker_violations, vulnerabilities, new_code_smells");
  64. action.createParam(PARAM_OPERATOR)
  65. .setDescription("Condition operator:<br/>" +
  66. "<ul>" +
  67. "<li>LT = is lower than</li>" +
  68. "<li>GT = is greater than</li></ul>")
  69. .setExampleValue(Condition.Operator.GREATER_THAN.getDbValue())
  70. .setPossibleValues(getPossibleOperators());
  71. action.createParam(PARAM_ERROR)
  72. .setMaximumLength(CONDITION_MAX_LENGTH)
  73. .setDescription("Condition error threshold")
  74. .setRequired(true)
  75. .setExampleValue("10");
  76. }
  77. private static String getInvalidMetrics() {
  78. return INVALID_METRIC_KEYS.stream().map(s -> "<li>" + s + "</li>")
  79. .collect(Collectors.joining());
  80. }
  81. private static Set<String> getPossibleOperators() {
  82. return Stream.of(Condition.Operator.values())
  83. .map(Condition.Operator::getDbValue)
  84. .collect(Collectors.toSet());
  85. }
  86. }