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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 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.organization.OrganizationDto;
  31. import org.sonar.db.qualitygate.QualityGateDto;
  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. public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport, QualityGateFinder finder) {
  43. this.dbClient = dbClient;
  44. this.wsSupport = wsSupport;
  45. this.finder = finder;
  46. }
  47. @Override
  48. public void define(WebService.NewController controller) {
  49. WebService.NewAction action = controller.createAction("list")
  50. .setDescription("Get a list of quality gates")
  51. .setSince("4.3")
  52. .setResponseExample(Resources.getResource(this.getClass(), "list-example.json"))
  53. .setChangelog(
  54. new Change("7.0", "'isDefault' field is added on quality gate"),
  55. new Change("7.0", "'default' field on root level is deprecated"),
  56. new Change("7.0", "'isBuiltIn' field is added in the response"),
  57. new Change("7.0", "'actions' fields are added in the response"))
  58. .setHandler(this);
  59. wsSupport.createOrganizationParam(action);
  60. }
  61. @Override
  62. public void handle(Request request, Response response) {
  63. try (DbSession dbSession = dbClient.openSession(false)) {
  64. OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
  65. QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
  66. Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization);
  67. writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response);
  68. }
  69. }
  70. private ListWsResponse buildResponse(OrganizationDto organization, Collection<QualityGateDto> qualityGates, @Nullable QualityGateDto defaultQualityGate) {
  71. Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId();
  72. ListWsResponse.Builder builder = ListWsResponse.newBuilder()
  73. .setActions(ListWsResponse.RootActions.newBuilder().setCreate(wsSupport.isQualityGateAdmin(organization)))
  74. .addAllQualitygates(qualityGates.stream()
  75. .map(qualityGate -> QualityGate.newBuilder()
  76. .setId(qualityGate.getId())
  77. .setName(qualityGate.getName())
  78. .setIsDefault(qualityGate.getId().equals(defaultId))
  79. .setIsBuiltIn(qualityGate.isBuiltIn())
  80. .setActions(wsSupport.getActions(organization, qualityGate, defaultQualityGate))
  81. .build())
  82. .collect(toList()));
  83. ofNullable(defaultId).ifPresent(builder::setDefault);
  84. return builder.build();
  85. }
  86. }