1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarqube.ws.tester;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.sonarqube.ws.Projects.CreateWsResponse.Project;
import org.sonarqube.ws.client.qualitygates.CreateRequest;
import org.sonarqube.ws.client.qualitygates.DestroyRequest;
import org.sonarqube.ws.client.qualitygates.ListRequest;
import org.sonarqube.ws.client.qualitygates.QualitygatesService;
import org.sonarqube.ws.client.qualitygates.SelectRequest;
import org.sonarqube.ws.client.qualitygates.SetAsDefaultRequest;
import static java.util.Arrays.stream;
import static org.sonarqube.ws.Qualitygates.CreateResponse;
import static org.sonarqube.ws.Qualitygates.ListWsResponse;
public class QGateTester {
private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
private final TesterSession session;
QGateTester(TesterSession session) {
this.session = session;
}
public QualitygatesService service() {
return session.wsClient().qualitygates();
}
void deleteAll() {
List<ListWsResponse.QualityGate> builtInQualityGates = session.wsClient().qualitygates().list(new ListRequest()).getQualitygatesList().stream()
.filter(ListWsResponse.QualityGate::getIsBuiltIn)
.toList();
if (builtInQualityGates.size() == 1) {
session.wsClient().qualitygates().setAsDefault(new SetAsDefaultRequest().setName(builtInQualityGates.get(0).getName()));
}
session.wsClient().qualitygates().list(new ListRequest()).getQualitygatesList().stream()
.filter(qualityGate -> !qualityGate.getIsDefault())
.filter(qualityGate -> !qualityGate.getIsBuiltIn())
.forEach(qualityGate -> session.wsClient().qualitygates().destroy(new DestroyRequest().setName(qualityGate.getName())));
}
@SafeVarargs
public final CreateResponse generate(Consumer<CreateRequest>... populators) {
int id = ID_GENERATOR.getAndIncrement();
CreateRequest request = new CreateRequest()
.setName("QualityGate " + id);
stream(populators).forEach(p -> p.accept(request));
return session.wsClient().qualitygates().create(request);
}
public void associateProject(CreateResponse qualityGate, Project project) {
service().select(new SelectRequest()
.setGateName(qualityGate.getName())
.setProjectKey(project.getKey()));
}
public static class ListResponse {
@SerializedName("default")
private final String defaultQGate;
@SerializedName("qualitygates")
private final List<QGate> qualityGates = new ArrayList<>();
public ListResponse(String defaultQGate) {
this.defaultQGate = defaultQGate;
}
public List<QGate> getQualityGates() {
return qualityGates;
}
public String getDefault() {
return defaultQGate;
}
public static ListResponse parse(String json) {
Gson gson = new Gson();
return gson.fromJson(json, ListResponse.class);
}
public static class QGate {
@SerializedName("id")
private final String id;
@SerializedName("name")
private final String name;
public QGate(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
}
}
|