Skip to content

Commit b4313d0

Browse files
committed
Added the ability to get commit statuses (#274).
1 parent 01a610a commit b4313d0

File tree

5 files changed

+299
-42
lines changed

5 files changed

+299
-42
lines changed

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 90 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package org.gitlab4j.api;
22

3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLEncoder;
5+
import java.util.Date;
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
import javax.ws.rs.core.Form;
10+
import javax.ws.rs.core.GenericType;
11+
import javax.ws.rs.core.MultivaluedMap;
12+
import javax.ws.rs.core.Response;
13+
314
import org.gitlab4j.api.models.Comment;
415
import org.gitlab4j.api.models.Commit;
516
import org.gitlab4j.api.models.CommitAction;
617
import org.gitlab4j.api.models.CommitPayload;
718
import org.gitlab4j.api.models.CommitRef;
819
import org.gitlab4j.api.models.CommitRef.RefType;
20+
import org.gitlab4j.api.models.CommitStatus;
21+
import org.gitlab4j.api.models.CommitStatusFilter;
922
import org.gitlab4j.api.models.Diff;
1023
import org.gitlab4j.api.utils.ISO8601;
1124

12-
import javax.ws.rs.core.Form;
13-
import javax.ws.rs.core.GenericType;
14-
import javax.ws.rs.core.Response;
15-
import java.io.UnsupportedEncodingException;
16-
import java.net.URLEncoder;
17-
import java.util.Date;
18-
import java.util.List;
19-
import java.util.Optional;
20-
2125
/**
2226
* This class implements the client side API for the GitLab commits calls.
2327
*/
@@ -287,6 +291,80 @@ public List<CommitRef> getCommitRefs(int projectId, String sha, CommitRef.RefTyp
287291
return (response.readEntity(new GenericType<List<CommitRef>>(){}));
288292
}
289293

294+
/**
295+
* Get a list of repository commit statuses that meet the provided filter.
296+
*
297+
* GET /projects/:id/repository/commits/:sha/statuses
298+
*
299+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
300+
* @param sha the commit SHA
301+
* @param filter the commit statuses file, contains ref, stage, name, all
302+
* @return a List containing the commit statuses for the specified project and sha that meet the provided filter
303+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
304+
*/
305+
public List<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha, CommitStatusFilter filter) throws GitLabApiException {
306+
return (getCommitStatuses(projectIdOrPath, sha, filter, 1, getDefaultPerPage()));
307+
}
308+
309+
/**
310+
* Get a list of repository commit statuses that meet the provided filter.
311+
*
312+
* GET /projects/:id/repository/commits/:sha/statuses
313+
*
314+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
315+
* @param sha the commit SHA
316+
* @param filter the commit statuses file, contains ref, stage, name, all
317+
* @param page the page to get
318+
* @param perPage the number of commits statuses per page
319+
* @return a List containing the commit statuses for the specified project and sha that meet the provided filter
320+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
321+
*/
322+
public List<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha,
323+
CommitStatusFilter filter, int page, int perPage) throws GitLabApiException {
324+
325+
if (projectIdOrPath == null) {
326+
throw new RuntimeException("projectIdOrPath cannot be null");
327+
}
328+
329+
if (sha == null || sha.trim().isEmpty()) {
330+
throw new RuntimeException("sha cannot be null");
331+
}
332+
333+
MultivaluedMap<String, String> queryParams = (filter != null ?
334+
filter.getQueryParams(page, perPage).asMap() : getPageQueryParams(page, perPage));
335+
Response response = get(Response.Status.OK, queryParams,
336+
"projects", this.getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "statuses");
337+
return (response.readEntity(new GenericType<List<CommitStatus>>() {}));
338+
}
339+
340+
/**
341+
* Get a Pager of repository commit statuses that meet the provided filter.
342+
*
343+
* GET /projects/:id/repository/commits/:sha/statuses
344+
*
345+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
346+
* @param sha the commit SHA
347+
* @param filter the commit statuses file, contains ref, stage, name, all
348+
* @param itemsPerPage the number of CommitStatus instances that will be fetched per page
349+
* @return a Pager containing the commit statuses for the specified project and sha that meet the provided filter
350+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
351+
*/
352+
public Pager<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha,
353+
CommitStatusFilter filter, int itemsPerPage) throws GitLabApiException {
354+
355+
if (projectIdOrPath == null) {
356+
throw new RuntimeException("projectIdOrPath cannot be null");
357+
}
358+
359+
if (sha == null || sha.trim().isEmpty()) {
360+
throw new RuntimeException("sha cannot be null");
361+
}
362+
363+
MultivaluedMap<String, String> queryParams = (filter != null ? filter.getQueryParams().asMap() : null);
364+
return (new Pager<CommitStatus>(this, CommitStatus.class, itemsPerPage, queryParams,
365+
"projects", this.getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "statuses"));
366+
}
367+
290368
/**
291369
* Get the list of diffs of a commit in a project.
292370
*
@@ -399,37 +477,7 @@ public Comment addComment(int projectId, String sha, String note) throws GitLabA
399477
*
400478
* POST /projects/:id/repository/commits
401479
*
402-
* @param projectId the ID of the project
403-
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
404-
* @param commitMessage the commit message
405-
* @param startBranch the name of the branch to start the new commit from
406-
* @param authorEmail the commit author's email address
407-
* @param authorName the commit author's name
408-
* @param actions the array of CommitAction to commit as a batch
409-
* @return the create Commit instance
410-
* @throws GitLabApiException if any exception occurs during execution
411-
*/
412-
public Commit createCommit(int projectId, String branch, String commitMessage, String startBranch,
413-
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
414-
415-
CommitPayload payload = new CommitPayload();
416-
payload.setBranch(branch);
417-
payload.setCommitMessage(commitMessage);
418-
payload.setStartBranch(startBranch);
419-
payload.setAuthorEmail(authorEmail);
420-
payload.setAuthorName(authorName);
421-
payload.setActions(actions);
422-
423-
Response response = post(Response.Status.CREATED, payload, "projects", projectId, "repository", "commits");
424-
return (response.readEntity(Commit.class));
425-
}
426-
427-
/**
428-
* Create a commit with multiple files and actions.
429-
*
430-
* POST /projects/:id/repository/commits
431-
*
432-
* @param project the path of the project
480+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
433481
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
434482
* @param commitMessage the commit message
435483
* @param startBranch the name of the branch to start the new commit from
@@ -439,7 +487,7 @@ public Commit createCommit(int projectId, String branch, String commitMessage, S
439487
* @return the create Commit instance
440488
* @throws GitLabApiException if any exception occurs during execution
441489
*/
442-
public Commit createCommit(String project, String branch, String commitMessage, String startBranch,
490+
public Commit createCommit(Object projectIdOrPath, String branch, String commitMessage, String startBranch,
443491
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
444492

445493
CommitPayload payload = new CommitPayload();
@@ -450,7 +498,7 @@ public Commit createCommit(String project, String branch, String commitMessage,
450498
payload.setAuthorName(authorName);
451499
payload.setActions(actions);
452500

453-
Response response = post(Response.Status.CREATED, payload, "projects", urlEncode(project), "repository", "commits");
501+
Response response = post(Response.Status.CREATED, payload, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
454502
return (response.readEntity(Commit.class));
455503
}
456504
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
package org.gitlab4j.api.models;
3+
4+
import java.util.Date;
5+
6+
import javax.xml.bind.annotation.XmlAccessType;
7+
import javax.xml.bind.annotation.XmlAccessorType;
8+
import javax.xml.bind.annotation.XmlRootElement;
9+
10+
@XmlRootElement
11+
@XmlAccessorType(XmlAccessType.FIELD)
12+
public class CommitStatus {
13+
14+
private Boolean allowFailure;
15+
private Author author;
16+
private Date createdAt;
17+
private String description;
18+
private Date finishedAt;
19+
private Integer id;
20+
private String name;
21+
private String ref;
22+
private String sha;
23+
private Date startedAt;
24+
private String status;
25+
private String targetUrl;
26+
27+
public Boolean isAllowFailure() {
28+
return allowFailure;
29+
}
30+
31+
public void setAllowFailure(Boolean allowFailure) {
32+
this.allowFailure = allowFailure;
33+
}
34+
35+
public Author getAuthor() {
36+
return author;
37+
}
38+
39+
public void setAuthor(Author author) {
40+
this.author = author;
41+
}
42+
43+
public Date getCreatedAt() {
44+
return createdAt;
45+
}
46+
47+
public void setCreatedAt(Date createdAt) {
48+
this.createdAt = createdAt;
49+
}
50+
51+
public String getDescription() {
52+
return description;
53+
}
54+
55+
public void setDescription(String description) {
56+
this.description = description;
57+
}
58+
59+
public Date getFinishedAt() {
60+
return this.finishedAt;
61+
}
62+
63+
public void setFinishedAt(Date finishedAt) {
64+
this.finishedAt = finishedAt;
65+
}
66+
67+
public Integer getId() {
68+
return id;
69+
}
70+
71+
public void setId(Integer id) {
72+
this.id = id;
73+
}
74+
75+
public String getName() {
76+
return name;
77+
}
78+
79+
public void setName(String name) {
80+
this.name = name;
81+
}
82+
83+
public String getRef() {
84+
return ref;
85+
}
86+
87+
public void setRef(String ref) {
88+
this.ref = ref;
89+
}
90+
91+
public String getSha() {
92+
return sha;
93+
}
94+
95+
public void setSha(String sha) {
96+
this.sha = sha;
97+
}
98+
99+
public Date getStartedAt() {
100+
return startedAt;
101+
}
102+
103+
public void setStartedAt(Date startedAt) {
104+
this.startedAt = startedAt;
105+
}
106+
107+
public String getStatus() {
108+
return status;
109+
}
110+
111+
public void setStatus(String status) {
112+
this.status = status;
113+
}
114+
115+
public String getTargetUrl() {
116+
return targetUrl;
117+
}
118+
119+
public void setTargetUrl(String targetUrl) {
120+
this.targetUrl = targetUrl;
121+
}
122+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.gitlab4j.api.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
5+
import org.gitlab4j.api.Constants;
6+
import org.gitlab4j.api.GitLabApiForm;
7+
8+
/**
9+
* This class is used to filter commit status when getting lists of them.
10+
*/
11+
public class CommitStatusFilter {
12+
13+
private String ref;
14+
private String stage;
15+
private String name;
16+
private Boolean all;
17+
18+
public CommitStatusFilter withRef(String ref) {
19+
this.ref = ref;
20+
return this;
21+
}
22+
23+
public CommitStatusFilter withStage(String stage) {
24+
this.stage = stage;
25+
return this;
26+
}
27+
28+
public CommitStatusFilter withName(String name) {
29+
this.name = name;
30+
return this;
31+
}
32+
33+
34+
public CommitStatusFilter withAll(Boolean all) {
35+
this.all = all;
36+
return this;
37+
}
38+
39+
@JsonIgnore
40+
public GitLabApiForm getQueryParams(int page, int perPage) {
41+
return (getQueryParams()
42+
.withParam(Constants.PAGE_PARAM, page)
43+
.withParam(Constants.PER_PAGE_PARAM, perPage));
44+
}
45+
46+
@JsonIgnore
47+
public GitLabApiForm getQueryParams() {
48+
return (new GitLabApiForm()
49+
.withParam("ref", ref)
50+
.withParam("stage", stage)
51+
.withParam("name", name)
52+
.withParam("all", all));
53+
}
54+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.gitlab4j.api.models.Comment;
3737
import org.gitlab4j.api.models.Commit;
3838
import org.gitlab4j.api.models.CommitPayload;
39+
import org.gitlab4j.api.models.CommitStatus;
3940
import org.gitlab4j.api.models.CompareResults;
4041
import org.gitlab4j.api.models.DeployKey;
4142
import org.gitlab4j.api.models.Diff;
@@ -150,6 +151,17 @@ public void testCommitPayload() {
150151
}
151152
}
152153

154+
@Test
155+
public void testCommitStatus() {
156+
157+
try {
158+
CommitStatus commitStatus = makeFakeApiCall(CommitStatus.class, "commit-status");
159+
assertTrue(compareJson(commitStatus, "commit-status"));
160+
} catch (Exception e) {
161+
e.printStackTrace();
162+
}
163+
}
164+
153165
@Test
154166
public void testCompareResults() {
155167

0 commit comments

Comments
 (0)