Commit 7a61bdd9 authored by Greg Messner's avatar Greg Messner
Browse files

Simplified creating commits (#453).

parent 180f0474
...@@ -669,7 +669,30 @@ public class CommitsApi extends AbstractApi { ...@@ -669,7 +669,30 @@ public class CommitsApi extends AbstractApi {
public Commit createCommit(Object projectIdOrPath, 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 { String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
CommitPayload payload = new CommitPayload()
.withBranch(branch)
.withStartBranch(startBranch)
.withCommitMessage(commitMessage)
.withAuthorEmail(authorEmail)
.withAuthorName(authorName)
.withActions(actions);
return (createCommit(projectIdOrPath, payload));
}
/**
* Create a commit with multiple files and actions.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/repository/commits</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param payload a CommitPayload instance holding the parameters for the commit
* @return the created Commit instance
* @throws GitLabApiException if any exception occurs during execution
*/
public Commit createCommit(Object projectIdOrPath, CommitPayload payload) throws GitLabApiException {
// Validate the actions // Validate the actions
List<CommitAction> actions = payload.getActions();
if (actions == null || actions.isEmpty()) { if (actions == null || actions.isEmpty()) {
throw new GitLabApiException("actions cannot be null or empty."); throw new GitLabApiException("actions cannot be null or empty.");
} }
...@@ -686,13 +709,9 @@ public class CommitsApi extends AbstractApi { ...@@ -686,13 +709,9 @@ public class CommitsApi extends AbstractApi {
} }
} }
CommitPayload payload = new CommitPayload(); if (payload.getStartProject() != null) {
payload.setBranch(branch); payload.setStartProject(getProjectIdOrPath(payload.getStartProject()));
payload.setCommitMessage(commitMessage); }
payload.setStartBranch(startBranch);
payload.setAuthorEmail(authorEmail);
payload.setAuthorName(authorName);
payload.setActions(actions);
Response response = post(Response.Status.CREATED, payload, Response response = post(Response.Status.CREATED, payload,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits"); "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
......
package org.gitlab4j.api.models; package org.gitlab4j.api.models;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
...@@ -9,9 +10,13 @@ public class CommitPayload { ...@@ -9,9 +10,13 @@ public class CommitPayload {
private String branch; private String branch;
private String commitMessage; private String commitMessage;
private String startBranch; private String startBranch;
private String startSha;
private Object startProject;
private List<CommitAction> actions; private List<CommitAction> actions;
private String authorEmail; private String authorEmail;
private String authorName; private String authorName;
private Boolean stats;
private Boolean force;
public String getBranch() { public String getBranch() {
return branch; return branch;
...@@ -21,6 +26,11 @@ public class CommitPayload { ...@@ -21,6 +26,11 @@ public class CommitPayload {
this.branch = branch; this.branch = branch;
} }
public CommitPayload withBranch(String branch) {
this.branch = branch;
return (this);
}
public String getCommitMessage() { public String getCommitMessage() {
return commitMessage; return commitMessage;
} }
...@@ -29,6 +39,11 @@ public class CommitPayload { ...@@ -29,6 +39,11 @@ public class CommitPayload {
this.commitMessage = commitMessage; this.commitMessage = commitMessage;
} }
public CommitPayload withCommitMessage(String commitMessage) {
this.commitMessage = commitMessage;
return (this);
}
public String getStartBranch() { public String getStartBranch() {
return startBranch; return startBranch;
} }
...@@ -37,6 +52,37 @@ public class CommitPayload { ...@@ -37,6 +52,37 @@ public class CommitPayload {
this.startBranch = startBranch; this.startBranch = startBranch;
} }
public CommitPayload withStartBranch(String startBranch) {
this.startBranch = startBranch;
return (this);
}
public String getStartSha() {
return startSha;
}
public void setStartSha(String startSha) {
this.startSha = startSha;
}
public CommitPayload withStartSha(String startSha) {
this.startSha = startSha;
return (this);
}
public Object getStartProject() {
return startProject;
}
public void setStartProject(Object startProject) {
this.startProject = startProject;
}
public CommitPayload withStartProject(Object startProject) {
this.startProject = startProject;
return (this);
}
public List<CommitAction> getActions() { public List<CommitAction> getActions() {
return actions; return actions;
} }
...@@ -45,6 +91,34 @@ public class CommitPayload { ...@@ -45,6 +91,34 @@ public class CommitPayload {
this.actions = actions; this.actions = actions;
} }
public CommitPayload withActions(List<CommitAction> actions) {
this.actions = actions;
return (this);
}
public CommitPayload withAction(CommitAction action) {
if (actions == null) {
actions = new ArrayList<>();
}
actions.add(action);
return (this);
}
public CommitPayload withAction(CommitAction.Action action, String filePath) {
return (withAction(action, null, filePath));
}
public CommitPayload withAction(CommitAction.Action action, String content, String filePath) {
CommitAction commitAction = new CommitAction()
.withAction(action)
.withContent(content)
.withFilePath(filePath);
return (withAction(commitAction));
}
public String getAuthorEmail() { public String getAuthorEmail() {
return authorEmail; return authorEmail;
} }
...@@ -53,6 +127,11 @@ public class CommitPayload { ...@@ -53,6 +127,11 @@ public class CommitPayload {
this.authorEmail = authorEmail; this.authorEmail = authorEmail;
} }
public CommitPayload withAuthorEmail(String authorEmail) {
this.authorEmail = authorEmail;
return (this);
}
public String getAuthorName() { public String getAuthorName() {
return authorName; return authorName;
} }
...@@ -61,6 +140,37 @@ public class CommitPayload { ...@@ -61,6 +140,37 @@ public class CommitPayload {
this.authorName = authorName; this.authorName = authorName;
} }
public CommitPayload withAuthorName(String authorName) {
this.authorName = authorName;
return (this);
}
public Boolean getStats() {
return stats;
}
public void setStats(Boolean stats) {
this.stats = stats;
}
public CommitPayload withStats(Boolean stats) {
this.stats = stats;
return (this);
}
public Boolean getForce() {
return force;
}
public void setForce(Boolean force) {
this.force = force;
}
public CommitPayload withForce(Boolean force) {
this.force = force;
return (this);
}
@Override @Override
public String toString() { public String toString() {
return (JacksonJson.toJsonString(this)); return (JacksonJson.toJsonString(this));
......
...@@ -21,6 +21,7 @@ import org.gitlab4j.api.models.Comment; ...@@ -21,6 +21,7 @@ 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.CommitAction;
import org.gitlab4j.api.models.CommitAction.Action; import org.gitlab4j.api.models.CommitAction.Action;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.CommitRef; import org.gitlab4j.api.models.CommitRef;
import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
...@@ -286,6 +287,50 @@ public class TestCommitsApi extends AbstractIntegrationTest { ...@@ -286,6 +287,50 @@ public class TestCommitsApi extends AbstractIntegrationTest {
} }
} }
@Test
public void testCreateCommitWithPayload() throws GitLabApiException {
String TEST_BRANCH = "create_commit_from_payload";
Optional<Branch> testBranch = gitLabApi.getRepositoryApi().getOptionalBranch(testProject, TEST_BRANCH);
if (!testBranch.isPresent()) {
gitLabApi.getRepositoryApi().createBranch(testProject, TEST_BRANCH, "master");
}
if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH).isPresent()) {
try {
gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH, "Deleted test file");
} catch (GitLabApiException ignore) {}
}
// Arrange
CommitPayload commitPayload = new CommitPayload()
.withBranch(TEST_BRANCH)
.withCommitMessage("Testing createCommit() create action")
.withAction(Action.CREATE, "This is the original data in the file", TEST_CREATE_COMMIT_FILEPATH);
// Act
Commit commit = gitLabApi.getCommitsApi().createCommit(testProject, commitPayload);
// Assert
assertNotNull(commit);
// Arrange
commitPayload = new CommitPayload()
.withBranch(TEST_BRANCH)
.withCommitMessage("Testing createCommit() delete action")
.withAction(Action.DELETE, TEST_CREATE_COMMIT_FILEPATH);
// Act
commit = gitLabApi.getCommitsApi().createCommit(testProject, commitPayload);
// Assert
assertNotNull(commit);
Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH);
assertFalse(repoFile.isPresent());
}
@Test @Test
public void testRevertCommit() throws GitLabApiException { public void testRevertCommit() throws GitLabApiException {
......
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