Skip to content

Commit ffc5e18

Browse files
ctabingmessner
authored andcommitted
Adds support of commit cherry-picking (#403)
1 parent fd7f5a2 commit ffc5e18

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,23 @@ public Commit revertCommit(Object projectIdOrPath, String sha, String branch) th
644644
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "revert");
645645
return (response.readEntity(Commit.class));
646646
}
647+
648+
/**
649+
* Cherry picks a commit in a given branch.
650+
*
651+
* <pre><code>GitLab Endpoint: POST /projects/:id/repository/commits/:sha/cherry_pick</code></pre>
652+
*
653+
* @since GitLab 8.15
654+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
655+
* @param sha the commit SHA to cherry pick
656+
* @param branch the target branch to cherry pick the commit on
657+
* @return a Commit instance holding the cherry picked commit
658+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
659+
*/
660+
public Commit cherryPickCommit(Object projectIdOrPath, String sha, String branch) throws GitLabApiException {
661+
GitLabApiForm formData = new GitLabApiForm().withParam("branch", branch, true);
662+
Response response = post(Response.Status.CREATED, formData,
663+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "cherry_pick");
664+
return (response.readEntity(Commit.class));
665+
}
647666
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Optional;
1616

1717
import javax.ws.rs.core.Response;
18+
import org.gitlab4j.api.models.Branch;
1819

1920
import org.gitlab4j.api.models.Comment;
2021
import org.gitlab4j.api.models.Commit;
@@ -321,4 +322,50 @@ public void testRevertCommit() throws GitLabApiException {
321322
repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
322323
assertFalse(repoFile.isPresent());
323324
}
325+
326+
@Test
327+
public void testCherryPickCommit() throws GitLabApiException {
328+
329+
// Make sure the branch to cherry pick does not exist
330+
if(gitLabApi.getRepositoryApi().getOptionalBranch(testProject, "cherry-pick-branch").isPresent()) {
331+
gitLabApi.getRepositoryApi().deleteBranch(testProject, "cherry-pick-branch");
332+
}
333+
334+
// Make sure the file to create does not exist.
335+
String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test";
336+
if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) {
337+
gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file");
338+
}
339+
340+
// Act
341+
Branch branch = gitLabApi.getRepositoryApi().createBranch(testProject, "cherry-pick-branch", "master");
342+
343+
// Assert
344+
assertNotNull(branch);
345+
Optional<RepositoryFile> repoFileBranch = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
346+
assertFalse(repoFileBranch.isPresent());
347+
348+
// Arrange
349+
CommitAction commitAction = new CommitAction()
350+
.withAction(Action.CREATE)
351+
.withContent("This is the original data in the file")
352+
.withFilePath(filePath);
353+
354+
// Act
355+
Commit commit = gitLabApi.getCommitsApi().createCommit(
356+
testProject, "master", "Testing createCommit() create action", null, null, null, commitAction);
357+
358+
// Assert
359+
assertNotNull(commit);
360+
Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
361+
assertTrue(repoFile.isPresent());
362+
363+
// Act
364+
Commit cherryPickedCommit = gitLabApi.getCommitsApi().cherryPickCommit(testProject, commit.getId(), "cherry-pick-branch");
365+
366+
// Assert
367+
assertNotNull(cherryPickedCommit);
368+
Optional<RepositoryFile> repoFileBranchCherryPicked = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
369+
assertTrue(repoFileBranchCherryPicked.isPresent());
370+
}
324371
}

0 commit comments

Comments
 (0)