From baad39dd4abb3c1830f21c66991302d859c06442 Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Wed, 15 Mar 2017 14:21:12 +0100 Subject: [PATCH 1/8] fix typo in doc string --- gitlab/objects.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index efe75d0a6..b845324f7 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) From ccec093e0218d61f686211a994c1ab7adc085f35 Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Wed, 15 Mar 2017 17:08:32 +0100 Subject: [PATCH 2/8] implement API for commit cherry-picking --- gitlab/exceptions.py | 3 +++ gitlab/objects.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 11bbe26cb..c1c37a2f8 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -146,6 +146,9 @@ class GitlabTodoError(GitlabOperationError): 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 b845324f7..fd77a9d9f 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1353,6 +1353,20 @@ 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 From 4b09c93baa1a390750f61490f9b17252a89fc587 Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Wed, 15 Mar 2017 17:09:39 +0100 Subject: [PATCH 3/8] add CLI support for cherry-picking --- gitlab/cli.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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) From e3ec6c26992627008db89cefc0a8ca138262a83e Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Wed, 15 Mar 2017 17:47:35 +0100 Subject: [PATCH 4/8] fix style problems --- gitlab/exceptions.py | 1 + gitlab/objects.py | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index c1c37a2f8..fc901d1a9 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -146,6 +146,7 @@ class GitlabTodoError(GitlabOperationError): class GitlabTimeTrackingError(GitlabOperationError): pass + class GitlabCherryPickError(GitlabOperationError): pass diff --git a/gitlab/objects.py b/gitlab/objects.py index fd77a9d9f..6c44f53b4 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1353,7 +1353,7 @@ def builds(self, **kwargs): {'project_id': self.project_id}, **kwargs) - def cherry_pick(self,branch,**kwargs): + def cherry_pick(self, branch, **kwargs): """Cherry-pick a commit into a branch. Args: @@ -1364,9 +1364,12 @@ def cherry_pick(self,branch,**kwargs): """ 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) + + 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) + raise_error_from_response(r, errors, expected_code=201) + class ProjectCommitManager(BaseManager): obj_cls = ProjectCommit From 3a652863ecaab4cfb7cab433e1afeae153bfad0c Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Wed, 15 Mar 2017 17:54:42 +0100 Subject: [PATCH 5/8] update documentation --- docs/gl_objects/commits.py | 4 ++++ docs/gl_objects/commits.rst | 6 ++++++ 2 files changed, 10 insertions(+) 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..0fd96a720 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 =============== From 345b1d736633432022ae1fc5438066c0ecae1e06 Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Thu, 16 Mar 2017 11:59:11 +0100 Subject: [PATCH 6/8] fix formatting issue with long line --- gitlab/objects.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index 6c44f53b4..1b713277b 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1362,8 +1362,8 @@ def cherry_pick(self, branch, **kwargs): Raises: GitlabCherryPickError: If the cherry pick could not be applied. """ - url = '/projects/%s/repository/commits/%s/cherry_pick' % (self.project_id, - self.id) + url = '/projects/{0:s}/repository/commits/{1:s}/cherry_pick'.format( + self.project_id, self.id) r = self.gitlab._raw_post(url, data={'project_id': self.project_id, 'branch': branch}, **kwargs) From d1ae6c68f2b0ae61f7c29c43ab39603e4a59a01a Mon Sep 17 00:00:00 2001 From: Christian Gumpert Date: Mon, 20 Mar 2017 10:24:59 +0100 Subject: [PATCH 7/8] address review comments --- docs/gl_objects/commits.rst | 2 +- gitlab/objects.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/gl_objects/commits.rst b/docs/gl_objects/commits.rst index 0fd96a720..6fef8bf7e 100644 --- a/docs/gl_objects/commits.rst +++ b/docs/gl_objects/commits.rst @@ -43,7 +43,7 @@ Get the diff for a commit: :start-after: # diff :end-before: # end diff -Cherry-pick a commit into another branch +Cherry-pick a commit into another branch: .. literalinclude:: commits.py :start-after: # cherry diff --git a/gitlab/objects.py b/gitlab/objects.py index 1b713277b..b42c65a5c 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1362,8 +1362,8 @@ def cherry_pick(self, branch, **kwargs): Raises: GitlabCherryPickError: If the cherry pick could not be applied. """ - url = '/projects/{0:s}/repository/commits/{1:s}/cherry_pick'.format( - self.project_id, self.id) + pid = self.project_id + url = '/projects/%s/repository/commits/%s/cherry_pick' % (pid, self.id) r = self.gitlab._raw_post(url, data={'project_id': self.project_id, 'branch': branch}, **kwargs) From 823cc6efd84837fa04f1560e389a63bbb5e248a4 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Tue, 21 Mar 2017 06:48:01 +0100 Subject: [PATCH 8/8] cherry_pick: minor formatting update --- gitlab/objects.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index b42c65a5c..f77636f7a 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1362,8 +1362,8 @@ def cherry_pick(self, branch, **kwargs): Raises: GitlabCherryPickError: If the cherry pick could not be applied. """ - pid = self.project_id - url = '/projects/%s/repository/commits/%s/cherry_pick' % (pid, self.id) + 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)