- Notifications
You must be signed in to change notification settings - Fork673
fix:mr.cancel_merge_when_pipeline_succeeds()
#2350
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -165,9 +165,7 @@ class ProjectMergeRequest( | ||
@cli.register_custom_action("ProjectMergeRequest") | ||
@exc.on_http_error(exc.GitlabMROnBuildSuccessError) | ||
def cancel_merge_when_pipeline_succeeds(self, **kwargs: Any) -> Dict[str, str]: | ||
JohnVillalovos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"""Cancel merge when the pipeline succeeds. | ||
Args: | ||
@@ -179,17 +177,20 @@ def cancel_merge_when_pipeline_succeeds( | ||
request | ||
Returns: | ||
dict of the parsed json returned by the server | ||
""" | ||
path = ( | ||
f"{self.manager.path}/{self.encoded_id}/cancel_merge_when_pipeline_succeeds" | ||
) | ||
server_data = self.manager.gitlab.http_post(path, **kwargs) | ||
# 2022-10-30: The docs at | ||
# https://docs.gitlab.com/ee/api/merge_requests.html#cancel-merge-when-pipeline-succeeds | ||
# are incorrect in that the return value is actually just: | ||
# {'status': 'success'} for a successful cancel. | ||
if TYPE_CHECKING: | ||
assert isinstance(server_data, dict) | ||
return server_data | ||
@cli.register_custom_action("ProjectMergeRequest") | ||
@exc.on_http_error(exc.GitlabListError) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -181,11 +181,27 @@ def test_merge_request_reset_approvals(gitlab_url, project, wait_for_sidekiq): | ||
assert mr.reset_approvals() | ||
def test_cancel_merge_when_pipeline_succeeds(project, merge_request, wait_for_sidekiq): | ||
mr = merge_request(source_branch="test_merge_request_merge", create_pipeline=True) | ||
# Set to merge when the pipeline succeeds, which should never happen | ||
mr.merge(merge_when_pipeline_succeeds=True) | ||
wait_for_sidekiq(timeout=60) | ||
mr = project.mergerequests.get(mr.iid) | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
assert mr.merged_at is None | ||
assert mr.merge_when_pipeline_succeeds is True | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
cancel = mr.cancel_merge_when_pipeline_succeeds() | ||
assert cancel == {"status": "success"} | ||
def test_merge_request_merge(project, merge_request, wait_for_sidekiq): | ||
mr = merge_request(source_branch="test_merge_request_merge") | ||
mr.merge() | ||
wait_for_sidekiq(timeout=60) | ||
mr = project.mergerequests.get(mr.iid) | ||
assert mr.merged_at is not None | ||
assert mr.merge_when_pipeline_succeeds is False | ||
with pytest.raises(gitlab.GitlabMRClosedError): | ||
# Two merge attempts should raise GitlabMRClosedError | ||
mr.merge() | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -379,7 +379,7 @@ def merge_request(project, wait_for_sidekiq): | ||
to_delete = [] | ||
def _merge_request(*, source_branch: str, create_pipeline: bool = False): | ||
# Wait for processes to be done before we start... | ||
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server | ||
# Error". Hoping that waiting until all other processes are done will | ||
@@ -401,6 +401,21 @@ def _merge_request(*, source_branch: str): | ||
"commit_message": "New commit in new branch", | ||
} | ||
) | ||
if create_pipeline: | ||
project.files.create( | ||
{ | ||
"file_path": ".gitlab-ci.yml", | ||
"branch": source_branch, | ||
"content": """ | ||
test: | ||
rules: | ||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"' | ||
script: | ||
- sleep 24h # We don't expect this to finish | ||
""", | ||
"commit_message": "Add a simple pipeline", | ||
} | ||
) | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
mr = project.mergerequests.create( | ||
{ | ||
"source_branch": source_branch, | ||