Commit ffc5e180 authored by Cédric Tabin's avatar Cédric Tabin Committed by Greg Messner
Browse files

Adds support of commit cherry-picking (#403)

parent fd7f5a2a
......@@ -644,4 +644,23 @@ public class CommitsApi extends AbstractApi {
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "revert");
return (response.readEntity(Commit.class));
}
/**
* Cherry picks a commit in a given branch.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/repository/commits/:sha/cherry_pick</code></pre>
*
* @since GitLab 8.15
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha the commit SHA to cherry pick
* @param branch the target branch to cherry pick the commit on
* @return a Commit instance holding the cherry picked commit
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Commit cherryPickCommit(Object projectIdOrPath, String sha, String branch) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("branch", branch, true);
Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "cherry_pick");
return (response.readEntity(Commit.class));
}
}
......@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
......@@ -321,4 +322,50 @@ public class TestCommitsApi extends AbstractIntegrationTest {
repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
assertFalse(repoFile.isPresent());
}
@Test
public void testCherryPickCommit() throws GitLabApiException {
// Make sure the branch to cherry pick does not exist
if(gitLabApi.getRepositoryApi().getOptionalBranch(testProject, "cherry-pick-branch").isPresent()) {
gitLabApi.getRepositoryApi().deleteBranch(testProject, "cherry-pick-branch");
}
// Make sure the file to create does not exist.
String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test";
if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) {
gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file");
}
// Act
Branch branch = gitLabApi.getRepositoryApi().createBranch(testProject, "cherry-pick-branch", "master");
// Assert
assertNotNull(branch);
Optional<RepositoryFile> repoFileBranch = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
assertFalse(repoFileBranch.isPresent());
// Arrange
CommitAction commitAction = new CommitAction()
.withAction(Action.CREATE)
.withContent("This is the original data in the file")
.withFilePath(filePath);
// Act
Commit commit = gitLabApi.getCommitsApi().createCommit(
testProject, "master", "Testing createCommit() create action", null, null, null, commitAction);
// Assert
assertNotNull(commit);
Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
assertTrue(repoFile.isPresent());
// Act
Commit cherryPickedCommit = gitLabApi.getCommitsApi().cherryPickCommit(testProject, commit.getId(), "cherry-pick-branch");
// Assert
assertNotNull(cherryPickedCommit);
Optional<RepositoryFile> repoFileBranchCherryPicked = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
assertTrue(repoFileBranchCherryPicked.isPresent());
}
}
\ No newline at end of file
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