Skip to content

Commit 0585293

Browse files
committed
Use target branch name in batch merge to allow multiple instances
A project can use multiple instances of marge bots assigned to different target branches. This allows dealing with different release branches in parallel. However batches could not be used on multiple instances because they conflicted on the name of the branch created for the batch. By naming this branch after the target branch, then multiple batch branches can exist.
1 parent ca64531 commit 0585293

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

marge/batch_job.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ def __init__(self, *, api, user, project, repo, options, merge_requests):
2020
super().__init__(api=api, user=user, project=project, repo=repo, options=options)
2121
self._merge_requests = merge_requests
2222

23-
def remove_batch_branch(self):
23+
def remove_batch_branch(self, target_branch):
2424
log.info('Removing local batch branch')
2525
try:
26-
self._repo.remove_branch(BatchMergeJob.BATCH_BRANCH_NAME)
26+
self._repo.remove_branch(f'{BatchMergeJob.BATCH_BRANCH_NAME}/{target_branch}')
2727
except git.GitError:
2828
pass
2929

30-
def close_batch_mr(self):
30+
def close_batch_mr(self, target_branch):
3131
log.info('Closing batch MRs')
3232
params = {
33+
'target_branch': target_branch,
3334
'author_id': self._user.id,
3435
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
3536
'state': 'opened',
@@ -48,7 +49,7 @@ def close_batch_mr(self):
4849
def create_batch_mr(self, target_branch):
4950
log.info('Creating batch MR')
5051
params = {
51-
'source_branch': BatchMergeJob.BATCH_BRANCH_NAME,
52+
'source_branch': f'{BatchMergeJob.BATCH_BRANCH_NAME}/{target_branch}',
5253
'target_branch': target_branch,
5354
'title': 'Marge Bot Batch MR - DO NOT TOUCH',
5455
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
@@ -92,9 +93,9 @@ def get_mergeable_mrs(self, merge_requests):
9293
mergeable_mrs.append(merge_request)
9394
return mergeable_mrs
9495

95-
def push_batch(self):
96+
def push_batch(self, target_branch):
9697
log.info('Pushing batch branch')
97-
self._repo.push(BatchMergeJob.BATCH_BRANCH_NAME, force=True)
98+
self._repo.push(f'{BatchMergeJob.BATCH_BRANCH_NAME}/{target_branch}', force=True)
9899

99100
def ensure_mr_not_changed(self, merge_request):
100101
log.info('Ensuring MR !%s did not change', merge_request.iid)
@@ -181,11 +182,13 @@ def accept_mr(
181182
return final_sha
182183

183184
def execute(self):
184-
# Cleanup previous batch work
185-
self.remove_batch_branch()
186-
self.close_batch_mr()
187185

188186
target_branch = self._merge_requests[0].target_branch
187+
188+
# Cleanup previous batch work
189+
self.remove_batch_branch(target_branch)
190+
self.close_batch_mr(target_branch)
191+
189192
merge_requests = self.get_mrs_with_common_target_branch(target_branch)
190193
merge_requests = self.get_mergeable_mrs(merge_requests)
191194

@@ -201,7 +204,8 @@ def execute(self):
201204
remote_target_branch_sha = self._repo.get_commit_hash('origin/%s' % target_branch)
202205

203206
self._repo.checkout_branch(target_branch, 'origin/%s' % target_branch)
204-
self._repo.checkout_branch(BatchMergeJob.BATCH_BRANCH_NAME, 'origin/%s' % target_branch)
207+
self._repo.checkout_branch(f'{BatchMergeJob.BATCH_BRANCH_NAME}/{target_branch}',
208+
'origin/%s' % target_branch)
205209

206210
working_merge_requests = []
207211

@@ -241,7 +245,7 @@ def execute(self):
241245
raise CannotBatch('not enough ready merge requests')
242246
if self._project.only_allow_merge_if_pipeline_succeeds:
243247
# This switches git to <batch> branch
244-
self.push_batch()
248+
self.push_batch(target_branch)
245249
batch_mr = self.create_batch_mr(
246250
target_branch=target_branch,
247251
)

tests/test_batch_job.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def get_batch_merge_job(self, api, mocklab, **batch_merge_kwargs):
4949
def test_remove_batch_branch(self, api, mocklab):
5050
repo = create_autospec(marge.git.Repo, spec_set=True)
5151
batch_merge_job = self.get_batch_merge_job(api, mocklab, repo=repo)
52-
batch_merge_job.remove_batch_branch()
52+
batch_merge_job.remove_batch_branch('master')
5353
repo.remove_branch.assert_called_once_with(
54-
BatchMergeJob.BATCH_BRANCH_NAME,
54+
f'{BatchMergeJob.BATCH_BRANCH_NAME}/master',
5555
)
5656

5757
def test_close_batch_mr(self, api, mocklab):
@@ -60,9 +60,10 @@ def test_close_batch_mr(self, api, mocklab):
6060
mr_class.search.return_value = [batch_mr]
6161

6262
batch_merge_job = self.get_batch_merge_job(api, mocklab)
63-
batch_merge_job.close_batch_mr()
63+
batch_merge_job.close_batch_mr('master')
6464

6565
params = {
66+
'target_branch': 'master',
6667
'author_id': batch_merge_job._user.id,
6768
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
6869
'state': 'opened',
@@ -86,7 +87,7 @@ def test_create_batch_mr(self, api, mocklab):
8687
r_batch_mr = batch_merge_job.create_batch_mr(target_branch)
8788

8889
params = {
89-
'source_branch': BatchMergeJob.BATCH_BRANCH_NAME,
90+
'source_branch': f'{BatchMergeJob.BATCH_BRANCH_NAME}/{target_branch}',
9091
'target_branch': target_branch,
9192
'title': 'Marge Bot Batch MR - DO NOT TOUCH',
9293
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
@@ -132,9 +133,9 @@ def test_ensure_mergeable_mr_ci_not_ok(self, bmj_get_mr_ci_status, api, mocklab)
132133

133134
def test_push_batch(self, api, mocklab):
134135
batch_merge_job = self.get_batch_merge_job(api, mocklab)
135-
batch_merge_job.push_batch()
136+
batch_merge_job.push_batch('master')
136137
batch_merge_job._repo.push.assert_called_once_with(
137-
BatchMergeJob.BATCH_BRANCH_NAME,
138+
f'{BatchMergeJob.BATCH_BRANCH_NAME}/master',
138139
force=True,
139140
)
140141

0 commit comments

Comments
 (0)