diff --git a/tools/functional/api/test_merge_requests.py b/tools/functional/api/test_merge_requests.py index c5de5ebc5..80a650cf6 100644 --- a/tools/functional/api/test_merge_requests.py +++ b/tools/functional/api/test_merge_requests.py @@ -1,6 +1,9 @@ +import time + import pytest import gitlab +import gitlab.v4.objects def test_merge_requests(project): @@ -95,3 +98,59 @@ def test_merge_request_merge(project): with pytest.raises(gitlab.GitlabMRClosedError): # Two merge attempts should raise GitlabMRClosedError mr.merge() + + +def test_merge_request_should_remove_source_branch( + project: gitlab.v4.objects.Project, wait_for_sidekiq +): + """Test to ensure https://github.com/python-gitlab/python-gitlab/issues/1120 + is fixed""" + + source_branch = "remove_source_branch" + project.branches.create({"branch": source_branch, "ref": "master"}) + + # NOTE(jlvillal): Must create a commit in the new branch before we can + # create an MR that will work. + project.files.create( + { + "file_path": f"README.{source_branch}", + "branch": source_branch, + "content": "Initial content", + "commit_message": "New commit in new branch", + } + ) + + mr = project.mergerequests.create( + { + "source_branch": source_branch, + "target_branch": "master", + "title": "Should remove source branch", + "remove_source_branch": True, + } + ) + + result = wait_for_sidekiq(timeout=60) + assert result is True, "sidekiq process should have terminated but did not" + + mr_iid = mr.iid + for _ in range(60): + mr = project.mergerequests.get(mr_iid) + if mr.merge_status != "checking": + break + time.sleep(0.5) + assert mr.merge_status != "checking" + + # Ensure we can get the MR branch + project.branches.get(source_branch) + + mr.merge(should_remove_source_branch=True) + + result = wait_for_sidekiq(timeout=60) + assert result is True, "sidekiq process should have terminated but did not" + + # Ensure we can NOT get the MR branch + with pytest.raises(gitlab.exceptions.GitlabGetError): + project.branches.get(source_branch) + + mr = project.mergerequests.get(mr.iid) + assert mr.merged_at is not None # Now is merged