Skip to content

Commit bfadd89

Browse files
committed
Fix #621 : Add deployment API
1 parent b442f13 commit bfadd89

File tree

3 files changed

+403
-0
lines changed

3 files changed

+403
-0
lines changed

src/main/java/org/gitlab4j/api/Constants.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,28 @@ public String toString() {
243243
}
244244
}
245245

246+
/** Enum to use for ordering the results of getDeployments. */
247+
public static enum DeploymentOrderBy {
248+
249+
ID, IID, CREATED_AT, UPDATED_AT, REF;
250+
private static JacksonJsonEnumHelper<DeploymentOrderBy> enumHelper = new JacksonJsonEnumHelper<>(DeploymentOrderBy.class);
251+
252+
@JsonCreator
253+
public static DeploymentOrderBy forValue(String value) {
254+
return enumHelper.forValue(value);
255+
}
256+
257+
@JsonValue
258+
public String toValue() {
259+
return (enumHelper.toString(this));
260+
}
261+
262+
@Override
263+
public String toString() {
264+
return (enumHelper.toString(this));
265+
}
266+
}
267+
246268
/** Enum to use for specifying the scope when calling getPipelines(). */
247269
public enum PipelineScope {
248270

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Stream;
6+
7+
import javax.ws.rs.core.Response;
8+
9+
import org.gitlab4j.api.models.Deployment;
10+
import org.gitlab4j.api.models.DeploymentFilter;
11+
import org.gitlab4j.api.models.MergeRequest;
12+
13+
/**
14+
* This class implements the client side API for the GitLab Deployments API calls.
15+
* See https://docs.gitlab.com/ee/api/deployments.html
16+
*
17+
*/
18+
public class DeploymentsApi extends AbstractApi {
19+
20+
public DeploymentsApi(GitLabApi gitLabApi) {
21+
super(gitLabApi);
22+
}
23+
24+
/**
25+
* Get a list of deployments for the specified project.
26+
*
27+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
28+
*
29+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
30+
* @return a list of Deployments
31+
* @throws GitLabApiException if any exception occurs
32+
*/
33+
public List<Deployment> getProjectDeployments(Object projectIdOrPath) throws GitLabApiException {
34+
return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).all());
35+
}
36+
37+
/**
38+
* Get a Pager of all deployments for the specified project.
39+
*
40+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
41+
*
42+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
43+
* @param itemsPerPage the number of Deployments instances that will be fetched per page
44+
* @return a Pager of Deployment
45+
* @throws GitLabApiException if any exception occurs
46+
*/
47+
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
48+
return (getProjectDeployments(projectIdOrPath, null, itemsPerPage));
49+
}
50+
51+
/**
52+
* Get a Pager of all deployments for the specified project.
53+
*
54+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
55+
*
56+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
57+
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
58+
* @return a Pager of Deployment
59+
* @throws GitLabApiException if any exception occurs
60+
*/
61+
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException {
62+
return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage()));
63+
}
64+
65+
/**
66+
* Get a Pager of all deployments for the specified project.
67+
*
68+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
69+
*
70+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
71+
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
72+
* @param itemsPerPage the number of Deployments instances that will be fetched per page
73+
* @return a Pager of Deployment
74+
* @throws GitLabApiException if any exception occurs
75+
*/
76+
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter, int itemsPerPage) throws GitLabApiException {
77+
GitLabApiForm formData = (filter != null ? filter.getQueryParams() : new GitLabApiForm());
78+
return (new Pager<Deployment>(this, Deployment.class, itemsPerPage, formData.asMap(),
79+
"projects", getProjectIdOrPath(projectIdOrPath), "deployments"));
80+
}
81+
82+
/**
83+
* Get a Stream of all deployments for the specified project.
84+
*
85+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
86+
*
87+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
88+
* @return a list of Deployment
89+
* @throws GitLabApiException if any exception occurs
90+
*/
91+
public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath) throws GitLabApiException {
92+
return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).stream());
93+
}
94+
95+
/**
96+
* Get a Stream of all deployments for the specified project.
97+
*
98+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
99+
*
100+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
101+
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
102+
* @return a list of Deployment
103+
* @throws GitLabApiException if any exception occurs
104+
*/
105+
public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException {
106+
return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage()).stream());
107+
}
108+
109+
/**
110+
* Get a specific deployment.
111+
*
112+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre>
113+
*
114+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
115+
* @param deploymentId the ID of a project's deployment
116+
* @return the specified Deployment instance
117+
* @throws GitLabApiException if any exception occurs
118+
*/
119+
public Deployment getDeployment(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
120+
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
121+
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
122+
return (response.readEntity(Deployment.class));
123+
}
124+
125+
/**
126+
* Get a specific deployment as an Optional instance.
127+
*
128+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre>
129+
*
130+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
131+
* @param deploymentId the ID of a project's deployment
132+
* @return the specified Deployment as an Optional instance
133+
*/
134+
public Optional<Deployment> getOptionalDeployment(Object projectIdOrPath, Integer deploymentId) {
135+
try {
136+
return (Optional.ofNullable(getDeployment(projectIdOrPath, deploymentId)));
137+
} catch (GitLabApiException glae) {
138+
return (GitLabApi.createOptionalFromException(glae));
139+
}
140+
}
141+
142+
/**
143+
* Creates a new deployment for a project.
144+
*
145+
* <pre><code>GitLab Endpoint: POST /projects/:id/deployments</code></pre>
146+
*
147+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
148+
* @param environment The name of the environment to create the deployment for, required
149+
* @param sha The SHA of the commit that is deployed, required
150+
* @param ref The name of the branch or tag that is deployed, required
151+
* @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false). , required
152+
* @param status The status to filter deployments by, optional
153+
* @return a Deployment instance with info on the added deployment
154+
* @throws GitLabApiException if any exception occurs
155+
*/
156+
public Deployment addDeployment(Object projectIdOrPath, String environment, String sha, String ref, Boolean tag, DeploymentStatus status) throws GitLabApiException {
157+
158+
GitLabApiForm formData = new GitLabApiForm()
159+
.withParam("environment", environment, true)
160+
.withParam("sha", sha, true)
161+
.withParam("ref", ref, true)
162+
.withParam("tag", tag, true)
163+
.withParam("status", status);
164+
Response response = post(Response.Status.CREATED, formData,
165+
"projects", getProjectIdOrPath(projectIdOrPath), "deployments");
166+
return (response.readEntity(Deployment.class));
167+
}
168+
169+
/**
170+
* Updates an existing project deploy key.
171+
*
172+
* <pre><code>GitLab Endpoint: PUT /projects/:id/deployments/:key_id</code></pre>
173+
*
174+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
175+
* @param deploymentId The ID of the deployment to update, required
176+
* @param status The new status of the deployment, optional
177+
* @return an updated Deployment instance
178+
* @throws GitLabApiException if any exception occurs
179+
*/
180+
public Deployment updateDeployment(Object projectIdOrPath, Integer deploymentId, DeploymentStatus status) throws GitLabApiException {
181+
182+
if (deploymentId == null) {
183+
throw new RuntimeException("deploymentId cannot be null");
184+
}
185+
186+
final Deployment key = new Deployment();
187+
key.setStatus(status);
188+
final Response response = put(Response.Status.OK, key,
189+
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
190+
191+
return (response.readEntity(Deployment.class));
192+
}
193+
194+
/**
195+
* Get a list of Merge Requests shipped with a given deployment.
196+
*
197+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
198+
*
199+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
200+
* @param deploymentId The ID of the deployment to update, required
201+
* @return a list containing the MergeRequest instances shipped with a given deployment
202+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
203+
*/
204+
public List<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
205+
return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).all());
206+
}
207+
208+
/**
209+
* Get a Pager of Merge Requests shipped with a given deployment.
210+
*
211+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
212+
*
213+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
214+
* @param deploymentId The ID of the deployment to update, required
215+
* @param itemsPerPage the number of Commit instances that will be fetched per page
216+
* @return a Pager containing the MergeRequest instances shipped with a given deployment
217+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
218+
*/
219+
public Pager<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId, int itemsPerPage) throws GitLabApiException {
220+
return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, null,
221+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", deploymentId, "merge_requests"));
222+
}
223+
224+
/**
225+
* Get a Stream of Merge Requests shipped with a given deployment.
226+
*
227+
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
228+
*
229+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
230+
* @param deploymentId The ID of the deployment to update, required
231+
* @return a Stream containing the MergeRequest instances shipped with a given deployment
232+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
233+
*/
234+
public Stream<MergeRequest> getMergeRequestsStream(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
235+
return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).stream());
236+
}
237+
238+
239+
}

0 commit comments

Comments
 (0)