From 14ef9b762ddf22e7ef3d00be65ba6e57ec3875bb Mon Sep 17 00:00:00 2001 From: Chen Chenglong Date: Tue, 11 Jul 2017 11:32:01 +0800 Subject: [PATCH] Add Job API (#45) --- src/main/java/org/gitlab4j/api/Constants.java | 17 ++ src/main/java/org/gitlab4j/api/GitLabApi.java | 15 +- src/main/java/org/gitlab4j/api/JobApi.java | 211 ++++++++++++++++++ .../gitlab4j/api/models/ArtifactsFile.java | 29 +++ .../java/org/gitlab4j/api/models/Job.java | 147 ++++++++++++ .../org/gitlab4j/api/models/JobStatus.java | 36 +++ .../java/org/gitlab4j/api/models/Runner.java | 56 +++++ 7 files changed, 509 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/gitlab4j/api/JobApi.java create mode 100644 src/main/java/org/gitlab4j/api/models/ArtifactsFile.java create mode 100644 src/main/java/org/gitlab4j/api/models/Job.java create mode 100644 src/main/java/org/gitlab4j/api/models/JobStatus.java create mode 100644 src/main/java/org/gitlab4j/api/models/Runner.java diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index e8cf701a..81635514 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -121,4 +121,21 @@ public interface Constants { return (name().toLowerCase()); } } + + /** Enum to use for specifying the scope when calling getJobs(). */ + public enum JobScope { + + CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(JobScope.class); + + @JsonCreator + public static JobScope forValue(String value) { return enumHelper.forValue(value); } + + @JsonValue + public String toValue() { return (name().toLowerCase()); } + + @Override + public String toString() { return (name().toLowerCase()); } + } } diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index 79213fe1..2c835c53 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -37,6 +37,7 @@ public class GitLabApi { private ServicesApi servicesApi; private SessionApi sessoinApi; private UserApi userApi; + private JobApi jobApi; private NotesApi notesApi; private Session session; @@ -187,6 +188,7 @@ public class GitLabApi { sessoinApi = new SessionApi(this); userApi = new UserApi(this); repositoryFileApi = new RepositoryFileApi(this); + jobApi = new JobApi(this); notesApi = new NotesApi(this); } @@ -238,10 +240,10 @@ public class GitLabApi { GitLabApiClient getApiClient() { return (apiClient); } - + /** * Get the version info for the GitLab server using the GitLab Version API. - * + * * @return the version info for the GitLab server * @throws GitLabApiException if any exception occurs */ @@ -371,6 +373,14 @@ public class GitLabApi { return (userApi); } + /** + * Gets the JobApi instance owned by this GitLabApi instance. The JobApi is used + * to perform all jobs related API calls. + * + * @return the JobsApi instance owned by this GitLabApi instance + */ + public JobApi getJobApi() { return (jobApi); } + /** * Gets the NotesApi instance owned by this GitLabApi instance. The NotesApi is used * to perform all notes related API calls. @@ -380,4 +390,5 @@ public class GitLabApi { public NotesApi getNotesApi() { return (notesApi); } + } diff --git a/src/main/java/org/gitlab4j/api/JobApi.java b/src/main/java/org/gitlab4j/api/JobApi.java new file mode 100644 index 00000000..594fa000 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/JobApi.java @@ -0,0 +1,211 @@ +package org.gitlab4j.api; + +import org.gitlab4j.api.models.Job; + +import java.util.List; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +/** + * This class provides an entry point to all the GitLab API job calls. + */ +public class JobApi extends AbstractApi implements Constants { + + public JobApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Get a list of jobs in a project. + * + * GET /projects/:id/jobs + * + * @param projectId the project ID to get the list of jobs for + * @return a list containing the jobs for the specified project ID + * @throws GitLabApiException if any exception occurs during execution + */ + public List getJobsFromProjectId(int projectId) throws GitLabApiException { + Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, + "jobs"); + return (response.readEntity(new GenericType>() { + })); + } + + /** + * Get a list of jobs in a project in the specified page range. + * + * GET /projects/:id/jobs + * + * @param projectId the project ID to get the list of jobs for + * @param page the page to get + * @param perPage the number of Job instances per page + * @return a list containing the jobs for the specified project ID in the specified page range + * @throws GitLabApiException if any exception occurs during execution + */ + public List getJobsFromProjectId(int projectId, int page, int perPage) + throws GitLabApiException { + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", + projectId, "jobs"); + return (response.readEntity(new GenericType>() { + })); + } + + /** + * Get a Pager of jobs in a project. + * + * GET /projects/:id/jobs + * + * @param projectId the project ID to get the list of jobs for + * @param itemsPerPage the number of Job instances that will be fetched per page + * @return a Pager containing the jobs for the specified project ID + * @throws GitLabApiException if any exception occurs during execution + */ + public Pager getJobsFromProjectId(int projectId, int itemsPerPage) + throws GitLabApiException { + return (new Pager(this, Job.class, itemsPerPage, null, "projects", projectId, "jobs")); + } + + /** + * Get a list of jobs in a project. + * + * GET /projects/:id/jobs + * + * @param projectId the project ID to get the list of jobs for + * @param scope the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL + * @return a list containing the jobs for the specified project ID + * @throws GitLabApiException if any exception occurs during execution + */ + public List getJobsFromProjectId(int projectId, JobScope scope) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("scope", scope) + .withParam(PER_PAGE_PARAM, getDefaultPerPage()); + Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, + "jobs"); + return (response.readEntity(new GenericType>() { + })); + } + + /** + * Get a list of jobs in a pipeline. + * + * GET /projects/:id/pipelines/:pipeline_id/jobs + * + * @param projectId the project ID to get the list of pipeline for + * @param pipelineId the pipeline ID to get the list of jobs for + * @return a list containing the jobs for the specified project ID and pipeline ID + * @throws GitLabApiException if any exception occurs during execution + */ + public List getJobsFromPipelineId(int projectId, int pipelineId) + throws GitLabApiException { + Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, + "pipelines", pipelineId, "jobs"); + return (response.readEntity(new GenericType>() { + })); + } + + /** + * Get a list of jobs in a pipeline. + * + * GET /projects/:id/pipelines/:pipeline_id/jobs + * + * @param projectId the project ID to get the list of pipeline for + * @param pipelineId the pipeline ID to get the list of jobs for + * @param scope the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL + * @return a list containing the jobs for the specified project ID and pipeline ID + * @throws GitLabApiException if any exception occurs during execution + */ + public List getJobsFromPipelineId(int projectId, int pipelineId, JobScope scope) + throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("scope", scope) + .withParam(PER_PAGE_PARAM, getDefaultPerPage()); + Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, + "pipelines", pipelineId, "jobs"); + return (response.readEntity(new GenericType>() { + })); + } + + /** + * Get single job in a project. + * + * GET /projects/:id/jobs/:job_id + * + * @param projectId the project ID to get the specified job for + * @param jobId the job ID to get + * @return a single job for the specified project ID + * @throws GitLabApiException if any exception occurs during execution + */ + public Job getJob(int projectId, int jobId) throws GitLabApiException { + Response response = get(Response.Status.OK, null, "projects", projectId, "jobs", jobId); + return (response.readEntity(Job.class)); + } + + /** + * Cancel specified job in a project. + * + * POST /projects/:id/jobs/:job_id/cancel + * + * @param projectId the project ID to cancel speficied job + * @param jobId the ID to cancel job + * @return job instance which just canceled + * @throws GitLabApiException if any exception occurs during execution + */ + public Job cancleJob(int projectId, int jobId) throws GitLabApiException { + GitLabApiForm formData = null; + Response response = post(Response.Status.CREATED, formData, "projects", projectId, "jobs", + jobId, "cancel"); + return (response.readEntity(Job.class)); + } + + /** + * Retry specified job in a project. + * + * POST /projects/:id/jobs/:job_id/retry + * + * @param projectId the project ID to retry speficied job + * @param jobId the ID to retry job + * @return job instance which just retried + * @throws GitLabApiException if any exception occurs during execution + */ + public Job retryJob(int projectId, int jobId) throws GitLabApiException { + GitLabApiForm formData = null; + Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, + "retry"); + return (response.readEntity(Job.class)); + } + + /** + * Erase specified job in a project. + * + * POST /projects/:id/jobs/:job_id/erase + * + * @param projectId the project ID to erase speficied job + * @param jobId the ID to erase job + * @return job instance which just erased + * @throws GitLabApiException if any exception occurs during execution + */ + public Job eraseJob(int projectId, int jobId) throws GitLabApiException { + GitLabApiForm formData = null; + Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, + "erase"); + return (response.readEntity(Job.class)); + } + + /** + * Play specified job in a project. + * + * POST /projects/:id/jobs/:job_id/play + * + * @param projectId the project ID to play speficied job + * @param jobId the ID to play job + * @return job instance which just played + * @throws GitLabApiException if any exception occurs during execution + */ + public Job playJob(int projectId, int jobId) throws GitLabApiException { + GitLabApiForm formData = null; + Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, + "play"); + return (response.readEntity(Job.class)); + } +} diff --git a/src/main/java/org/gitlab4j/api/models/ArtifactsFile.java b/src/main/java/org/gitlab4j/api/models/ArtifactsFile.java new file mode 100644 index 00000000..d168ad2e --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/ArtifactsFile.java @@ -0,0 +1,29 @@ +package org.gitlab4j.api.models; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class ArtifactsFile { + + private String filename; + private Integer size; + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public Integer getSize() { + return size; + } + + public void setSize(Integer size) { + this.size = size; + } +} diff --git a/src/main/java/org/gitlab4j/api/models/Job.java b/src/main/java/org/gitlab4j/api/models/Job.java new file mode 100644 index 00000000..f5d168bd --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/Job.java @@ -0,0 +1,147 @@ +package org.gitlab4j.api.models; + +import java.util.Date; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class Job { + + private Integer id; + private Commit commit; + private String coverage; + private Date created_at; + private Date finished_at; + private String name; + private Pipeline pipeline; + private String ref; + private Runner runner; + private User user; + private Date started_at; + private ArtifactsFile artifacts_file; + private Boolean tag; + private String stage; + private JobStatus status; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Commit getCommit() { + return commit; + } + + public void setCommit(Commit commit) { + this.commit = commit; + } + + public Date getCreated_at() { + return created_at; + } + + public void setCreated_at(Date created_at) { + this.created_at = created_at; + } + + public Date getFinished_at() { + return finished_at; + } + + public void setFinished_at(Date finished_at) { + this.finished_at = finished_at; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Pipeline getPipeline() { + return pipeline; + } + + public void setPipeline(Pipeline pipeline) { + this.pipeline = pipeline; + } + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Date getStarted_at() { + return started_at; + } + + public void setStarted_at(Date started_at) { + this.started_at = started_at; + } + + public Boolean getTag() { + return tag; + } + + public void setTag(Boolean tag) { + this.tag = tag; + } + + public String getStage() { + return stage; + } + + public void setStage(String stage) { + this.stage = stage; + } + + public JobStatus getStatus() { + return status; + } + + public void setStatus(JobStatus status) { + this.status = status; + } + + public String getCoverage() { + return coverage; + } + + public void setCoverage(String coverage) { + this.coverage = coverage; + } + + public ArtifactsFile getArtifacts_file() { + return artifacts_file; + } + + public void setArtifacts_file(ArtifactsFile artifacts_file) { + this.artifacts_file = artifacts_file; + } + + public Runner getRunner() { + return runner; + } + + public void setRunner(Runner runner) { + this.runner = runner; + } +} diff --git a/src/main/java/org/gitlab4j/api/models/JobStatus.java b/src/main/java/org/gitlab4j/api/models/JobStatus.java new file mode 100644 index 00000000..9f625f0d --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/JobStatus.java @@ -0,0 +1,36 @@ +package org.gitlab4j.api.models; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Enum for the various Job status values. + */ +public enum JobStatus { + + RUNNING, PENDING, SUCCESS, FAILED, CANCELED, SKIPPED; + + private static Map valuesMap = new HashMap<>(6); + static { + for (JobStatus status : JobStatus.values()) + valuesMap.put(status.toValue(), status); + } + + @JsonCreator + public static JobStatus forValue(String value) { + return valuesMap.get(value); + } + + @JsonValue + public String toValue() { + return (name().toLowerCase()); + } + + @Override + public String toString() { + return (name().toLowerCase()); + } +} \ No newline at end of file diff --git a/src/main/java/org/gitlab4j/api/models/Runner.java b/src/main/java/org/gitlab4j/api/models/Runner.java new file mode 100644 index 00000000..ce889c5d --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/Runner.java @@ -0,0 +1,56 @@ +package org.gitlab4j.api.models; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class Runner { + + private Integer id; + private String description; + private Boolean active; + private Boolean is_shared; + private String name; + + 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 Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public Boolean getIs_shared() { + return is_shared; + } + + public void setIs_shared(Boolean is_shared) { + this.is_shared = is_shared; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} -- GitLab