Skip to content

Commit 22bf128

Browse files
cgumpertGauvain Pocentek
authored and
Gauvain Pocentek
committed
Provide API wrapper for cherry picking commits (#236)
1 parent 8677f1c commit 22bf128

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

docs/gl_objects/commits.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
diff = commit.diff()
4040
# end diff
4141

42+
# cherry
43+
commit.cherry_pick(branch='target_branch')
44+
# end cherry
45+
4246
# comments list
4347
comments = gl.project_commit_comments.list(project_id=1, commit_id='master')
4448
# or

docs/gl_objects/commits.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Get the diff for a commit:
4343
:start-after: # diff
4444
:end-before: # end diff
4545

46+
Cherry-pick a commit into another branch:
47+
48+
.. literalinclude:: commits.py
49+
:start-after: # cherry
50+
:end-before: # end cherry
51+
4652
Commit comments
4753
===============
4854

gitlab/cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
gitlab.ProjectCommit: {'diff': {'required': ['id', 'project-id']},
4343
'blob': {'required': ['id', 'project-id',
4444
'filepath']},
45-
'builds': {'required': ['id', 'project-id']}},
45+
'builds': {'required': ['id', 'project-id']},
46+
'cherrypick': {'required': ['id', 'project-id',
47+
'branch']}},
4648
gitlab.ProjectIssue: {'subscribe': {'required': ['id', 'project-id']},
4749
'unsubscribe': {'required': ['id', 'project-id']},
4850
'move': {'required': ['id', 'project-id',
@@ -267,6 +269,13 @@ def do_project_commit_builds(self, cls, gl, what, args):
267269
except Exception as e:
268270
_die("Impossible to get commit builds", e)
269271

272+
def do_project_commit_cherrypick(self, cls, gl, what, args):
273+
try:
274+
o = self.do_get(cls, gl, what, args)
275+
o.cherry_pick(branch=args['branch'])
276+
except Exception as e:
277+
_die("Impossible to cherry-pick commit", e)
278+
270279
def do_project_build_cancel(self, cls, gl, what, args):
271280
try:
272281
o = self.do_get(cls, gl, what, args)

gitlab/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ class GitlabTimeTrackingError(GitlabOperationError):
147147
pass
148148

149149

150+
class GitlabCherryPickError(GitlabOperationError):
151+
pass
152+
153+
150154
def raise_error_from_response(response, error, expected_code=200):
151155
"""Tries to parse gitlab error message from response and raises error.
152156

gitlab/objects.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ class ProjectBranch(GitlabObject):
11581158
requiredCreateAttrs = ['branch_name', 'ref']
11591159

11601160
def protect(self, protect=True, **kwargs):
1161-
"""Protects the project."""
1161+
"""Protects the branch."""
11621162
url = self._url % {'project_id': self.project_id}
11631163
action = 'protect' if protect else 'unprotect'
11641164
url = "%s/%s/%s" % (url, self.name, action)
@@ -1171,7 +1171,7 @@ def protect(self, protect=True, **kwargs):
11711171
del self.protected
11721172

11731173
def unprotect(self, **kwargs):
1174-
"""Unprotects the project."""
1174+
"""Unprotects the branch."""
11751175
self.protect(False, **kwargs)
11761176

11771177

@@ -1374,6 +1374,23 @@ def builds(self, **kwargs):
13741374
{'project_id': self.project_id},
13751375
**kwargs)
13761376

1377+
def cherry_pick(self, branch, **kwargs):
1378+
"""Cherry-pick a commit into a branch.
1379+
1380+
Args:
1381+
branch (str): Name of target branch.
1382+
1383+
Raises:
1384+
GitlabCherryPickError: If the cherry pick could not be applied.
1385+
"""
1386+
url = ('/projects/%s/repository/commits/%s/cherry_pick' %
1387+
(self.project_id, self.id))
1388+
1389+
r = self.gitlab._raw_post(url, data={'project_id': self.project_id,
1390+
'branch': branch}, **kwargs)
1391+
errors = {400: GitlabCherryPickError}
1392+
raise_error_from_response(r, errors, expected_code=201)
1393+
13771394

13781395
class ProjectCommitManager(BaseManager):
13791396
obj_cls = ProjectCommit

0 commit comments

Comments
 (0)