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.

QGateTester.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.sonarqube.ws.tester;
  21. import com.google.gson.Gson;
  22. import com.google.gson.annotations.SerializedName;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. import java.util.function.Consumer;
  27. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  28. import org.sonarqube.ws.client.qualitygates.CreateRequest;
  29. import org.sonarqube.ws.client.qualitygates.DestroyRequest;
  30. import org.sonarqube.ws.client.qualitygates.ListRequest;
  31. import org.sonarqube.ws.client.qualitygates.QualitygatesService;
  32. import org.sonarqube.ws.client.qualitygates.SelectRequest;
  33. import org.sonarqube.ws.client.qualitygates.SetAsDefaultRequest;
  34. import static java.util.Arrays.stream;
  35. import static org.sonarqube.ws.Qualitygates.CreateResponse;
  36. import static org.sonarqube.ws.Qualitygates.ListWsResponse;
  37. public class QGateTester {
  38. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  39. private final TesterSession session;
  40. QGateTester(TesterSession session) {
  41. this.session = session;
  42. }
  43. public QualitygatesService service() {
  44. return session.wsClient().qualitygates();
  45. }
  46. void deleteAll() {
  47. List<ListWsResponse.QualityGate> builtInQualityGates = session.wsClient().qualitygates().list(new ListRequest()).getQualitygatesList().stream()
  48. .filter(ListWsResponse.QualityGate::getIsBuiltIn)
  49. .toList();
  50. if (builtInQualityGates.size() == 1) {
  51. session.wsClient().qualitygates().setAsDefault(new SetAsDefaultRequest().setName(builtInQualityGates.get(0).getName()));
  52. }
  53. session.wsClient().qualitygates().list(new ListRequest()).getQualitygatesList().stream()
  54. .filter(qualityGate -> !qualityGate.getIsDefault())
  55. .filter(qualityGate -> !qualityGate.getIsBuiltIn())
  56. .forEach(qualityGate -> session.wsClient().qualitygates().destroy(new DestroyRequest().setName(qualityGate.getName())));
  57. }
  58. @SafeVarargs
  59. public final CreateResponse generate(Consumer<CreateRequest>... populators) {
  60. int id = ID_GENERATOR.getAndIncrement();
  61. CreateRequest request = new CreateRequest()
  62. .setName("QualityGate " + id);
  63. stream(populators).forEach(p -> p.accept(request));
  64. return session.wsClient().qualitygates().create(request);
  65. }
  66. public void associateProject(CreateResponse qualityGate, Project project) {
  67. service().select(new SelectRequest()
  68. .setGateName(qualityGate.getName())
  69. .setProjectKey(project.getKey()));
  70. }
  71. public static class ListResponse {
  72. @SerializedName("default")
  73. private final String defaultQGate;
  74. @SerializedName("qualitygates")
  75. private final List<QGate> qualityGates = new ArrayList<>();
  76. public ListResponse(String defaultQGate) {
  77. this.defaultQGate = defaultQGate;
  78. }
  79. public List<QGate> getQualityGates() {
  80. return qualityGates;
  81. }
  82. public String getDefault() {
  83. return defaultQGate;
  84. }
  85. public static ListResponse parse(String json) {
  86. Gson gson = new Gson();
  87. return gson.fromJson(json, ListResponse.class);
  88. }
  89. public static class QGate {
  90. @SerializedName("id")
  91. private final String id;
  92. @SerializedName("name")
  93. private final String name;
  94. public QGate(String id, String name) {
  95. this.id = id;
  96. this.name = name;
  97. }
  98. public String getId() {
  99. return id;
  100. }
  101. public String getName() {
  102. return name;
  103. }
  104. }
  105. }
  106. }