Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de29503

Browse files
mschoettlenejch
authored andcommitted
fix(api): return the new commit when calling cherry_pick
1 parent 939505b commit de29503

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

gitlab/v4/objects/commits.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def diff(self, **kwargs: Any) -> Union[gitlab.GitlabList, List[Dict[str, Any]]]:
4848

4949
@cli.register_custom_action(cls_names="ProjectCommit", required=("branch",))
5050
@exc.on_http_error(exc.GitlabCherryPickError)
51-
def cherry_pick(self, branch: str, **kwargs: Any) -> None:
51+
def cherry_pick(
52+
self, branch: str, **kwargs: Any
53+
) -> Union[Dict[str, Any], requests.Response]:
5254
"""Cherry-pick a commit into a branch.
5355
5456
Args:
@@ -58,10 +60,13 @@ def cherry_pick(self, branch: str, **kwargs: Any) -> None:
5860
Raises:
5961
GitlabAuthenticationError: If authentication is not correct
6062
GitlabCherryPickError: If the cherry-pick could not be performed
63+
64+
Returns:
65+
The new commit data (*not* a RESTObject)
6166
"""
6267
path = f"{self.manager.path}/{self.encoded_id}/cherry_pick"
6368
post_data = {"branch": branch}
64-
self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
69+
return self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
6570

6671
@cli.register_custom_action(cls_names="ProjectCommit", optional=("type",))
6772
@exc.on_http_error(exc.GitlabGetError)

tests/functional/api/test_repository.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,28 @@ def test_commit_discussion(project):
165165
note_from_get.delete()
166166

167167

168+
def test_cherry_pick_commit(project):
169+
commits = project.commits.list()
170+
commit = commits[1]
171+
parent_commit = commit.parent_ids[0]
172+
173+
# create a branch to cherry pick onto
174+
project.branches.create(
175+
{
176+
"branch": "test",
177+
"ref": parent_commit,
178+
}
179+
)
180+
cherry_pick_commit = commit.cherry_pick(branch="test")
181+
182+
expected_message = f"{commit.message}\n\n(cherry picked from commit {commit.id})"
183+
assert cherry_pick_commit["message"].startswith(expected_message)
184+
185+
with pytest.raises(gitlab.GitlabCherryPickError):
186+
# Two cherry pick attempts should raise GitlabCherryPickError
187+
commit.cherry_pick(branch="test")
188+
189+
168190
def test_revert_commit(project):
169191
commit = project.commits.list()[0]
170192
revert_commit = commit.revert(branch="main")

tests/unit/objects/test_commits.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def resp_commit():
3737
"short_id": "8b090c1b",
3838
"title": 'Revert "Initial commit"',
3939
}
40+
cherry_pick_content = {
41+
"id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
42+
"short_id": "8b090c1b",
43+
"title": "Initial commit",
44+
"message": "Initial commit\n\n\n(cherry picked from commit 6b2257eabcec3db1f59dafbd84935e3caea04235)",
45+
}
4046

4147
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
4248
rsps.add(
@@ -53,6 +59,13 @@ def resp_commit():
5359
content_type="application/json",
5460
status=200,
5561
)
62+
rsps.add(
63+
method=responses.POST,
64+
url="http://localhost/api/v4/projects/1/repository/commits/6b2257ea/cherry_pick",
65+
json=cherry_pick_content,
66+
content_type="application/json",
67+
status=200,
68+
)
5669
yield rsps
5770

5871

@@ -118,6 +131,18 @@ def test_create_commit(project, resp_create_commit):
118131
assert commit.title == data["commit_message"]
119132

120133

134+
def test_cherry_pick_commit(project, resp_commit):
135+
commit = project.commits.get("6b2257ea", lazy=True)
136+
cherry_pick_commit = commit.cherry_pick(branch="main")
137+
138+
assert cherry_pick_commit["short_id"] == "8b090c1b"
139+
assert cherry_pick_commit["title"] == "Initial commit"
140+
assert (
141+
cherry_pick_commit["message"]
142+
== "Initial commit\n\n\n(cherry picked from commit 6b2257eabcec3db1f59dafbd84935e3caea04235)"
143+
)
144+
145+
121146
def test_revert_commit(project, resp_commit):
122147
commit = project.commits.get("6b2257ea", lazy=True)
123148
revert_commit = commit.revert(branch="main")

0 commit comments

Comments
 (0)