diff options
author | Christian <cgumpert@users.noreply.github.com> | 2017-03-21 06:50:20 +0100 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2017-03-21 06:50:20 +0100 |
commit | 22bf12827387cb1719bacae6c0c745cd768eee6c (patch) | |
tree | 80fd22ecc95ec6f41fb442a0ddf5d6b551822ee0 | |
parent | 8677f1c0250e9ab6b1e16da17d593101128cf057 (diff) | |
download | gitlab-22bf12827387cb1719bacae6c0c745cd768eee6c.tar.gz |
Provide API wrapper for cherry picking commits (#236)
-rw-r--r-- | docs/gl_objects/commits.py | 4 | ||||
-rw-r--r-- | docs/gl_objects/commits.rst | 6 | ||||
-rw-r--r-- | gitlab/cli.py | 11 | ||||
-rw-r--r-- | gitlab/exceptions.py | 4 | ||||
-rw-r--r-- | gitlab/objects.py | 21 |
5 files changed, 43 insertions, 3 deletions
diff --git a/docs/gl_objects/commits.py b/docs/gl_objects/commits.py index 2ed66f5..0d47edb 100644 --- a/docs/gl_objects/commits.py +++ b/docs/gl_objects/commits.py @@ -39,6 +39,10 @@ commit = project.commits.get('e3d5a71b') 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 8be1b86..6fef8bf 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 32b3ec8..2a41907 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -42,7 +42,9 @@ EXTRA_ACTIONS = { 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 @@ class GitlabCLI(object): 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 11bbe26..fc901d1 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 7c961b3..245b88f 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1158,7 +1158,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) @@ -1171,7 +1171,7 @@ class ProjectBranch(GitlabObject): del self.protected def unprotect(self, **kwargs): - """Unprotects the project.""" + """Unprotects the branch.""" self.protect(False, **kwargs) @@ -1374,6 +1374,23 @@ class ProjectCommit(GitlabObject): {'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 |