Skip to content

Commit

Permalink
Added support for deleting and keeping job artifacts (gitlab4j#493).
Browse files Browse the repository at this point in the history
  • Loading branch information
gmessner committed Jan 8, 2020
1 parent a56573e commit df274ee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/main/java/org/gitlab4j/api/JobApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public List<Job> getJobsForPipeline(Object projectIdOrPath, int pipelineId, JobS
* @return a single job for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public Job getJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
public Job getJob(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId);
return (response.readEntity(Job.class));
}
Expand All @@ -186,7 +186,7 @@ public Job getJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
* @param jobId the job ID to get
* @return a single job for the specified project ID as an Optional intance
*/
public Optional<Job> getOptionalJob(Object projectIdOrPath, int jobId) {
public Optional<Job> getOptionalJob(Object projectIdOrPath, Integer jobId) {
try {
return (Optional.ofNullable(getJob(projectIdOrPath, jobId)));
} catch (GitLabApiException glae) {
Expand Down Expand Up @@ -422,7 +422,7 @@ public InputStream downloadSingleArtifactsFile(Object projectIdOrPath, Integer j
* @return a String containing the specified job's trace
* @throws GitLabApiException if any exception occurs during execution
*/
public String getTrace(Object projectIdOrPath, int jobId) throws GitLabApiException {
public String getTrace(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
"projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "trace");
return (response.readEntity(String.class));
Expand All @@ -438,7 +438,7 @@ public String getTrace(Object projectIdOrPath, int jobId) throws GitLabApiExcept
* @return job instance which just canceled
* @throws GitLabApiException if any exception occurs during execution
*/
public Job cancleJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
public Job cancleJob(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "cancel");
return (response.readEntity(Job.class));
Expand All @@ -454,7 +454,7 @@ public Job cancleJob(Object projectIdOrPath, int jobId) throws GitLabApiExceptio
* @return job instance which just retried
* @throws GitLabApiException if any exception occurs during execution
*/
public Job retryJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
public Job retryJob(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "retry");
return (response.readEntity(Job.class));
Expand All @@ -470,7 +470,7 @@ public Job retryJob(Object projectIdOrPath, int jobId) throws GitLabApiException
* @return job instance which just erased
* @throws GitLabApiException if any exception occurs during execution
*/
public Job eraseJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
public Job eraseJob(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "erase");
return (response.readEntity(Job.class));
Expand All @@ -486,9 +486,38 @@ public Job eraseJob(Object projectIdOrPath, int jobId) throws GitLabApiException
* @return job instance which just played
* @throws GitLabApiException if any exception occurs during execution
*/
public Job playJob(Object projectIdOrPath, int jobId) throws GitLabApiException {
public Job playJob(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "play");
return (response.readEntity(Job.class));
}

/**
* Prevents artifacts from being deleted when expiration is set.
*
* <pre><code>GitLab Endpoint: DELETE /projects/:id/jobs/:job_id/artifacts</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to keep artifacts for
* @return the Job instance that was just modified
* @throws GitLabApiException if any exception occurs during execution
*/
public Job keepArtifacts(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = post(Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "keep");
return (response.readEntity(Job.class));
}

/**
* Delete artifacts of a job.
*
* <pre><code>GitLab Endpoint: DELETE /projects/:id/jobs/:job_id/artifacts</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to delete artifacts for
* @throws GitLabApiException if any exception occurs during execution
*/
public void deleteArtifacts(Object projectIdOrPath, Integer jobId) throws GitLabApiException {
delete(Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "artifacts");
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Job {
private JobStatus status;
private String when;
private Boolean manual;
private Boolean allowFailure;
private Float duration;

public Integer getId() {
return id;
Expand Down Expand Up @@ -188,6 +190,22 @@ public void setManual(Boolean manual) {
this.manual = manual;
}

public Boolean getAllowFailure() {
return allowFailure;
}

public void setAllowFailure(Boolean allowFailure) {
this.allowFailure = allowFailure;
}

public Float getDuration() {
return duration;
}

public void setDuration(Float duration) {
this.duration = duration;
}

public Job withId(Integer id) {
this.id = id;
return this;
Expand Down Expand Up @@ -273,6 +291,11 @@ public Job withManual(Boolean manual) {
return this;
}

public Job allowFailureManual(Boolean allowFailure) {
this.allowFailure = allowFailure;
return this;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/org/gitlab4j/api/job.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"status": "failed",
"tag": false,
"web_url": "https://example.com/foo/bar/-/jobs/7",
"allow_failure": false,
"duration": 0.465,
"user": {
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"created_at": "2015-12-21T13:14:24.077Z",
Expand Down

0 comments on commit df274ee

Please sign in to comment.