forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironmentsApi.java
215 lines (198 loc) · 9.73 KB
/
EnvironmentsApi.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.gitlab4j.api;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Environment;
/**
* This class provides an entry point to all the GitLab API Environments API calls.
* @see <a href="https://docs.gitlab.com/ce/api/environments.html">Environments API</a>
*/
public class EnvironmentsApi extends AbstractApi {
public EnvironmentsApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get all environments for a given project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @return a List of Environment instances
* @throws GitLabApiException if any exception occurs
*/
public List<Environment> getEnvironments(Object projectIdOrPath) throws GitLabApiException {
return (getEnvironments(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a Stream of all environments for a given project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @return a Stream of Environment instances
* @throws GitLabApiException if any exception occurs
*/
public Stream<Environment> getEnvironmentsStream(Object projectIdOrPath) throws GitLabApiException {
return (getEnvironments(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Get a Pager of all environments for a given project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param itemsPerPage the number of Environment instances that will be fetched per page
* @return a Pager of Environment instances
* @throws GitLabApiException if any exception occurs
*/
public Pager<Environment> getEnvironments(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Environment>(this, Environment.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "environments"));
}
/**
* Get a specific environment.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments/:environment_id</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to get
* @return an Environment instance
* @throws GitLabApiException if any exception occurs
*/
public Environment getEnvironment(Object projectIdOrPath, Long environmentId) throws GitLabApiException {
Response response = get(Response.Status.OK, null,
"projects", getProjectIdOrPath(projectIdOrPath), "environments", environmentId);
return (response.readEntity(Environment.class));
}
/**
* Get a specific environment. as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments/:environment_id</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to get
* @return the Environment as an Optional instance
*/
public Optional<Environment> getOptionalEnvironment(Object projectIdOrPath, Long environmentId) {
try {
return (Optional.ofNullable(getEnvironment(projectIdOrPath, environmentId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a new environment with the given name and external_url.
*
* <pre><code>GitLab Endpoint:POST /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param name the name of the environment
* @param externalUrl the place to link to for this environment
* @return the created Environment instance
* @throws GitLabApiException if any exception occurs
* @deprecated use {@link #createEnvironment(Object, String, String, String)} instead
*/
@Deprecated
public Environment createEnvironment(Object projectIdOrPath, String name, String externalUrl) throws GitLabApiException {
return createEnvironment(projectIdOrPath, name, externalUrl, null);
}
/**
* Create a new environment with the given name, external_url and tier.
*
* <pre><code>GitLab Endpoint:POST /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param name the name of the environment
* @param externalUrl the place to link to for this environment
* @param tier the tier of the environment
* @return the created Environment instance
* @throws GitLabApiException if any exception occurs
*/
public Environment createEnvironment(Object projectIdOrPath, String name, String externalUrl, String tier) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("name", name, true).withParam("external_url", externalUrl).withParam("tier", tier);
Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "environments");
return (response.readEntity(Environment.class));
}
/**
* Update an existing environment.
*
* <pre><code>GitLab Endpoint:POST /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to update
* @param name the name of the environment
* @param externalUrl the place to link to for this environment
* @return the created Environment instance
* @throws GitLabApiException if any exception occurs
* @deprecated use {@link #updateEnvironment(Object, Long, String, String, String)} instead
*/
@Deprecated
public Environment updateEnvironment(Object projectIdOrPath, Long environmentId, String name, String externalUrl) throws GitLabApiException {
return updateEnvironment(projectIdOrPath, environmentId, name, externalUrl, null);
}
/**
* Update an existing environment.
*
* <pre><code>GitLab Endpoint:POST /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to update
* @param name the name of the environment
* @param externalUrl the place to link to for this environment
* @param tier the tier of the environment
* @return the created Environment instance
* @throws GitLabApiException if any exception occurs
*/
public Environment updateEnvironment(Object projectIdOrPath, Long environmentId, String name, String externalUrl, String tier) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("name", name).withParam("external_url", externalUrl).withParam("tier", tier);
Response response = putWithFormData(Response.Status.OK, formData, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "environments", environmentId);
return (response.readEntity(Environment.class));
}
/**
* Stop an environment.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/environments/:environment_id/stop</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to stop
* @return the stopped Environment instance
* @throws GitLabApiException if any exception occurs
*/
public Environment stopEnvironment(Object projectIdOrPath, Long environmentId) throws GitLabApiException {
Response response = post(Response.Status.OK, (GitLabApiForm) null,
"projects", getProjectIdOrPath(projectIdOrPath), "environments", environmentId, "stop");
return (response.readEntity(Environment.class));
}
/**
* Delete an environment.
*
* <pre><code>GitLab Endpoint: DELETE /projects/:id/environments/:environment_id</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteEnvironment(Object projectIdOrPath, Long environmentId) throws GitLabApiException {
delete(Response.Status.OK, null,
"projects", getProjectIdOrPath(projectIdOrPath), "environments", environmentId);
}
/**
* Stop an environment.
*
* <pre><code>GitLab Endpoint:POST /projects/:id/environments/:environment_id/stop</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param environmentId the ID of the environment to stop
* @return the Environment instance of the stopped environment
* @throws GitLabApiException if any exception occurs
*/
public Environment createEnvironment(Object projectIdOrPath, Long environmentId) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm();
Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "environments", environmentId, "stop");
return (response.readEntity(Environment.class));
}
}