Skip to content

Commit 10fd8a5

Browse files
committed
Added support for Pipeline triggers (#341).
1 parent 065dd38 commit 10fd8a5

File tree

6 files changed

+349
-15
lines changed

6 files changed

+349
-15
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.ws.rs.core.MultivaluedHashMap;
1010

1111
import org.gitlab4j.api.models.AccessLevel;
12+
import org.gitlab4j.api.models.Variable;
1213
import org.gitlab4j.api.utils.ISO8601;
1314

1415
/**
@@ -194,4 +195,26 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro
194195
this.param(name, stringValue);
195196
return (this);
196197
}
198+
199+
/**
200+
* Fluent method for adding a List<Variable> type query and form parameters to a get(), post(), or put() call.
201+
*
202+
* @param variables the List of Variable to add
203+
* @return this GitLabAPiForm instance
204+
*/
205+
public GitLabApiForm withParam(List<Variable> variables) {
206+
207+
if (variables == null || variables.isEmpty()) {
208+
return (this);
209+
}
210+
211+
variables.forEach(v -> {
212+
String value = v.getValue();
213+
if (value != null) {
214+
this.param("variables[" + v.getKey() + "]", value);
215+
}
216+
});
217+
218+
return (this);
219+
}
197220
}

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

Lines changed: 205 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
import org.gitlab4j.api.models.Pipeline;
1212
import org.gitlab4j.api.models.PipelineSchedule;
1313
import org.gitlab4j.api.models.PipelineStatus;
14+
import org.gitlab4j.api.models.Trigger;
1415
import org.gitlab4j.api.models.Variable;
1516

