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

Added the ability to get commit statuses (#274).

parent 01a610a1
package org.gitlab4j.api;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitAction;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.CommitRef;
import org.gitlab4j.api.models.CommitRef.RefType;
import org.gitlab4j.api.models.CommitStatus;
import org.gitlab4j.api.models.CommitStatusFilter;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.utils.ISO8601;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
* This class implements the client side API for the GitLab commits calls.
*/
......@@ -287,6 +291,80 @@ public class CommitsApi extends AbstractApi {
return (response.readEntity(new GenericType<List<CommitRef>>(){}));
}
/**
* Get a list of repository commit statuses that meet the provided filter.
*
* GET /projects/:id/repository/commits/:sha/statuses
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha the commit SHA
* @param filter the commit statuses file, contains ref, stage, name, all
* @return a List containing the commit statuses for the specified project and sha that meet the provided filter
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha, CommitStatusFilter filter) throws GitLabApiException {
return (getCommitStatuses(projectIdOrPath, sha, filter, 1, getDefaultPerPage()));
}
/**
* Get a list of repository commit statuses that meet the provided filter.
*
* GET /projects/:id/repository/commits/:sha/statuses
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha the commit SHA
* @param filter the commit statuses file, contains ref, stage, name, all
* @param page the page to get
* @param perPage the number of commits statuses per page
* @return a List containing the commit statuses for the specified project and sha that meet the provided filter
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha,
CommitStatusFilter filter, int page, int perPage) throws GitLabApiException {
if (projectIdOrPath == null) {
throw new RuntimeException("projectIdOrPath cannot be null");
}
if (sha == null || sha.trim().isEmpty()) {
throw new RuntimeException("sha cannot be null");
}
MultivaluedMap<String, String> queryParams = (filter != null ?
filter.getQueryParams(page, perPage).asMap() : getPageQueryParams(page, perPage));
Response response = get(Response.Status.OK, queryParams,
"projects", this.getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "statuses");
return (response.readEntity(new GenericType<List<CommitStatus>>() {}));
}
/**
* Get a Pager of repository commit statuses that meet the provided filter.
*
* GET /projects/:id/repository/commits/:sha/statuses
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha the commit SHA
* @param filter the commit statuses file, contains ref, stage, name, all
* @param itemsPerPage the number of CommitStatus instances that will be fetched per page
* @return a Pager containing the commit statuses for the specified project and sha that meet the provided filter
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha,
CommitStatusFilter filter, int itemsPerPage) throws GitLabApiException {
if (projectIdOrPath == null) {
throw new RuntimeException("projectIdOrPath cannot be null");
}
if (sha == null || sha.trim().isEmpty()) {
throw new RuntimeException("sha cannot be null");
}
MultivaluedMap<String, String> queryParams = (filter != null ? filter.getQueryParams().asMap() : null);
return (new Pager<CommitStatus>(this, CommitStatus.class, itemsPerPage, queryParams,
"projects", this.getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "statuses"));
}
/**
* Get the list of diffs of a commit in a project.
*
......@@ -399,37 +477,7 @@ public class CommitsApi extends AbstractApi {
*
* POST /projects/:id/repository/commits
*
* @param projectId the ID of the project
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
* @param commitMessage the commit message
* @param startBranch the name of the branch to start the new commit from
* @param authorEmail the commit author's email address
* @param authorName the commit author's name
* @param actions the array of CommitAction to commit as a batch
* @return the create Commit instance
* @throws GitLabApiException if any exception occurs during execution
*/
public Commit createCommit(int projectId, String branch, String commitMessage, String startBranch,
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
CommitPayload payload = new CommitPayload();
payload.setBranch(branch);
payload.setCommitMessage(commitMessage);
payload.setStartBranch(startBranch);
payload.setAuthorEmail(authorEmail);
payload.setAuthorName(authorName);
payload.setActions(actions);
Response response = post(Response.Status.CREATED, payload, "projects", projectId, "repository", "commits");
return (response.readEntity(Commit.class));
}
/**
* Create a commit with multiple files and actions.
*
* POST /projects/:id/repository/commits
*
* @param project the path of the project
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
* @param commitMessage the commit message
* @param startBranch the name of the branch to start the new commit from
......@@ -439,7 +487,7 @@ public class CommitsApi extends AbstractApi {
* @return the create Commit instance
* @throws GitLabApiException if any exception occurs during execution
*/
public Commit createCommit(String project, String branch, String commitMessage, String startBranch,
public Commit createCommit(Object projectIdOrPath, String branch, String commitMessage, String startBranch,
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
CommitPayload payload = new CommitPayload();
......@@ -450,7 +498,7 @@ public class CommitsApi extends AbstractApi {
payload.setAuthorName(authorName);
payload.setActions(actions);
Response response = post(Response.Status.CREATED, payload, "projects", urlEncode(project), "repository", "commits");
Response response = post(Response.Status.CREATED, payload, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
return (response.readEntity(Commit.class));
}
}
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 CommitStatus {
private Boolean allowFailure;
private Author author;
private Date createdAt;
private String description;
private Date finishedAt;
private Integer id;
private String name;
private String ref;
private String sha;
private Date startedAt;
private String status;
private String targetUrl;
public Boolean isAllowFailure() {
return allowFailure;
}
public void setAllowFailure(Boolean allowFailure) {
this.allowFailure = allowFailure;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getFinishedAt() {
return this.finishedAt;
}
public void setFinishedAt(Date finishedAt) {
this.finishedAt = finishedAt;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public String getSha() {
return sha;
}
public void setSha(String sha) {
this.sha = sha;
}
public Date getStartedAt() {
return startedAt;
}
public void setStartedAt(Date startedAt) {
this.startedAt = startedAt;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTargetUrl() {
return targetUrl;
}
public void setTargetUrl(String targetUrl) {
this.targetUrl = targetUrl;
}
}
package org.gitlab4j.api.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.GitLabApiForm;
/**
* This class is used to filter commit status when getting lists of them.
*/
public class CommitStatusFilter {
private String ref;
private String stage;
private String name;
private Boolean all;
public CommitStatusFilter withRef(String ref) {
this.ref = ref;
return this;
}
public CommitStatusFilter withStage(String stage) {
this.stage = stage;
return this;
}
public CommitStatusFilter withName(String name) {
this.name = name;
return this;
}
public CommitStatusFilter withAll(Boolean all) {
this.all = all;
return this;
}
@JsonIgnore
public GitLabApiForm getQueryParams(int page, int perPage) {
return (getQueryParams()
.withParam(Constants.PAGE_PARAM, page)
.withParam(Constants.PER_PAGE_PARAM, perPage));
}
@JsonIgnore
public GitLabApiForm getQueryParams() {
return (new GitLabApiForm()
.withParam("ref", ref)
.withParam("stage", stage)
.withParam("name", name)
.withParam("all", all));
}
}
......@@ -36,6 +36,7 @@ import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.CommitStatus;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.DeployKey;
import org.gitlab4j.api.models.Diff;
......@@ -150,6 +151,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public void testCommitStatus() {
try {
CommitStatus commitStatus = makeFakeApiCall(CommitStatus.class, "commit-status");
assertTrue(compareJson(commitStatus, "commit-status"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testCompareResults() {
......
{
"status" : "pending",
"created_at" : "2016-01-19T08:40:25.934Z",
"started_at" : "2016-01-19T08:40:25.934Z",
"name" : "bundler:audit",
"allow_failure" : true,
"author" : {
"username" : "thedude",
"state" : "active",
"web_url" : "https://gitlab.example.com/thedude",
"avatar_url" : "https://gitlab.example.com/uploads/user/avatar/28/The-Big-Lebowski-400-400.png",
"id" : 28,
"name" : "Jeff Lebowski"
},
"description" : "this is a description",
"sha" : "18f3e63d05582537db6d183d9d557be09e1f90c8",
"target_url" : "https://gitlab.example.com/thedude/gitlab-ce/builds/91",
"finished_at" : "2016-01-19T08:40:25.934Z",
"id" : 91,
"ref" : "master"
}
\ No newline at end of file
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