|
| 1 | +import time |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 |
|
3 | 5 | import gitlab
|
| 6 | +import gitlab.v4.objects |
4 | 7 |
|
5 | 8 |
|
6 | 9 | def test_merge_requests(project):
|
@@ -95,3 +98,59 @@ def test_merge_request_merge(project):
|
95 | 98 | with pytest.raises(gitlab.GitlabMRClosedError):
|
96 | 99 | # Two merge attempts should raise GitlabMRClosedError
|
97 | 100 | mr.merge()
|
| 101 | + |
| 102 | + |
| 103 | +def test_merge_request_should_remove_source_branch( |
| 104 | + project: gitlab.v4.objects.Project, wait_for_sidekiq |
| 105 | +): |
| 106 | + """Test to ensure https://github.com/python-gitlab/python-gitlab/issues/1120 |
| 107 | + is fixed""" |
| 108 | + |
| 109 | + source_branch = "remove_source_branch" |
| 110 | + project.branches.create({"branch": source_branch, "ref": "master"}) |
| 111 | + |
| 112 | + # NOTE(jlvillal): Must create a commit in the new branch before we can |
| 113 | + # create an MR that will work. |
| 114 | + project.files.create( |
| 115 | + { |
| 116 | + "file_path": f"README.{source_branch}", |
| 117 | + "branch": source_branch, |
| 118 | + "content": "Initial content", |
| 119 | + "commit_message": "New commit in new branch", |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + mr = project.mergerequests.create( |
| 124 | + { |
| 125 | + "source_branch": source_branch, |
| 126 | + "target_branch": "master", |
| 127 | + "title": "Should remove source branch", |
| 128 | + "remove_source_branch": True, |
| 129 | + } |
| 130 | + ) |
| 131 | + |
| 132 | + result = wait_for_sidekiq(timeout=60) |
| 133 | + assert result is True, "sidekiq process should have terminated but did not" |
| 134 | + |
| 135 | + mr_iid = mr.iid |
| 136 | + for _ in range(60): |
| 137 | + mr = project.mergerequests.get(mr_iid) |
| 138 | + if mr.merge_status != "checking": |
| 139 | + break |
| 140 | + time.sleep(0.5) |
| 141 | + assert mr.merge_status != "checking" |
| 142 | + |
| 143 | + # Ensure we can get the MR branch |
| 144 | + project.branches.get(source_branch) |
| 145 | + |
| 146 | + mr.merge(should_remove_source_branch=True) |
| 147 | + |
| 148 | + result = wait_for_sidekiq(timeout=60) |
| 149 | + assert result is True, "sidekiq process should have terminated but did not" |
| 150 | + |
| 151 | + # Ensure we can NOT get the MR branch |
| 152 | + with pytest.raises(gitlab.exceptions.GitlabGetError): |
| 153 | + project.branches.get(source_branch) |
| 154 | + |
| 155 | + mr = project.mergerequests.get(mr.iid) |
| 156 | + assert mr.merged_at is not None # Now is merged |
0 commit comments