1617
/**
17-
* This class provides an entry point to all the GitLab API pipeline calls.
18+
* <p>This class provides an entry point to all the GitLab API pipeline related calls.
19+
* For more information on the Pipeline APIs see:</p>
20+
*
21+
* <a href="https://docs.gitlab.com/ee/api/pipelines.html">Pipelines API</a>
22+
* <a href="https://docs.gitlab.com/ee/api/pipeline_schedules.html">Pipeline Schedules API</a>
23+
* <a href="https://docs.gitlab.com/ee/api/pipeline_triggers.html">Pipeline Triggers API</a>
1824
*/
1925
public class PipelineApi extends AbstractApi implements Constants {
2026

@@ -314,7 +320,7 @@ public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath) throw
314320
*
315321
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
316322
* @param page the page to get
317-
* @param perPage the number of ProjectHook instances per page
323+
* @param perPage the number of PipelineSchedule instances per page
318324
* @return a list of project pipeline_schedules for the specified project in the specified page range
319325
* @throws GitLabApiException if any exception occurs
320326
*/
@@ -329,7 +335,7 @@ public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath, int p
329335
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
330336
*
331337
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
332-
* @param itemsPerPage the number of Project instances that will be fetched per page
338+
* @param itemsPerPage the number of PipelineSchedule instances that will be fetched per page
333339
* @return a Pager of project pipeline_schedules for the specified project
334340
* @throws GitLabApiException if any exception occurs
335341
*/
@@ -356,8 +362,8 @@ public Stream<PipelineSchedule> getPipelineSchedulesStream(Object projectIdOrPat
356362
* <pre><code>GET /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
357363
*
358364
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
359-
* @param pipelineScheduleId the ID of the hook to get
360-
* @return the project hook for the specified project ID/hook ID pair
365+
* @param pipelineScheduleId the ID of the pipeline schedule to get
366+
* @return the project PipelineSchedule
361367
* @throws GitLabApiException if any exception occurs
362368
*/
363369
public PipelineSchedule getPipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
@@ -372,7 +378,7 @@ public PipelineSchedule getPipelineSchedule(Object projectIdOrPath, Integer pipe
372378
*
373379
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
374380
* @param pipelineScheduleId the ID of the hook to get
375-
* @return the project hook for the specified project ID/hook ID pair as an Optional instance
381+
* @return the project PipelineSchedule as an Optional instance
376382
*/
377383
public Optional<PipelineSchedule> getOptionalPipelineSchedule (Object projectIdOrPath, Integer pipelineScheduleId) {
378384
try {
@@ -517,4 +523,197 @@ public void deletePipelineScheduleVariable(Object projectIdOrPath, Integer pipel
517523
delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath),
518524
"pipeline_schedules", pipelineScheduleId, "variables", key);
519525
}
526+
527+
/**
528+
* Get a list of the project pipeline triggers for the specified project.
529+
*
530+
* <pre><code>GET /projects/:id/triggers</code></pre>
531+
*
532+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
533+
* @return a list of pipeline triggers for the specified project
534+
* @throws GitLabApiException if any exception occurs
535+
*/
536+
public List<Trigger> getPipelineTriggers(Object projectIdOrPath) throws GitLabApiException {
537+
return (getPipelineTriggers(projectIdOrPath, getDefaultPerPage()).all());
538+
}
539+
540+
/**
541+
* Get list of project pipeline triggers in the specified page range.
542+
*
543+
* <pre><code>GET /projects/:id/triggers</code></pre>
544+
*
545+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
546+
* @param page the page to get
547+
* @param perPage the number of Trigger instances per page
548+
* @return a list of project pipeline triggers for the specified project in the specified page range
549+
* @throws GitLabApiException if any exception occurs
550+
*/
551+
public List<Trigger> getPipelineTriggerss(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
552+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", getProjectIdOrPath(projectIdOrPath), "triggers");
553+
return (response.readEntity(new GenericType<List<Trigger>>() {}));
554+
}
555+
556+
/**
557+
* Get Pager of project pipeline triggers.
558+
*
559+
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
560+
*
561+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
562+
* @param itemsPerPage the number of Project instances that will be fetched per page
563+
* @return a Pager of project pipeline triggers for the specified project
564+
* @throws GitLabApiException if any exception occurs
565+
*/
566+
public Pager<Trigger> getPipelineTriggers(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
567+
return (new Pager<Trigger>(this, Trigger.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "triggers"));
568+
}
569+
570+
/**
571+
* Get a Stream of the project pipeline triggers for the specified project.
572+
*
573+
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
574+
*
575+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
576+
* @return a Stream of project pipeline triggers for the specified project
577+
* @throws GitLabApiException if any exception occurs
578+
*/
579+
public Stream<Trigger> getPipelineTriggersStream(Object projectIdOrPath) throws GitLabApiException {
580+
return (getPipelineTriggers(projectIdOrPath, getDefaultPerPage()).stream());
581+
}
582+
583+
/**
584+
* Get a specific pipeline schedule for project.
585+
*
586+
* <pre><code>GET /projects/:id/triggers/:trigger_id</code></pre>
587+
*
588+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
589+
* @param triggerId the ID of the trigger to get
590+
* @return the project pipeline trigger
591+
* @throws GitLabApiException if any exception occurs
592+
*/
593+
public Trigger getPipelineTrigger(Object projectIdOrPath, Integer triggerId) throws GitLabApiException {
594+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "triggers", triggerId);
595+
return (response.readEntity(Trigger.class));
596+
}
597+
598+
/**
599+
* Get a specific pipeline trigger for project as an Optional instance.
600+
*
601+
* <pre><code>GET /projects/:id/triggers/:trigger_id</code></pre>
602+
*
603+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
604+
* @param triggerId the ID of the trigger to get
605+
* @return the project pipeline trigger as an Optional instance
606+
*/
607+
public Optional<Trigger> getOptionalPipelineTrigger(Object projectIdOrPath, Integer triggerId) {
608+
try {
609+
return (Optional.ofNullable(getPipelineTrigger(projectIdOrPath, triggerId)));
610+
} catch (GitLabApiException glae) {
611+
return (GitLabApi.createOptionalFromException(glae));
612+
}
613+
}
614+
615+
/**
616+
* Create a pipeline trigger for a project.
617+
*
618+
* <pre><code>POST /projects/:id/triggers</code></pre>
619+
*
620+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
621+
* @param description the trigger description
622+
* @return the created Trigger instance
623+
* @throws GitLabApiException if any exception occurs
624+
*/
625+
public Trigger createPipelineTrigger(Object projectIdOrPath, String description) throws GitLabApiException {
626+
GitLabApiForm formData = new GitLabApiForm().withParam("description", description, true);
627+
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "triggers");
628+
return (response.readEntity(Trigger.class));
629+
}
630+
631+
/**
632+
* Updates a pipeline trigger for project.
633+
*
634+
* <pre><code>PUT /projects/:id/triggers/:trigger_id</code></pre>
635+
*
636+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
637+
* @param triggerId the trigger ID to update
638+
* @param description the new trigger description
639+
* @return the updated Trigger instance
640+
* @throws GitLabApiException if any exception occurs
641+
*/
642+
public Trigger updatePipelineTrigger(Object projectIdOrPath, Integer triggerId, String description) throws GitLabApiException {
643+
GitLabApiForm formData = new GitLabApiForm().withParam("description", description, false);
644+
Response response = put(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "triggers", triggerId);
645+
return (response.readEntity(Trigger.class));
646+
}
647+
648+
/**
649+
* Deletes a pipeline trigger from the project.
650+
*
651+
* <pre><code>DELETE /projects/:id/triggers/:trigger_id</code></pre>
652+
*
653+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
654+
* @param triggerId the project trigger ID to delete
655+
* @throws GitLabApiException if any exception occurs
656+
*/
657+
public void deletePipelineTrigger(Object projectIdOrPath, Integer triggerId) throws GitLabApiException {
658+
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "triggers", triggerId);
659+
}
660+
661+
/**
662+
* Take ownership of a pipeline trigger for project.
663+
*
664+
* <pre><code>PUT /projects/:id/triggers/:trigger_id/take_ownership</code></pre>
665+
*
666+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
667+
* @param triggerId the trigger ID to take opwnership of
668+
* @return the updated Trigger instance
669+
* @throws GitLabApiException if any exception occurs
670+
*/
671+
public Trigger takeOwnewrshipOfPipelineTrigger(Object projectIdOrPath, Integer triggerId) throws GitLabApiException {
672+
Response response = put(Response.Status.OK, null,
673+
"projects", getProjectIdOrPath(projectIdOrPath), "triggers", triggerId, "take_ownership");
674+
return (response.readEntity(Trigger.class));
675+
}
676+
677+
/**
678+
* Trigger a pipeline for a project.
679+
*
680+
* <pre><code>POST /projects/:id/trigger/pipeline</code></pre>
681+
*
682+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
683+
* @param trigger the Trigger instance holding the trigger token
684+
* @param ref the ref that the pipeline is to be triggered for
685+
* @param variables a List of variables to be passed with the trigger
686+
* @return a Pipeline instance holding information on the triggered pipeline
687+
* @throws GitLabApiException if any exception occurs
688+
*/
689+
public Pipeline triggerPipeline(Object projectIdOrPath, Trigger trigger, String ref, List<Variable> variables) throws GitLabApiException {
690+
691+
if (trigger == null) {
692+
throw new GitLabApiException("trigger cannot be null");
693+
}
694+
695+
return (triggerPipeline(projectIdOrPath, trigger.getToken(), ref, variables));
696+
}
697+
698+
/**
699+
* Trigger a pipeline for a project.
700+
*
701+
* <pre><code>POST /projects/:id/trigger/pipeline</code></pre>
702+
*
703+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
704+
* @param token the trigger token
705+
* @param ref the ref that the pipeline is to be triggered for
706+
* @param variables a List of variables to be passed with the trigger
707+
* @return a Pipeline instance holding information on the triggered pipeline
708+
* @throws GitLabApiException if any exception occurs
709+
*/
710+
public Pipeline triggerPipeline(Object projectIdOrPath, String token, String ref, List<Variable> variables) throws GitLabApiException {
711+
GitLabApiForm formData = new GitLabApiForm()
712+
.withParam("token", token, true)
713+
.withParam("ref", ref, true)
714+
.withParam(variables);
715+
Response response = post(Response.Status.CREATED, formData,
716+
"projects", getProjectIdOrPath(projectIdOrPath), "trigger", "pipeline");
717+
return (response.readEntity(Pipeline.class));
718+
}
520719
}

