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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 java.util.Set;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.server.exceptions.BadRequestException;
  28. import org.sonar.server.qualitygate.Condition;
  29. import org.sonar.server.ws.RemovedWebServiceHandler;
  30. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.CONTROLLER_QUALITY_GATES;
  31. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR;
  32. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
  33. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
  34. public class QualityGatesWs implements WebService {
  35. private static final int CONDITION_MAX_LENGTH = 64;
  36. private final QualityGatesWsAction[] actions;
  37. public QualityGatesWs(QualityGatesWsAction... actions) {
  38. this.actions = actions;
  39. }
  40. @Override
  41. public void define(Context context) {
  42. NewController controller = context.createController(CONTROLLER_QUALITY_GATES)
  43. .setSince("4.3")
  44. .setDescription("Manage quality gates, including conditions and project association.");
  45. for (QualityGatesWsAction action : actions) {
  46. action.define(controller);
  47. }
  48. // unset_default is no more authorized
  49. controller.createAction("unset_default")
  50. .setDescription("This webservice is no more available : a default quality gate is mandatory.")
  51. .setSince("4.3")
  52. .setDeprecatedSince("7.0")
  53. .setPost(true)
  54. .setHandler(RemovedWebServiceHandler.INSTANCE)
  55. .setResponseExample(RemovedWebServiceHandler.INSTANCE.getResponseExample())
  56. .setChangelog(
  57. new Change("7.0", "Unset a quality gate is no more authorized")
  58. );
  59. controller.done();
  60. }
  61. static void addConditionParams(NewAction action) {
  62. action
  63. .createParam(PARAM_METRIC)
  64. .setDescription("Condition metric.<br/>" +
  65. " Only metric of the following types are allowed:" +
  66. "<ul>" +
  67. "<li>INT</li>" +
  68. "<li>MILLISEC</li>" +
  69. "<li>RATING</li>" +
  70. "<li>WORK_DUR</li>" +
  71. "<li>FLOAT</li>" +
  72. "<li>PERCENT</li>" +
  73. "<li>LEVEL</li>" +
  74. "")
  75. .setRequired(true)
  76. .setExampleValue("blocker_violations");
  77. action.createParam(PARAM_OPERATOR)
  78. .setDescription("Condition operator:<br/>" +
  79. "<ul>" +
  80. "<li>LT = is lower than</li>" +
  81. "<li>GT = is greater than</li>" +
  82. "</ui>")
  83. .setExampleValue(Condition.Operator.GREATER_THAN.getDbValue())
  84. .setPossibleValues(getPossibleOperators());
  85. action.createParam(PARAM_ERROR)
  86. .setMaximumLength(CONDITION_MAX_LENGTH)
  87. .setDescription("Condition error threshold")
  88. .setRequired(true)
  89. .setExampleValue("10");
  90. }
  91. static Long parseId(Request request, String paramName) {
  92. try {
  93. return Long.valueOf(request.mandatoryParam(paramName));
  94. } catch (NumberFormatException badFormat) {
  95. throw BadRequestException.create(paramName + " must be a valid long value");
  96. }
  97. }
  98. private static Set<String> getPossibleOperators() {
  99. return Stream.of(Condition.Operator.values())
  100. .map(Condition.Operator::getDbValue)
  101. .collect(Collectors.toSet());
  102. }
  103. }