Skip to content

Commit

Permalink
feat: add support for commit revert API (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch committed Feb 18, 2020
1 parent 19242c3 commit 5298964
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/gl_objects/commits.rst
Expand Up @@ -72,6 +72,10 @@ Cherry-pick a commit into another branch::

commit.cherry_pick(branch='target_branch')

Revert a commit on a given branch::

commit.revert(branch='target_branch')

Get the references the commit has been pushed to (branches and tags)::

commit.refs() # all references
Expand Down
4 changes: 4 additions & 0 deletions gitlab/exceptions.py
Expand Up @@ -245,6 +245,10 @@ class GitlabRepairError(GitlabOperationError):
pass


class GitlabRevertError(GitlabOperationError):
pass


class GitlabLicenseError(GitlabOperationError):
pass

Expand Down
16 changes: 16 additions & 0 deletions gitlab/v4/objects.py
Expand Up @@ -2136,6 +2136,22 @@ def merge_requests(self, **kwargs):
path = "%s/%s/merge_requests" % (self.manager.path, self.get_id())
return self.manager.gitlab.http_get(path, **kwargs)

@cli.register_custom_action("ProjectCommit", ("branch",))
@exc.on_http_error(exc.GitlabRevertError)
def revert(self, branch, **kwargs):
"""Revert a commit on a given branch.
Args:
branch (str): Name of target branch
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabRevertError: If the revert could not be performed
"""
path = "%s/%s/revert" % (self.manager.path, self.get_id())
post_data = {"branch": branch}
self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)

class ProjectCommitManager(RetrieveMixin, CreateMixin, RESTManager):
_path = "/projects/%(project_id)s/repository/commits"
Expand Down
9 changes: 9 additions & 0 deletions tools/cli_test_v4.sh
Expand Up @@ -100,6 +100,15 @@ testcase "merge request validation" '
--iid "$MR_ID" >/dev/null 2>&1
'

# Test revert commit
COMMITS=$(GITLAB -v project-commit list --project-id "${PROJECT_ID}")
COMMIT_ID=$(pecho "${COMMITS}" | grep -m1 '^id:' | cut -d' ' -f2)

testcase "revert commit" '
GITLAB project-commit revert --project-id "$PROJECT_ID" \
--id "$COMMIT_ID" --branch master
'

# Test project labels
testcase "create project label" '
OUTPUT=$(GITLAB -v project-label create --project-id $PROJECT_ID \
Expand Down
15 changes: 15 additions & 0 deletions tools/python_test_v4.py
Expand Up @@ -462,6 +462,21 @@
discussion = commit.discussions.get(discussion.id)
# assert len(discussion.attributes["notes"]) == 1

# Revert commit
commit.revert(branch="master")
revert_commit = admin_project.commits.list()[0]

expected_message = "Revert \"{}\"\n\nThis reverts commit {}".format(
commit.message, commit.id)
assert revert_commit.message == expected_message

try:
commit.revert(branch="master")
# Only here to really ensure expected error without a full test framework
raise AssertionError("Two revert attempts should raise GitlabRevertError")
except gitlab.GitlabRevertError:
pass

# housekeeping
admin_project.housekeeping()

Expand Down

0 comments on commit 5298964

Please sign in to comment.