src/main/java/org/gitlab4j/api/models/Pipeline.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class Pipeline {
2121
private Date committed_at;
2222
private String coverage;
2323
private Integer duration;
24+
private String webUrl;
25+
private DetailedStatus detailedStatus;
2426

2527
public Integer getId() {
2628
return id;
@@ -142,6 +144,22 @@ public void setDuration(Integer duration) {
142144
this.duration = duration;
143145
}
144146

147+
public String getWebUrl() {
148+
return webUrl;
149+
}
150+
151+
public void setWebUrl(String webUrl) {
152+
this.webUrl = webUrl;
153+
}
154+
155+
public DetailedStatus getDetailedStatus() {
156+
return detailedStatus;
157+
}
158+
159+
public void setDetailedStatus(DetailedStatus detailedStatus) {
160+
this.detailedStatus = detailedStatus;
161+
}
162+
145163
@Override
146164
public String toString() {
147165
return (JacksonJson.toJsonString(this));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.gitlab4j.api.models.SystemHook;
8181
import org.gitlab4j.api.models.Tag;
8282
import org.gitlab4j.api.models.TreeItem;
83+
import org.gitlab4j.api.models.Trigger;
8384
import org.gitlab4j.api.models.User;
8485
import org.gitlab4j.api.models.Variable;
8586
import org.gitlab4j.api.services.JiraService;
@@ -434,6 +435,12 @@ public void testTree() throws Exception {
434435
assertTrue(compareJson(tree, "tree.json"));
435436
}
436437

438+
@Test
439+
public void testTrigger() throws Exception {
440+
Trigger trigger = unmarshalResource(Trigger.class, "trigger.json");
441+
assertTrue(compareJson(trigger, "trigger.json"));
442+
}
443+
437444
@Test
438445
public void testUser() throws Exception {
439446
User user = unmarshalResource(User.class, "user.json");

0 commit comments

Comments
 (0)