File tree Expand file tree Collapse file tree 5 files changed +48
-0
lines changed
Expand file tree Collapse file tree 5 files changed +48
-0
lines changed Original file line number Diff line number Diff 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+
7579Get the references the commit has been pushed to (branches and tags)::
7680
7781 commit.refs() # all references
Original file line number Diff line number Diff line change @@ -245,6 +245,10 @@ class GitlabRepairError(GitlabOperationError):
245245 pass
246246
247247
248+ class GitlabRevertError (GitlabOperationError ):
249+ pass
250+
251+
248252class GitlabLicenseError (GitlabOperationError ):
249253 pass
250254
Original file line number Diff line number Diff 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
21402156class ProjectCommitManager (RetrieveMixin , CreateMixin , RESTManager ):
21412157 _path = "/projects/%(project_id)s/repository/commits"
Original file line number Diff line number Diff 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
104113testcase " create project label" '
105114 OUTPUT=$(GITLAB -v project-label create --project-id $PROJECT_ID \
Original file line number Diff line number Diff line change 462462discussion = 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 \n This 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
466481admin_project .housekeeping ()
467482
You can’t perform that action at this time.
0 commit comments