Skip to content

Commit 69ce360

Browse files
committed
Added support for batch commit creation (#122).
1 parent 1929d5d commit 69ce360

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ protected Response post(Response.Status expectedStatus, Form formData, Object...
123123
}
124124
}
125125

126+
/**
127+
* Perform an HTTP POST call with the specified payload object and path objects, returning
128+
* a ClientResponse instance with the data returned from the endpoint.
129+
*
130+
* @param expectedStatus the HTTP status that should be returned from the server
131+
* @param payload the object instance that will be serialized to JSON and used as the POST data
132+
* @param pathArgs variable list of arguments used to build the URI
133+
* @return a ClientResponse instance with the data returned from the endpoint
134+
* @throws GitLabApiException if any exception occurs during execution
135+
*/
136+
protected Response post(Response.Status expectedStatus, Object payload, Object... pathArgs) throws GitLabApiException {
137+
try {
138+
return validate(getApiClient().post(payload, pathArgs), expectedStatus);
139+
} catch (Exception e) {
140+
throw handle(e);
141+
}
142+
}
143+
126144
/**
127145
* Perform an HTTP POST call with the specified form data and path objects, returning
128146
* a ClientResponse instance with the data returned from the endpoint.

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.gitlab4j.api.models.Comment;
1313
import org.gitlab4j.api.models.Commit;
14+
import org.gitlab4j.api.models.CommitAction;
15+
import org.gitlab4j.api.models.CommitPayload;
1416
import org.gitlab4j.api.models.Diff;
1517
import org.gitlab4j.api.utils.ISO8601;
1618

@@ -283,4 +285,64 @@ public Comment addComment(int projectId, String sha, String note, String path, I
283285
public Comment addComment(int projectId, String sha, String note) throws GitLabApiException {
284286
return (addComment(projectId, sha, note, null, null, null));
285287
}
288+
289+
/**
290+
* Create a commit with multiple files and actions.
291+
*
292+
* POST /projects/:id/repository/commits
293+
*
294+
* @param projectId the ID of the project
295+
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
296+
* @param commitMessage the commit message
297+
* @param startBranch the name of the branch to start the new commit from
298+
* @param authorEmail the commit author's email address
299+
* @param authorName the commit author's name
300+
* @param actions the array of CommitAction to commit as a batch
301+
* @return the create Commit instance
302+
* @throws GitLabApiException
303+
*/
304+
public Commit createCommit(int projectId, String branch, String commitMessage, String startBranch,
305+
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
306+
307+
CommitPayload payload = new CommitPayload();
308+
payload.setBranch(branch);
309+
payload.setCommitMessage(commitMessage);
310+
payload.setStartBranch(startBranch);
311+
payload.setAuthorEmail(authorEmail);
312+
payload.setAuthorName(authorName);
313+
payload.setActions(actions);
314+
315+
Response response = post(Response.Status.CREATED, payload, "projects", projectId, "repository", "commits");
316+
return (response.readEntity(Commit.class));
317+
}
318+
319+
/**
320+
* Create a commit with multiple files and actions.
321+
*
322+
* POST /projects/:id/repository/commits
323+
*
324+
* @param project the path of the project
325+
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
326+
* @param commitMessage the commit message
327+
* @param startBranch the name of the branch to start the new commit from
328+
* @param authorEmail the commit author's email address
329+
* @param authorName the commit author's name
330+
* @param actions the array of CommitAction to commit as a batch
331+
* @return the create Commit instance
332+
* @throws GitLabApiException
333+
*/
334+
public Commit createCommit(String project, String branch, String commitMessage, String startBranch,
335+
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
336+
337+
CommitPayload payload = new CommitPayload();
338+
payload.setBranch(branch);
339+
payload.setCommitMessage(commitMessage);
340+
payload.setStartBranch(startBranch);
341+
payload.setAuthorEmail(authorEmail);
342+
payload.setAuthorName(authorName);
343+
payload.setActions(actions);
344+
345+
Response response = post(Response.Status.CREATED, payload, "projects", urlEncode(project), "repository", "commits");
346+
return (response.readEntity(Commit.class));
347+
}
286348
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,20 @@ protected Response post(MultivaluedMap<String, String> queryParams, URL url) {
413413
return (invocation(url, queryParams).post(null));
414414
}
415415

416+
/**
417+
* Perform an HTTP POST call with the specified payload object and URL, returning
418+
* a ClientResponse instance with the data returned from the endpoint.
419+
*
420+
* @param payload the object instance that will be serialized to JSON and used as the POST data
421+
* @param url the fully formed path to the GitLab API endpoint
422+
* @return a ClientResponse instance with the data returned from the endpoint
423+
*/
424+
protected Response post(Object payload, Object... pathArgs) throws IOException {
425+
URL url = getApiUrl(pathArgs);
426+
Entity<?> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
427+
return (invocation(url, null).post(entity));
428+
}
429+
416430
/**
417431
* Perform an HTTP PUT call with the specified form data and path objects, returning
418432
* a ClientResponse instance with the data returned from the endpoint.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.gitlab4j.api.models.Branch;
3535
import org.gitlab4j.api.models.Comment;
3636
import org.gitlab4j.api.models.Commit;
37+
import org.gitlab4j.api.models.CommitPayload;
3738
import org.gitlab4j.api.models.CompareResults;
3839
import org.gitlab4j.api.models.DeployKey;
3940
import org.gitlab4j.api.models.Diff;
@@ -111,6 +112,17 @@ public void testCommit() {
111112
}
112113
}
113114

115+
@Test
116+
public void testCommitPayload() {
117+
118+
try {
119+
CommitPayload commitPayload = makeFakeApiCall(CommitPayload.class, "commit-payload");
120+
assertTrue(compareJson(commitPayload, "commit-payload"));
121+
} catch (Exception e) {
122+
e.printStackTrace();
123+
}
124+
}
125+
114126
@Test
115127
public void testCompareResults() {
116128

0 commit comments

Comments
 (0)