Skip to content

Provide API wrapper for cherry picking commits #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/gl_objects/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/gl_objects/commits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===============

Expand Down
11 changes: 10 additions & 1 deletion gitlab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions gitlab/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
21 changes: 19 additions & 2 deletions gitlab/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)


Expand Down Expand Up @@ -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
Expand Down