@@ -15,24 +15,24 @@ class CannotBatch(Exception):
15
15
16
16
17
17
class BatchMergeJob (MergeJob ):
18
- BATCH_BRANCH_NAME = 'marge_bot_batch_merge_job '
18
+ BATCH_BRANCH_NAME = 'marge_bot_batch_merge_job_ '
19
19
20
20
def __init__ (self , * , api , user , project , repo , options , merge_requests ):
21
21
super ().__init__ (api = api , user = user , project = project , repo = repo , options = options )
22
22
self ._merge_requests = merge_requests
23
23
24
- def remove_batch_branch (self ):
24
+ def remove_batch_branch (self , batch_branch_name ):
25
25
log .info ('Removing local batch branch' )
26
26
try :
27
- self ._repo .remove_branch (BatchMergeJob . BATCH_BRANCH_NAME )
27
+ self ._repo .remove_branch (batch_branch_name )
28
28
except git .GitError :
29
29
pass
30
30
31
- def close_batch_mr (self ):
31
+ def close_batch_mr (self , batch_branch_name ):
32
32
log .info ('Closing batch MRs' )
33
33
params = {
34
34
'author_id' : self ._user .id ,
35
- 'labels' : BatchMergeJob . BATCH_BRANCH_NAME ,
35
+ 'labels' : batch_branch_name ,
36
36
'state' : 'opened' ,
37
37
'order_by' : 'created_at' ,
38
38
'sort' : 'desc' ,
@@ -46,14 +46,14 @@ def close_batch_mr(self):
46
46
log .info ('Closing batch MR !%s' , batch_mr .iid )
47
47
batch_mr .close ()
48
48
49
- def create_batch_mr (self , target_branch ):
50
- self .push_batch ()
49
+ def create_batch_mr (self , target_branch , batch_branch_name ):
50
+ self .push_batch (batch_branch_name )
51
51
log .info ('Creating batch MR' )
52
52
params = {
53
- 'source_branch' : BatchMergeJob . BATCH_BRANCH_NAME ,
53
+ 'source_branch' : batch_branch_name ,
54
54
'target_branch' : target_branch ,
55
55
'title' : 'Marge Bot Batch MR - DO NOT TOUCH' ,
56
- 'labels' : BatchMergeJob . BATCH_BRANCH_NAME ,
56
+ 'labels' : batch_branch_name ,
57
57
}
58
58
batch_mr = MergeRequest .create (
59
59
api = self ._api ,
@@ -94,9 +94,9 @@ def get_mergeable_mrs(self, merge_requests):
94
94
mergeable_mrs .append (merge_request )
95
95
return mergeable_mrs
96
96
97
- def push_batch (self ):
97
+ def push_batch (self , batch_branch_name ):
98
98
log .info ('Pushing batch branch' )
99
- self ._repo .push (BatchMergeJob . BATCH_BRANCH_NAME , force = True )
99
+ self ._repo .push (batch_branch_name , force = True )
100
100
101
101
def ensure_mr_not_changed (self , merge_request ):
102
102
log .info ('Ensuring MR !%s did not change' , merge_request .iid )
@@ -198,11 +198,15 @@ def accept_mr(
198
198
return final_sha
199
199
200
200
def execute (self ):
201
+ # name of batch branch based on the name of target branch to allow parallel merges #267
202
+ target_branch = self ._merge_requests [0 ].target_branch
203
+ batch_branch_name = (BatchMergeJob .BATCH_BRANCH_NAME + target_branch )
204
+ log .debug ("batch: execute: batch_branch_name: %s" , batch_branch_name )
205
+
201
206
# Cleanup previous batch work
202
- self .remove_batch_branch ()
203
- self .close_batch_mr ()
207
+ self .remove_batch_branch (batch_branch_name )
208
+ self .close_batch_mr (batch_branch_name )
204
209
205
- target_branch = self ._merge_requests [0 ].target_branch
206
210
merge_requests = self .get_mrs_with_common_target_branch (target_branch )
207
211
merge_requests = self .get_mergeable_mrs (merge_requests )
208
212
@@ -218,11 +222,9 @@ def execute(self):
218
222
remote_target_branch_sha = self ._repo .get_commit_hash ('origin/%s' % target_branch )
219
223
220
224
self ._repo .checkout_branch (target_branch , 'origin/%s' % target_branch )
221
- self ._repo .checkout_branch (BatchMergeJob . BATCH_BRANCH_NAME , 'origin/%s' % target_branch )
225
+ self ._repo .checkout_branch (batch_branch_name , 'origin/%s' % target_branch )
222
226
223
- batch_mr = self .create_batch_mr (
224
- target_branch = target_branch ,
225
- )
227
+ batch_mr = self .create_batch_mr (target_branch , batch_branch_name )
226
228
batch_mr_sha = batch_mr .sha
227
229
228
230
working_merge_requests = []
@@ -243,7 +245,7 @@ def execute(self):
243
245
)
244
246
# Update <batch> branch with MR changes
245
247
batch_mr_sha = self ._repo .merge (
246
- BatchMergeJob . BATCH_BRANCH_NAME ,
248
+ batch_branch_name ,
247
249
merge_request .source_branch ,
248
250
'-m' ,
249
251
'Batch merge !%s into %s (!%s)' % (
@@ -257,13 +259,13 @@ def execute(self):
257
259
# Update <source_branch> on latest <batch> branch so it contains previous MRs
258
260
self .fuse (
259
261
merge_request .source_branch ,
260
- BatchMergeJob . BATCH_BRANCH_NAME ,
262
+ batch_branch_name ,
261
263
source_repo_url = source_repo_url ,
262
264
local = True ,
263
265
)
264
266
# Update <batch> branch with MR changes
265
267
batch_mr_sha = self ._repo .fast_forward (
266
- BatchMergeJob . BATCH_BRANCH_NAME ,
268
+ batch_branch_name ,
267
269
merge_request .source_branch ,
268
270
local = True ,
269
271
)
@@ -286,7 +288,7 @@ def execute(self):
286
288
raise CannotBatch ('not enough ready merge requests' )
287
289
288
290
# This switches git to <batch> branch
289
- self .push_batch ()
291
+ self .push_batch (batch_branch_name )
290
292
for merge_request in working_merge_requests :
291
293
merge_request .comment ('I will attempt to batch this MR (!{})...' .format (batch_mr .iid ))
292
294
0 commit comments