Commit 69ce3604 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for batch commit creation (#122).

parent 1929d5d2
...@@ -123,6 +123,24 @@ public abstract class AbstractApi implements Constants { ...@@ -123,6 +123,24 @@ public abstract class AbstractApi implements Constants {
} }
} }
/**
* Perform an HTTP POST call with the specified payload object and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param payload the object instance that will be serialized to JSON and used as the POST data
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response post(Response.Status expectedStatus, Object payload, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().post(payload, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/** /**
* Perform an HTTP POST call with the specified form data and path objects, returning * Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint. * a ClientResponse instance with the data returned from the endpoint.
......
...@@ -11,6 +11,8 @@ import javax.ws.rs.core.Response; ...@@ -11,6 +11,8 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Comment; import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitAction;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.utils.ISO8601; import org.gitlab4j.api.utils.ISO8601;
...@@ -283,4 +285,64 @@ public class CommitsApi extends AbstractApi { ...@@ -283,4 +285,64 @@ public class CommitsApi extends AbstractApi {
public Comment addComment(int projectId, String sha, String note) throws GitLabApiException { public Comment addComment(int projectId, String sha, String note) throws GitLabApiException {
return (addComment(projectId, sha, note, null, null, null)); return (addComment(projectId, sha, note, null, null, null));
} }
/**
* Create a commit with multiple files and actions.
*
* 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
*/
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 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
*/
public Commit createCommit(String project, 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", urlEncode(project), "repository", "commits");
return (response.readEntity(Commit.class));
}
} }
...@@ -413,6 +413,20 @@ public class GitLabApiClient { ...@@ -413,6 +413,20 @@ public class GitLabApiClient {
return (invocation(url, queryParams).post(null)); return (invocation(url, queryParams).post(null));
} }
/**
* Perform an HTTP POST call with the specified payload object and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param payload the object instance that will be serialized to JSON and used as the POST data
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response post(Object payload, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
Entity<?> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
return (invocation(url, null).post(entity));
}
/** /**
* Perform an HTTP PUT call with the specified form data and path objects, returning * Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint. * a ClientResponse instance with the data returned from the endpoint.
......
...@@ -34,6 +34,7 @@ import org.gitlab4j.api.models.ArtifactsFile; ...@@ -34,6 +34,7 @@ import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Comment; import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.CompareResults; import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.DeployKey; import org.gitlab4j.api.models.DeployKey;
import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Diff;
...@@ -111,6 +112,17 @@ public class TestGitLabApiBeans { ...@@ -111,6 +112,17 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testCommitPayload() {
try {
CommitPayload commitPayload = makeFakeApiCall(CommitPayload.class, "commit-payload");
assertTrue(compareJson(commitPayload, "commit-payload"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testCompareResults() { public void testCompareResults() {
......
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