1515import java .util .Optional ;
1616
1717import javax .ws .rs .core .Response ;
18+ import org .gitlab4j .api .models .Branch ;
1819
1920import org .gitlab4j .api .models .Comment ;
2021import 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