Commit a635acf6 authored by Greg Messner's avatar Greg Messner
Browse files

Finalized Job API support.

parent 280fd2ec
...@@ -13,8 +13,10 @@ import org.gitlab4j.api.models.Version; ...@@ -13,8 +13,10 @@ import org.gitlab4j.api.models.Version;
*/ */
public class GitLabApi { public class GitLabApi {
public static final int DEFAULT_PER_PAGE = 9999; /** GitLab4J default per page. GitLab will ignore anything over 100. */
public static final int DEFAULT_PER_PAGE = 100;
/** Specifies the version of the GitLab API to communicate with. */
public enum ApiVersion { public enum ApiVersion {
V3, V4; V3, V4;
......
package org.gitlab4j.api; package org.gitlab4j.api;
import org.gitlab4j.api.models.Job; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import org.gitlab4j.api.models.Job;
/** /**
* This class provides an entry point to all the GitLab API job calls. * This class provides an entry point to all the GitLab API job calls.
*/ */
...@@ -25,9 +33,8 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -25,9 +33,8 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID * @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public List<Job> getJobsFromProjectId(int projectId) throws GitLabApiException { public List<Job> getJobs(int projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "jobs");
"jobs");
return (response.readEntity(new GenericType<List<Job>>() { return (response.readEntity(new GenericType<List<Job>>() {
})); }));
} }
...@@ -43,10 +50,8 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -43,10 +50,8 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID in the specified page range * @return a list containing the jobs for the specified project ID in the specified page range
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public List<Job> getJobsFromProjectId(int projectId, int page, int perPage) public List<Job> getJobs(int projectId, int page, int perPage) throws GitLabApiException {
throws GitLabApiException { Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "jobs");
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects",
projectId, "jobs");
return (response.readEntity(new GenericType<List<Job>>() { return (response.readEntity(new GenericType<List<Job>>() {
})); }));
} }
...@@ -61,8 +66,7 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -61,8 +66,7 @@ public class JobApi extends AbstractApi implements Constants {
* @return a Pager containing the jobs for the specified project ID * @return a Pager containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Pager<Job> getJobsFromProjectId(int projectId, int itemsPerPage) public Pager<Job> getJobs(int projectId, int itemsPerPage) throws GitLabApiException {
throws GitLabApiException {
return (new Pager<Job>(this, Job.class, itemsPerPage, null, "projects", projectId, "jobs")); return (new Pager<Job>(this, Job.class, itemsPerPage, null, "projects", projectId, "jobs"));
} }
...@@ -76,14 +80,10 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -76,14 +80,10 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID * @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public List<Job> getJobsFromProjectId(int projectId, JobScope scope) throws GitLabApiException { public List<Job> getJobs(int projectId, JobScope scope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() GitLabApiForm formData = new GitLabApiForm().withParam("scope", scope).withParam(PER_PAGE_PARAM, getDefaultPerPage());
.withParam("scope", scope) Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "jobs");
.withParam(PER_PAGE_PARAM, getDefaultPerPage()); return (response.readEntity(new GenericType<List<Job>>() {}));
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId,
"jobs");
return (response.readEntity(new GenericType<List<Job>>() {
}));
} }
/** /**
...@@ -96,12 +96,10 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -96,12 +96,10 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID and pipeline ID * @return a list containing the jobs for the specified project ID and pipeline ID
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public List<Job> getJobsFromPipelineId(int projectId, int pipelineId) public List<Job> getJobsForPipeline(int projectId, int pipelineId) throws GitLabApiException {
throws GitLabApiException { Response response = get(Response.Status.OK, getDefaultPerPageParam(),
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "projects", projectId, "pipelines", pipelineId, "jobs");
"pipelines", pipelineId, "jobs"); return (response.readEntity(new GenericType<List<Job>>() {}));
return (response.readEntity(new GenericType<List<Job>>() {
}));
} }
/** /**
...@@ -115,15 +113,10 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -115,15 +113,10 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID and pipeline ID * @return a list containing the jobs for the specified project ID and pipeline ID
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public List<Job> getJobsFromPipelineId(int projectId, int pipelineId, JobScope scope) public List<Job> getJobsForPipeline(int projectId, int pipelineId, JobScope scope) throws GitLabApiException {
throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm().withParam("scope", scope).withParam(PER_PAGE_PARAM, getDefaultPerPage());
GitLabApiForm formData = new GitLabApiForm() Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "pipelines", pipelineId, "jobs");
.withParam("scope", scope) return (response.readEntity(new GenericType<List<Job>>() {}));
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId,
"pipelines", pipelineId, "jobs");
return (response.readEntity(new GenericType<List<Job>>() {
}));
} }
/** /**
...@@ -141,20 +134,76 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -141,20 +134,76 @@ public class JobApi extends AbstractApi implements Constants {
return (response.readEntity(Job.class)); return (response.readEntity(Job.class));
} }
/**
* Download the artifacts file from the given reference name and job provided the job finished successfully.
* The file will be saved to the specified directory. If the file already exists in the directory it will
* be overwritten.
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId the ID of the project
* @param ref the ref from a repository
* @param jobName the name of the job to download the artifacts for
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the download of the specified artifacts file
* @throws GitLabApiException if any exception occurs
*/
public File downloadArtifactsFile(Integer projectId, String ref, String jobName, File directory) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("job", jobName, true);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "jobs", "artifacts", ref, "download");
try {
if (directory == null)
directory = new File(System.getProperty("java.io.tmpdir"));
String filename = jobName + "-artifacts.zip";
File file = new File(directory, filename);
InputStream in = response.readEntity(InputStream.class);
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return (file);
} catch (IOException ioe) {
throw new GitLabApiException(ioe);
}
}
/**
* Get an InputStream pointing to the artifacts file from the given reference name and job
* provided the job finished successfully. The file will be saved to the specified directory.
* If the file already exists in the directory it will be overwritten.
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId the ID of the project
* @param ref the ref from a repository
* @param jobName the name of the job to download the artifacts for
* @return an InputStream to read the specified artifacts file from
* @throws GitLabApiException if any exception occurs
*/
public InputStream downloadArtifactsFile(Integer projectId, String ref, String jobName) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("job", jobName, true);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "jobs", "artifacts", ref, "download");
return (response.readEntity(InputStream.class));
}
/** /**
* Cancel specified job in a project. * Cancel specified job in a project.
* *
* POST /projects/:id/jobs/:job_id/cancel * POST /projects/:id/jobs/:job_id/cancel
* *
* @param projectId the project ID to cancel speficied job * @param projectId the project ID to cancel specified job
* @param jobId the ID to cancel job * @param jobId the ID to cancel job
* @return job instance which just canceled * @return job instance which just canceled
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Job cancleJob(int projectId, int jobId) throws GitLabApiException { public Job cancleJob(int projectId, int jobId) throws GitLabApiException {
GitLabApiForm formData = null; GitLabApiForm formData = null;
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "jobs", Response response = post(Response.Status.CREATED, formData, "projects", projectId, "jobs", jobId, "cancel");
jobId, "cancel");
return (response.readEntity(Job.class)); return (response.readEntity(Job.class));
} }
...@@ -170,8 +219,7 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -170,8 +219,7 @@ public class JobApi extends AbstractApi implements Constants {
*/ */
public Job retryJob(int projectId, int jobId) throws GitLabApiException { public Job retryJob(int projectId, int jobId) throws GitLabApiException {
GitLabApiForm formData = null; GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, "retry");
"retry");
return (response.readEntity(Job.class)); return (response.readEntity(Job.class));
} }
...@@ -180,15 +228,14 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -180,15 +228,14 @@ public class JobApi extends AbstractApi implements Constants {
* *
* POST /projects/:id/jobs/:job_id/erase * POST /projects/:id/jobs/:job_id/erase
* *
* @param projectId the project ID to erase speficied job * @param projectId the project ID to erase specified job
* @param jobId the ID to erase job * @param jobId the ID to erase job
* @return job instance which just erased * @return job instance which just erased
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Job eraseJob(int projectId, int jobId) throws GitLabApiException { public Job eraseJob(int projectId, int jobId) throws GitLabApiException {
GitLabApiForm formData = null; GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, "erase");
"erase");
return (response.readEntity(Job.class)); return (response.readEntity(Job.class));
} }
...@@ -197,15 +244,14 @@ public class JobApi extends AbstractApi implements Constants { ...@@ -197,15 +244,14 @@ public class JobApi extends AbstractApi implements Constants {
* *
* POST /projects/:id/jobs/:job_id/play * POST /projects/:id/jobs/:job_id/play
* *
* @param projectId the project ID to play speficied job * @param projectId the project ID to play specified job
* @param jobId the ID to play job * @param jobId the ID to play job
* @return job instance which just played * @return job instance which just played
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Job playJob(int projectId, int jobId) throws GitLabApiException { public Job playJob(int projectId, int jobId) throws GitLabApiException {
GitLabApiForm formData = null; GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, "play");
"play");
return (response.readEntity(Job.class)); return (response.readEntity(Job.class));
} }
} }
...@@ -187,7 +187,7 @@ public class RepositoryFileApi extends AbstractApi { ...@@ -187,7 +187,7 @@ public class RepositoryFileApi extends AbstractApi {
* @param projectId the ID of the project * @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file contents for * @param commitOrBranchName the commit or branch name to get the file contents for
* @param filepath the path of the file to get * @param filepath the path of the file to get
* @return a string with the file content for the specified file * @return an InputStream to read the raw file from
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public InputStream getRawFile(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException { public InputStream getRawFile(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException {
......
...@@ -12,15 +12,15 @@ public class Job { ...@@ -12,15 +12,15 @@ public class Job {
private Integer id; private Integer id;
private Commit commit; private Commit commit;
private String coverage; private String coverage;
private Date created_at; private Date createdAt;
private Date finished_at; private Date finishedAt;
private String name; private String name;
private Pipeline pipeline; private Pipeline pipeline;
private String ref; private String ref;
private Runner runner; private Runner runner;
private User user; private User user;
private Date started_at; private Date startedAt;
private ArtifactsFile artifacts_file; private ArtifactsFile artifactsFile;
private Boolean tag; private Boolean tag;
private String stage; private String stage;
private JobStatus status; private JobStatus status;
...@@ -41,20 +41,20 @@ public class Job { ...@@ -41,20 +41,20 @@ public class Job {
this.commit = commit; this.commit = commit;
} }
public Date getCreated_at() { public Date getCreatedAt() {
return created_at; return createdAt;
} }
public void setCreated_at(Date created_at) { public void setCreatedAt(Date createdAt) {
this.created_at = created_at; this.createdAt = createdAt;
} }
public Date getFinished_at() { public Date getFinishedAt() {
return finished_at; return finishedAt;
} }
public void setFinished_at(Date finished_at) { public void setFinishedAt(Date finishedAt) {
this.finished_at = finished_at; this.finishedAt = finishedAt;
} }
public String getName() { public String getName() {
...@@ -89,12 +89,12 @@ public class Job { ...@@ -89,12 +89,12 @@ public class Job {
this.user = user; this.user = user;
} }
public Date getStarted_at() { public Date getStartedAt() {
return started_at; return startedAt;
} }
public void setStarted_at(Date started_at) { public void setStartedAt(Date startedAt) {
this.started_at = started_at; this.startedAt = startedAt;
} }
public Boolean getTag() { public Boolean getTag() {
...@@ -129,12 +129,12 @@ public class Job { ...@@ -129,12 +129,12 @@ public class Job {
this.coverage = coverage; this.coverage = coverage;
} }
public ArtifactsFile getArtifacts_file() { public ArtifactsFile getArtifactsFile() {
return artifacts_file; return artifactsFile;
} }
public void setArtifacts_file(ArtifactsFile artifacts_file) { public void setArtifactsFile(ArtifactsFile artifactsFile) {
this.artifacts_file = artifacts_file; this.artifactsFile = artifactsFile;
} }
public Runner getRunner() { public Runner getRunner() {
......
...@@ -6,6 +6,7 @@ import java.util.List; ...@@ -6,6 +6,7 @@ import java.util.List;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
...@@ -171,29 +172,6 @@ public class PipelineEvent implements Event { ...@@ -171,29 +172,6 @@ public class PipelineEvent implements Event {
public void setArtifactsFile(ArtifactsFile artifactsFile) { public void setArtifactsFile(ArtifactsFile artifactsFile) {
this.artifactsFile = artifactsFile; this.artifactsFile = artifactsFile;
} }
@XmlAccessorType(XmlAccessType.FIELD)
public static class ArtifactsFile {
private String file;
private Long size;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
}
} }
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
......
...@@ -7,6 +7,7 @@ import java.io.InputStreamReader; ...@@ -7,6 +7,7 @@ import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CompareResults; import org.gitlab4j.api.models.CompareResults;
...@@ -14,6 +15,7 @@ import org.gitlab4j.api.models.Diff; ...@@ -14,6 +15,7 @@ import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Event; import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Job;
import org.gitlab4j.api.models.Key; import org.gitlab4j.api.models.Key;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.MergeRequest; import org.gitlab4j.api.models.MergeRequest;
...@@ -146,6 +148,28 @@ public class TestGitLabApiBeans { ...@@ -146,6 +148,28 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testJob() {
try {
Job job = makeFakeApiCall(Job.class, "job");
assertTrue(compareJson(job, "job"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testArtifactsFile() {
try {
ArtifactsFile artifactFile = makeFakeApiCall(ArtifactsFile.class, "artifacts-file");
assertTrue(compareJson(artifactFile, "artifacts-file"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testProjectHook() { public void testProjectHook() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment