Skip to content

Commit 5298964

Browse files
committed
feat: add support for commit revert API (python-gitlab#991)
1 parent 19242c3 commit 5298964

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

docs/gl_objects/commits.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ Cherry-pick a commit into another branch::
7272

7373
commit.cherry_pick(branch='target_branch')
7474

75+
Revert a commit on a given branch::
76+
77+
commit.revert(branch='target_branch')
78+
7579
Get the references the commit has been pushed to (branches and tags)::
7680

7781
commit.refs() # all references

gitlab/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ class GitlabRepairError(GitlabOperationError):
245245
pass
246246

247247

248+
class GitlabRevertError(GitlabOperationError):
249+
pass
250+
251+
248252
class GitlabLicenseError(GitlabOperationError):
249253
pass
250254

gitlab/v4/objects.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,22 @@ def merge_requests(self, **kwargs):
21362136
path = "%s/%s/merge_requests" % (self.manager.path, self.get_id())
21372137
return self.manager.gitlab.http_get(path, **kwargs)
21382138

2139+
@cli.register_custom_action("ProjectCommit", ("branch",))
2140+
@exc.on_http_error(exc.GitlabRevertError)
2141+
def revert(self, branch, **kwargs):
2142+
"""Revert a commit on a given branch.
2143+
2144+
Args:
2145+
branch (str): Name of target branch
2146+
**kwargs: Extra options to send to the server (e.g. sudo)
2147+
2148+
Raises:
2149+
GitlabAuthenticationError: If authentication is not correct
2150+
GitlabRevertError: If the revert could not be performed
2151+
"""
2152+
path = "%s/%s/revert" % (self.manager.path, self.get_id())
2153+
post_data = {"branch": branch}
2154+
self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
21392155

21402156
class ProjectCommitManager(RetrieveMixin, CreateMixin, RESTManager):
21412157
_path = "/projects/%(project_id)s/repository/commits"

tools/cli_test_v4.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ testcase "merge request validation" '
100100
--iid "$MR_ID" >/dev/null 2>&1
101101
'
102102

103+
# Test revert commit
104+
COMMITS=$(GITLAB -v project-commit list --project-id "${PROJECT_ID}")
105+
COMMIT_ID=$(pecho "${COMMITS}" | grep -m1 '^id:' | cut -d' ' -f2)
106+
107+
testcase "revert commit" '
108+
GITLAB project-commit revert --project-id "$PROJECT_ID" \
109+
--id "$COMMIT_ID" --branch master
110+
'
111+
103112
# Test project labels
104113
testcase "create project label" '
105114
OUTPUT=$(GITLAB -v project-label create --project-id $PROJECT_ID \

tools/python_test_v4.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,21 @@
462462
discussion = commit.discussions.get(discussion.id)
463463
# assert len(discussion.attributes["notes"]) == 1
464464

465+
# Revert commit
466+
commit.revert(branch="master")
467+
revert_commit = admin_project.commits.list()[0]
468+
469+
expected_message = "Revert \"{}\"\n\nThis reverts commit {}".format(
470+
commit.message, commit.id)
471+
assert revert_commit.message == expected_message
472+
473+
try:
474+
commit.revert(branch="master")
475+
# Only here to really ensure expected error without a full test framework
476+
raise AssertionError("Two revert attempts should raise GitlabRevertError")
477+
except gitlab.GitlabRevertError:
478+
pass
479+
465480
# housekeeping
466481
admin_project.housekeeping()
467482

0 commit comments

Comments
 (0)