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.

ListAction.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 com.google.common.io.Resources;
  22. import java.util.Collection;
  23. import javax.annotation.Nullable;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.Response;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.qualitygate.QualityGateDto;
  31. import org.sonar.server.qualitygate.QualityGateCaycChecker;
  32. import org.sonar.server.qualitygate.QualityGateFinder;
  33. import org.sonarqube.ws.Qualitygates.ListWsResponse;
  34. import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
  35. import static java.util.Optional.ofNullable;
  36. import static org.sonar.core.util.stream.MoreCollectors.toList;
  37. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  38. public class ListAction implements QualityGatesWsAction {
  39. private final DbClient dbClient;
  40. private final QualityGatesWsSupport wsSupport;
  41. private final QualityGateFinder finder;
  42. private final QualityGateCaycChecker qualityGateCaycChecker;
  43. public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport, QualityGateFinder finder, QualityGateCaycChecker qualityGateCaycChecker) {
  44. this.dbClient = dbClient;
  45. this.wsSupport = wsSupport;
  46. this.finder = finder;
  47. this.qualityGateCaycChecker = qualityGateCaycChecker;
  48. }
  49. @Override
  50. public void define(WebService.NewController controller) {
  51. controller.createAction("list")
  52. .setDescription("Get a list of quality gates")
  53. .setSince("4.3")
  54. .setResponseExample(Resources.getResource(this.getClass(), "list-example.json"))
  55. .setChangelog(
  56. new Change("9.9", "'caycStatus' field is added on quality gate"),
  57. new Change("8.4", "Field 'id' in the response is deprecated. Format changes from integer to string."),
  58. new Change("7.0", "'isDefault' field is added on quality gate"),
  59. new Change("7.0", "'default' field on root level is deprecated"),
  60. new Change("7.0", "'isBuiltIn' field is added in the response"),
  61. new Change("7.0", "'actions' fields are added in the response"))
  62. .setHandler(this);
  63. }
  64. @Override
  65. public void handle(Request request, Response response) {
  66. try (DbSession dbSession = dbClient.openSession(false)) {
  67. QualityGateDto defaultQualityGate = finder.getDefault(dbSession);
  68. Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
  69. writeProtobuf(buildResponse(dbSession, qualityGates, defaultQualityGate), request, response);
  70. }
  71. }
  72. private ListWsResponse buildResponse(DbSession dbSession, Collection<QualityGateDto> qualityGates, @Nullable QualityGateDto defaultQualityGate) {
  73. String defaultUuid = defaultQualityGate == null ? null : defaultQualityGate.getUuid();
  74. ListWsResponse.Builder builder = ListWsResponse.newBuilder()
  75. .setActions(ListWsResponse.RootActions.newBuilder().setCreate(wsSupport.isQualityGateAdmin()))
  76. .addAllQualitygates(qualityGates.stream()
  77. .map(qualityGate -> QualityGate.newBuilder()
  78. .setId(qualityGate.getUuid())
  79. .setName(qualityGate.getName())
  80. .setIsDefault(qualityGate.getUuid().equals(defaultUuid))
  81. .setIsBuiltIn(qualityGate.isBuiltIn())
  82. .setCaycStatus(qualityGateCaycChecker.checkCaycCompliant(dbSession, qualityGate.getUuid()).toString())
  83. .setActions(wsSupport.getActions(dbSession, qualityGate, defaultQualityGate))
  84. .build())
  85. .collect(toList()));
  86. ofNullable(defaultUuid).ifPresent(builder::setDefault);
  87. return builder.build();
  88. }
  89. }