Skip to content

Commit 437b75a

Browse files
committed
Mods to support addCommitStatus() (#274).
1 parent f93b15d commit 437b75a

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,50 @@ public Pager<CommitStatus> getCommitStatuses(Object projectIdOrPath, String sha,
363363
"projects", this.getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "statuses"));
364364
}
365365

366+
/**
367+
* <p>Add or update the build status of a commit. The following fluent methods are available on the
368+
* CommitStatus instance for setting up the status:</p>
369+
* <pre><code>
370+
* withCoverage(Float)
371+
* withDescription(String)
372+
* withName(String)
373+
* withRef(String)
374+
* withTargetUrl(String)
375+
* </code></pre>
376+
* <pre><code>
377+
* POST /projects/:id/statuses/:sha
378+
* </code></pre>
379+
*
380+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance (required)
381+
* @param sha a commit SHA (required)
382+
* @param state the state of the status. Can be one of the following: PENDING, RUNNING, SUCCESS, FAILED, CANCELED (required)
383+
* @param status the CommitSatus instance hoilding the optional parms: ref, name, target_url, description, and coverage
384+
* @return a CommitStatus instance with the updated info
385+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
386+
*/
387+
public CommitStatus addCommitStatus(Object projectIdOrPath, String sha, CommitBuildState state, CommitStatus status) throws GitLabApiException {
388+
389+
if (projectIdOrPath == null) {
390+
throw new RuntimeException("projectIdOrPath cannot be null");
391+
}
392+
393+
if (sha == null || sha.trim().isEmpty()) {
394+
throw new RuntimeException("sha cannot be null");
395+
}
396+
397+
GitLabApiForm formData = new GitLabApiForm().withParam("state", state, true);
398+
if (status != null) {
399+
formData.withParam("ref", status.getRef())
400+
.withParam("name", status.getName())
401+
.withParam("target_url", status.getTargetUrl())
402+
.withParam("description", status.getDescription())
403+
.withParam("coverage", status.getCoverage());
404+
}
405+
406+
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "statuses", sha);
407+
return (response.readEntity(CommitStatus.class));
408+
}
409+
366410
/**
367411
* Get the list of diffs of a commit in a project.
368412
*

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,29 @@ public String toString() {
501501
return (value);
502502
}
503503
}
504+
505+
/**
506+
* Enum for the various Commit build status values.
507+
*/
508+
public enum CommitBuildState {
509+
510+
PENDING, RUNNING, SUCCESS, FAILED, CANCELED;
511+
512+
private static JacksonJsonEnumHelper<CommitBuildState> enumHelper = new JacksonJsonEnumHelper<>(CommitBuildState.class);
513+
514+
@JsonCreator
515+
public static CommitBuildState forValue(String value) {
516+
return enumHelper.forValue(value);
517+
}
518+
519+
@JsonValue
520+
public String toValue() {
521+
return (enumHelper.toString(this));
522+
}
523+
524+
@Override
525+
public String toString() {
526+
return (enumHelper.toString(this));
527+
}
528+
}
504529
}

src/main/java/org/gitlab4j/api/models/CommitStatus.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CommitStatus {
1313

1414
private Boolean allowFailure;
1515
private Author author;
16+
private Float coverage;
1617
private Date createdAt;
1718
private String description;
1819
private Date finishedAt;
@@ -40,6 +41,14 @@ public void setAuthor(Author author) {
4041
this.author = author;
4142
}
4243

44+
public Float getCoverage() {
45+
return coverage;
46+
}
47+
48+
public void setCoverage(Float coverage) {
49+
this.coverage = coverage;
50+
}
51+
4352
public Date getCreatedAt() {
4453
return createdAt;
4554
}
@@ -119,4 +128,29 @@ public String getTargetUrl() {
119128
public void setTargetUrl(String targetUrl) {
120129
this.targetUrl = targetUrl;
121130
}
131+
132+
public CommitStatus withCoverage(Float coverage) {
133+
this.coverage = coverage;
134+
return this;
135+
}
136+
137+
public CommitStatus withDescription(String description) {
138+
this.description = description;
139+
return this;
140+
}
141+
142+
public CommitStatus withName(String name) {
143+
this.name = name;
144+
return this;
145+
}
146+
147+
public CommitStatus withRef(String ref) {
148+
this.ref = ref;
149+
return this;
150+
}
151+
152+
public CommitStatus withTargetUrl(String targetUrl) {
153+
this.targetUrl = targetUrl;
154+
return this;
155+
}
122156
}

0 commit comments

Comments
 (0)