diff --git a/docs/gl_objects/commits.py b/docs/gl_objects/commits.py index 2ed66f560..0d47edb9b 100644 --- a/docs/gl_objects/commits.py +++ b/docs/gl_objects/commits.py @@ -39,6 +39,10 @@ diff = commit.diff() # end diff +# cherry +commit.cherry_pick(branch='target_branch') +# end cherry + # comments list comments = gl.project_commit_comments.list(project_id=1, commit_id='master') # or diff --git a/docs/gl_objects/commits.rst b/docs/gl_objects/commits.rst index 8be1b8602..6fef8bf7e 100644 --- a/docs/gl_objects/commits.rst +++ b/docs/gl_objects/commits.rst @@ -43,6 +43,12 @@ Get the diff for a commit: :start-after: # diff :end-before: # end diff +Cherry-pick a commit into another branch: + +.. literalinclude:: commits.py + :start-after: # cherry + :end-before: # end cherry + Commit comments =============== diff --git a/gitlab/cli.py b/gitlab/cli.py index 32b3ec850..2a419072a 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -42,7 +42,9 @@ gitlab.ProjectCommit: {'diff': {'required': ['id', 'project-id']}, 'blob': {'required': ['id', 'project-id', 'filepath']}, - 'builds': {'required': ['id', 'project-id']}}, + 'builds': {'required': ['id', 'project-id']}, + 'cherrypick': {'required': ['id', 'project-id', + 'branch']}}, gitlab.ProjectIssue: {'subscribe': {'required': ['id', 'project-id']}, 'unsubscribe': {'required': ['id', 'project-id']}, 'move': {'required': ['id', 'project-id', @@ -267,6 +269,13 @@ def do_project_commit_builds(self, cls, gl, what, args): except Exception as e: _die("Impossible to get commit builds", e) + def do_project_commit_cherrypick(self, cls, gl, what, args): + try: + o = self.do_get(cls, gl, what, args) + o.cherry_pick(branch=args['branch']) + except Exception as e: + _die("Impossible to cherry-pick commit", e) + def do_project_build_cancel(self, cls, gl, what, args): try: o = self.do_get(cls, gl, what, args) diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 11bbe26cb..fc901d1a9 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -147,6 +147,10 @@ class GitlabTimeTrackingError(GitlabOperationError): pass +class GitlabCherryPickError(GitlabOperationError): + pass + + def raise_error_from_response(response, error, expected_code=200): """Tries to parse gitlab error message from response and raises error. diff --git a/gitlab/objects.py b/gitlab/objects.py index efe75d0a6..f77636f7a 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1137,7 +1137,7 @@ class ProjectBranch(GitlabObject): requiredCreateAttrs = ['branch_name', 'ref'] def protect(self, protect=True, **kwargs): - """Protects the project.""" + """Protects the branch.""" url = self._url % {'project_id': self.project_id} action = 'protect' if protect else 'unprotect' url = "%s/%s/%s" % (url, self.name, action) @@ -1150,7 +1150,7 @@ def protect(self, protect=True, **kwargs): del self.protected def unprotect(self, **kwargs): - """Unprotects the project.""" + """Unprotects the branch.""" self.protect(False, **kwargs) @@ -1353,6 +1353,23 @@ def builds(self, **kwargs): {'project_id': self.project_id}, **kwargs) + def cherry_pick(self, branch, **kwargs): + """Cherry-pick a commit into a branch. + + Args: + branch (str): Name of target branch. + + Raises: + GitlabCherryPickError: If the cherry pick could not be applied. + """ + url = ('/projects/%s/repository/commits/%s/cherry_pick' % + (self.project_id, self.id)) + + r = self.gitlab._raw_post(url, data={'project_id': self.project_id, + 'branch': branch}, **kwargs) + errors = {400: GitlabCherryPickError} + raise_error_from_response(r, errors, expected_code=201) + class ProjectCommitManager(BaseManager): obj_cls = ProjectCommit