Skip to content

Commit 88ec32b

Browse files
lpietgmessner
authored andcommitted
Update with pipeline schedule support (#319)
* pipeline schedule support * add test for json structure of pipeline-schedule
1 parent fce0863 commit 88ec32b

File tree

5 files changed

+429
-0
lines changed

5 files changed

+429
-0
lines changed

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.gitlab4j.api;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.stream.Stream;
56

67
import javax.ws.rs.core.GenericType;
78
import javax.ws.rs.core.Response;
89

910
import org.gitlab4j.api.models.Pipeline;
11+
import org.gitlab4j.api.models.PipelineSchedule;
1012
import org.gitlab4j.api.models.PipelineStatus;
1113

1214
/**
@@ -271,4 +273,167 @@ public Pipeline cancelPipelineJobs(Object projectIdOrPath, int pipelineId) throw
271273
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipelines", pipelineId, "cancel");
272274
return (response.readEntity(Pipeline.class));
273275
}
276+
277+
/**
278+
* Get a list of the project pipeline_schedules for the specified project.
279+
*
280+
* <pre><code>GET /projects/:id/pipeline_schedules</code></pre>
281+
*
282+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
283+
* @return a list of pipeline schedules for the specified project
284+
* @throws GitLabApiException if any exception occurs
285+
*/
286+
public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath) throws GitLabApiException {
287+
return (getPipelineSchedules(projectIdOrPath, getDefaultPerPage()).all());
288+
}
289+
/**
290+
* Get list of project pipeline schedules in the specified page range.
291+
*
292+
* <pre><code>GET /projects/:id/pipeline_schedules</code></pre>
293+
*
294+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
295+
* @param page the page to get
296+
* @param perPage the number of ProjectHook instances per page
297+
* @return a list of project pipeline_schedules for the specified project in the specified page range
298+
* @throws GitLabApiException if any exception occurs
299+
*/
300+
public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
301+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules");
302+
return (response.readEntity(new GenericType<List<PipelineSchedule>>() {}));
303+
}
304+
/**
305+
* Get Pager of project pipeline schedule.
306+
*
307+
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
308+
*
309+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
310+
* @param itemsPerPage the number of Project instances that will be fetched per page
311+
* @return a Pager of project pipeline_schedules for the specified project
312+
* @throws GitLabApiException if any exception occurs
313+
*/
314+
public Pager<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
315+
return (new Pager<PipelineSchedule>(this, PipelineSchedule.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules"));
316+
}
317+
318+
/**
319+
* Get a Stream of the project pipeline schedule for the specified project.
320+
*
321+
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
322+
*
323+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
324+
* @return a Stream of project pipeline schedules for the specified project
325+
* @throws GitLabApiException if any exception occurs
326+
*/
327+
public Stream<PipelineSchedule> getPipelineSchedulesStream(Object projectIdOrPath) throws GitLabApiException {
328+
return (getPipelineSchedules(projectIdOrPath, getDefaultPerPage()).stream());
329+
}
330+
331+
332+
/**
333+
* Get a specific pipeline schedule for project.
334+
*
335+
* <pre><code>GET /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
336+
*
337+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
338+
* @param pipelineScheduleId the ID of the hook to get
339+
* @return the project hook for the specified project ID/hook ID pair
340+
* @throws GitLabApiException if any exception occurs
341+
*/
342+
public PipelineSchedule getPipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
343+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId);
344+
return (response.readEntity(PipelineSchedule.class));
345+
}
346+
347+
/**
348+
* Get a specific pipeline schedule for project as an Optional instance.
349+
*
350+
* <pre><code>GET /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
351+
*
352+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
353+
* @param pipelineScheduleId the ID of the hook to get
354+
* @return the project hook for the specified project ID/hook ID pair as an Optional instance
355+
*/
356+
public Optional<PipelineSchedule> getOptionalPipelineSchedule (Object projectIdOrPath, Integer pipelineScheduleId) {
357+
try {
358+
return (Optional.ofNullable(getPipelineSchedule(projectIdOrPath, pipelineScheduleId)));
359+
} catch (GitLabApiException glae) {
360+
return (GitLabApi.createOptionalFromException(glae));
361+
}
362+
}
363+
364+
/**
365+
* create a pipeline schedule for a project.
366+
*
367+
* <pre><code>POST /projects/:id/pipeline_schedules</code></pre>
368+
*
369+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
370+
* @param pipelineSchedule a PipelineSchedule instance to create
371+
* @return the added PipelineSchedule instance
372+
* @throws GitLabApiException if any exception occurs
373+
*/
374+
public PipelineSchedule createPipelineSchedule(Object projectIdOrPath, PipelineSchedule pipelineSchedule)
375+
throws GitLabApiException {
376+
377+
GitLabApiForm formData = new GitLabApiForm()
378+
.withParam("description", pipelineSchedule.getDescription(), true)
379+
.withParam("ref", pipelineSchedule.getRef(), true)
380+
.withParam("cron", pipelineSchedule.getCron(), true)
381+
.withParam("cron_timezone", pipelineSchedule.getCronTimezone(), false)
382+
.withParam("active", pipelineSchedule.getActive(), false);
383+
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules");
384+
return (response.readEntity(PipelineSchedule.class));
385+
}
386+
387+
/**
388+
* Deletes a pipeline schedule from the project.
389+
*
390+
* <pre><code>DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
391+
*
392+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
393+
* @param pipelineScheduleId the project schedule ID to delete
394+
* @throws GitLabApiException if any exception occurs
395+
*/
396+
public void deletePipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
397+
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
398+
delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId);
399+
}
400+
401+
/**
402+
* Modifies a pipeline schedule for project.
403+
*
404+
* <pre><code>PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
405+
*
406+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
407+
* @param pipelineSchedule the pipelineSchedule instance that contains the pipelineSchedule info to modify
408+
* @return the modified project schedule
409+
* @throws GitLabApiException if any exception occurs
410+
*/
411+
public PipelineSchedule modifyPipelineSchedule(Object projectIdOrPath,PipelineSchedule pipelineSchedule) throws GitLabApiException {
412+
413+
GitLabApiForm formData = new GitLabApiForm()
414+
.withParam("description", pipelineSchedule.getDescription(), false)
415+
.withParam("ref", pipelineSchedule.getRef(), false)
416+
.withParam("cron", pipelineSchedule.getCron(), false)
417+
.withParam("cron_timezone", pipelineSchedule.getCronTimezone(), false)
418+
.withParam("active", pipelineSchedule.getActive(), false);
419+
420+
Response response = put(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineSchedule.getId());
421+
return (response.readEntity(PipelineSchedule.class));
422+
}
423+
424+
/**
425+
* Update the owner of the pipeline schedule of a project.
426+
*
427+
* <pre><code>POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/take_ownership</code></pre>
428+
*
429+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
430+
* @param pipelineScheduleId the pipelineSchedule instance id that ownership has to be taken of
431+
* @return the modified project schedule
432+
* @throws GitLabApiException if any exception occurs
433+
*/
434+
public PipelineSchedule takeOwnershipPipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
435+
436+
Response response = post(Response.Status.OK, "", "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId, "take_ownership");
437+
return (response.readEntity(PipelineSchedule.class));
438+
}
274439
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
import java.util.Date;
9+
10+
@XmlRootElement
11+
@XmlAccessorType(XmlAccessType.FIELD)
12+
public class PipelineSchedule {
13+
14+
private Integer id;
15+
private String description;
16+
private String ref;
17+
private String cron;
18+
private String cronTimezone;
19+
private Date nextRunAt;
20+
private Date createdAt;
21+
private Date updatedAt;
22+
private Boolean active;
23+
private Pipeline lastPipeline;
24+
private Owner owner;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public String getDescription() {
35+
return description;
36+
}
37+
38+
public void setDescription(String description) {
39+
this.description = description;
40+
}
41+
42+
public String getRef() {
43+
return ref;
44+
}
45+
46+
public void setRef(String ref) {
47+
this.ref = ref;
48+
}
49+
50+
public String getCron() {
51+
return cron;
52+
}
53+
54+
public void setCron(String cron) {
55+
this.cron = cron;
56+
}
57+
58+
public String getCronTimezone() {
59+
return cronTimezone;
60+
}
61+
62+
public void setCronTimezone(String cronTimezone) {
63+
this.cronTimezone = cronTimezone;
64+
}
65+
66+
public Date getNextRunAt() {
67+
return nextRunAt;
68+
}
69+
70+
public void setNextRunAt(Date nextRunAt) {
71+
this.nextRunAt = nextRunAt;
72+
}
73+
74+
public Date getCreatedAt() {
75+
return createdAt;
76+
}
77+
78+
public void setCreatedAt(Date createdAt) {
79+
this.createdAt = createdAt;
80+
}
81+
82+
public Date getUpdatedAt() {
83+
return updatedAt;
84+
}
85+
86+
public void setUpdatedAt(Date updatedAt) {
87+
this.updatedAt = updatedAt;
88+
}
89+
90+
public Boolean getActive() {
91+
return active;
92+
}
93+
94+
public void setActive(Boolean active) {
95+
this.active = active;
96+
}
97+
98+
public Pipeline getLastPipeline() {
99+
return lastPipeline;
100+
}
101+
102+
public void setLastPipeline(Pipeline lastPipeline) {
103+
this.lastPipeline = lastPipeline;
104+
}
105+
106+
public Owner getOwner() {
107+
return owner;
108+
}
109+
110+
public void setOwner(Owner owner) {
111+
this.owner = owner;
112+
}
113+
114+
@Override
115+
public String toString() {
116+
return (JacksonJson.toJsonString(this));
117+
}
118+
}
119+

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.gitlab4j.api.models.NotificationSettings;
6262
import org.gitlab4j.api.models.OauthTokenResponse;
6363
import org.gitlab4j.api.models.Pipeline;
64+
import org.gitlab4j.api.models.PipelineSchedule;
6465
import org.gitlab4j.api.models.Project;
6566
import org.gitlab4j.api.models.ProjectHook;
6667
import org.gitlab4j.api.models.ProjectUser;
@@ -215,6 +216,12 @@ public void testPipeline() throws Exception {
215216
assertTrue(compareJson(pipeline, "pipeline.json"));
216217
}
217218

219+
@Test
220+
public void testPipelineSchedule() throws Exception {
221+
PipelineSchedule pipelineSchedule = unmarshalResource(PipelineSchedule.class, "pipeline-schedule.json");
222+
assertTrue(compareJson(pipelineSchedule, "pipeline-schedule.json"));
223+
}
224+
218225
@Test
219226
public void testJob() throws Exception {
220227
Job job = unmarshalResource(Job.class, "job.json");

0 commit comments

Comments
 (0)