Skip to content

Commit 7364fba

Browse files
Add commit cherry-pick API
Co-Authored-By: hikingyo <[email protected]>
1 parent cdd5b56 commit 7364fba

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Api/Repositories.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,30 @@ public function mergeBase($project_id, array $refs)
447447
return $this->get($this->getProjectPath($project_id, 'repository/merge_base'), ['refs' => $refs]);
448448
}
449449

450+
/**
451+
* @param int|string $project_id
452+
* @param string $sha
453+
* @param array $params
454+
*
455+
* @return mixed
456+
*/
457+
public function cherryPick($project_id, string $sha, array $params = [])
458+
{
459+
$resolver = $this->createOptionsResolver();
460+
$booleanNormalizer = function (Options $resolver, $value) {
461+
return $value ? 'true' : 'false';
462+
};
463+
464+
$resolver->setDefined('branch')
465+
->setRequired('branch');
466+
467+
$resolver->setDefined('dry_run')
468+
->setAllowedTypes('dry_run', 'bool')
469+
->setNormalizer('dry_run', $booleanNormalizer);
470+
471+
return $this->post($this->getProjectPath($project_id, 'repository/commits/'.self::encodePath($sha).'/cherry_pick'), $params);
472+
}
473+
450474
protected function createOptionsResolver()
451475
{
452476
$allowedTypeValues = [

tests/Api/RepositoriesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,39 @@ public function shouldGetMergeBase(): void
626626
$this->assertEquals($expectedArray, $api->mergeBase(1, ['efgh5678efgh5678efgh5678efgh5678efgh5678', '1234567812345678123456781234567812345678']));
627627
}
628628

629+
/**
630+
* @test
631+
*/
632+
public function shouldCherryPick(): void
633+
{
634+
$expectedArray = [
635+
'id' => 'abcd1234abcd1234abcd1234abcd1234abcd1234',
636+
'short_id' => 'abcd1234',
637+
'title' => 'A commit',
638+
'author_name' => 'Example User',
639+
'author_email' => '[email protected]',
640+
'authored_date' => '2018-01-01T00:00:00.000Z',
641+
'created_at' => '2018-01-01T00:00:00.000Z',
642+
'committer_name' => 'Jane Doe',
643+
'committer_email' => '[email protected]',
644+
'committed_date' => '2018-01-01T00:00:00.000Z',
645+
'message' => 'A commit',
646+
'parent_ids' => [
647+
'efgh5678efgh5678efgh5678efgh5678efgh5678',
648+
],
649+
'web_url' => 'https://gitlab.example.com/thedude/gitlab-foss/-/commit/abcd1234abcd1234abcd1234abcd1234abcd1234',
650+
];
651+
652+
$api = $this->getApiMock();
653+
$api->expects($this->once())
654+
->method('post')
655+
->with('projects/1/repository/commits/123456123456/cherry_pick', ['branch' => 'feature_branch'])
656+
->will($this->returnValue($expectedArray))
657+
;
658+
659+
$this->assertEquals($expectedArray, $api->cherryPick(1, '123456123456', ['branch' => 'feature_branch']));
660+
}
661+
629662
protected function getApiClass()
630663
{
631664
return Repositories::class;

0 commit comments

Comments
 (0)