Skip to content

Update with pipeline schedule support #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions src/main/java/org/gitlab4j/api/PipelineApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.gitlab4j.api;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.PipelineStatus;

/**
Expand Down Expand Up @@ -271,4 +273,167 @@ public Pipeline cancelPipelineJobs(Object projectIdOrPath, int pipelineId) throw
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipelines", pipelineId, "cancel");
return (response.readEntity(Pipeline.class));
}

/**
* Get a list of the project pipeline_schedules for the specified project.
*
* <pre><code>GET /projects/:id/pipeline_schedules</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @return a list of pipeline schedules for the specified project
* @throws GitLabApiException if any exception occurs
*/
public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath) throws GitLabApiException {
return (getPipelineSchedules(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get list of project pipeline schedules in the specified page range.
*
* <pre><code>GET /projects/:id/pipeline_schedules</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param page the page to get
* @param perPage the number of ProjectHook instances per page
* @return a list of project pipeline_schedules for the specified project in the specified page range
* @throws GitLabApiException if any exception occurs
*/
public List<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules");
return (response.readEntity(new GenericType<List<PipelineSchedule>>() {}));
}
/**
* Get Pager of project pipeline schedule.
*
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a Pager of project pipeline_schedules for the specified project
* @throws GitLabApiException if any exception occurs
*/
public Pager<PipelineSchedule> getPipelineSchedules(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<PipelineSchedule>(this, PipelineSchedule.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules"));
}

/**
* Get a Stream of the project pipeline schedule for the specified project.
*
* <pre><code>GET /projects/:id/pipeline_schedule</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @return a Stream of project pipeline schedules for the specified project
* @throws GitLabApiException if any exception occurs
*/
public Stream<PipelineSchedule> getPipelineSchedulesStream(Object projectIdOrPath) throws GitLabApiException {
return (getPipelineSchedules(projectIdOrPath, getDefaultPerPage()).stream());
}


/**
* Get a specific pipeline schedule for project.
*
* <pre><code>GET /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineScheduleId the ID of the hook to get
* @return the project hook for the specified project ID/hook ID pair
* @throws GitLabApiException if any exception occurs
*/
public PipelineSchedule getPipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId);
return (response.readEntity(PipelineSchedule.class));
}

/**
* Get a specific pipeline schedule for project as an Optional instance.
*
* <pre><code>GET /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineScheduleId the ID of the hook to get
* @return the project hook for the specified project ID/hook ID pair as an Optional instance
*/
public Optional<PipelineSchedule> getOptionalPipelineSchedule (Object projectIdOrPath, Integer pipelineScheduleId) {
try {
return (Optional.ofNullable(getPipelineSchedule(projectIdOrPath, pipelineScheduleId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}

/**
* create a pipeline schedule for a project.
*
* <pre><code>POST /projects/:id/pipeline_schedules</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineSchedule a PipelineSchedule instance to create
* @return the added PipelineSchedule instance
* @throws GitLabApiException if any exception occurs
*/
public PipelineSchedule createPipelineSchedule(Object projectIdOrPath, PipelineSchedule pipelineSchedule)
throws GitLabApiException {

GitLabApiForm formData = new GitLabApiForm()
.withParam("description", pipelineSchedule.getDescription(), true)
.withParam("ref", pipelineSchedule.getRef(), true)
.withParam("cron", pipelineSchedule.getCron(), true)
.withParam("cron_timezone", pipelineSchedule.getCronTimezone(), false)
.withParam("active", pipelineSchedule.getActive(), false);
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules");
return (response.readEntity(PipelineSchedule.class));
}

/**
* Deletes a pipeline schedule from the project.
*
* <pre><code>DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineScheduleId the project schedule ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deletePipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId);
}

/**
* Modifies a pipeline schedule for project.
*
* <pre><code>PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineSchedule the pipelineSchedule instance that contains the pipelineSchedule info to modify
* @return the modified project schedule
* @throws GitLabApiException if any exception occurs
*/
public PipelineSchedule modifyPipelineSchedule(Object projectIdOrPath,PipelineSchedule pipelineSchedule) throws GitLabApiException {

GitLabApiForm formData = new GitLabApiForm()
.withParam("description", pipelineSchedule.getDescription(), false)
.withParam("ref", pipelineSchedule.getRef(), false)
.withParam("cron", pipelineSchedule.getCron(), false)
.withParam("cron_timezone", pipelineSchedule.getCronTimezone(), false)
.withParam("active", pipelineSchedule.getActive(), false);

Response response = put(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineSchedule.getId());
return (response.readEntity(PipelineSchedule.class));
}

/**
* Update the owner of the pipeline schedule of a project.
*
* <pre><code>POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/take_ownership</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param pipelineScheduleId the pipelineSchedule instance id that ownership has to be taken of
* @return the modified project schedule
* @throws GitLabApiException if any exception occurs
*/
public PipelineSchedule takeOwnershipPipelineSchedule(Object projectIdOrPath, Integer pipelineScheduleId) throws GitLabApiException {

Response response = post(Response.Status.OK, "", "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId, "take_ownership");
return (response.readEntity(PipelineSchedule.class));
}
}
119 changes: 119 additions & 0 deletions src/main/java/org/gitlab4j/api/models/PipelineSchedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.utils.JacksonJson;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PipelineSchedule {

private Integer id;
private String description;
private String ref;
private String cron;
private String cronTimezone;
private Date nextRunAt;
private Date createdAt;
private Date updatedAt;
private Boolean active;
private Pipeline lastPipeline;
private Owner owner;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getRef() {
return ref;
}

public void setRef(String ref) {
this.ref = ref;
}

public String getCron() {
return cron;
}

public void setCron(String cron) {
this.cron = cron;
}

public String getCronTimezone() {
return cronTimezone;
}

public void setCronTimezone(String cronTimezone) {
this.cronTimezone = cronTimezone;
}

public Date getNextRunAt() {
return nextRunAt;
}

public void setNextRunAt(Date nextRunAt) {
this.nextRunAt = nextRunAt;
}

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

public Boolean getActive() {
return active;
}

public void setActive(Boolean active) {
this.active = active;
}

public Pipeline getLastPipeline() {
return lastPipeline;
}

public void setLastPipeline(Pipeline lastPipeline) {
this.lastPipeline = lastPipeline;
}

public Owner getOwner() {
return owner;
}

public void setOwner(Owner owner) {
this.owner = owner;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}

7 changes: 7 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.gitlab4j.api.models.NotificationSettings;
import org.gitlab4j.api.models.OauthTokenResponse;
import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
Expand Down Expand Up @@ -215,6 +216,12 @@ public void testPipeline() throws Exception {
assertTrue(compareJson(pipeline, "pipeline.json"));
}

@Test
public void testPipelineSchedule() throws Exception {
PipelineSchedule pipelineSchedule = unmarshalResource(PipelineSchedule.class, "pipeline-schedule.json");
assertTrue(compareJson(pipelineSchedule, "pipeline-schedule.json"));
}

@Test
public void testJob() throws Exception {
Job job = unmarshalResource(Job.class, "job.json");
Expand Down
Loading