Skip to content

Commit

Permalink
feat: enable multiple target branches for batching MRs (smarkets#267)
Browse files Browse the repository at this point in the history
In order to support batch jobs in parallel, the batch branches
need to be tied to the target branch name.

Signed-off-by: Sascha Binckly <[email protected]>
  • Loading branch information
sg70 authored and Sven Geisler committed Mar 30, 2022
1 parent 70ab3c8 commit f16d589
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions marge/batch_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def __init__(self, *, api, user, project, repo, options, merge_requests):
def remove_batch_branch(self):
log.info('Removing local batch branch')
try:
self._repo.remove_branch(BatchMergeJob.BATCH_BRANCH_NAME)
self._repo.remove_branch(self._batch_branch_name)
except git.GitError:
pass

def close_batch_mr(self):
log.info('Closing batch MRs')
params = {
'author_id': self._user.id,
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
'labels': self._batch_branch_name,
'state': 'opened',
'order_by': 'created_at',
'sort': 'desc',
Expand All @@ -50,10 +50,10 @@ def create_batch_mr(self, target_branch):
self.push_batch()
log.info('Creating batch MR')
params = {
'source_branch': BatchMergeJob.BATCH_BRANCH_NAME,
'source_branch': self._batch_branch_name,
'target_branch': target_branch,
'title': 'Marge Bot Batch MR - DO NOT TOUCH',
'labels': BatchMergeJob.BATCH_BRANCH_NAME,
'labels': self._batch_branch_name,
}
batch_mr = MergeRequest.create(
api=self._api,
Expand Down Expand Up @@ -96,7 +96,7 @@ def get_mergeable_mrs(self, merge_requests):

def push_batch(self):
log.info('Pushing batch branch')
self._repo.push(BatchMergeJob.BATCH_BRANCH_NAME, force=True)
self._repo.push(self._batch_branch_name, force=True)

def ensure_mr_not_changed(self, merge_request):
log.info('Ensuring MR !%s did not change', merge_request.iid)
Expand Down Expand Up @@ -198,6 +198,11 @@ def accept_mr(
return final_sha

def execute(self):
self._batch_branch_name = (BatchMergeJob.BATCH_BRANCH_NAME + "_" +
self._merge_requests[0].target_branch)

log.debug("batch: execute: _batch_branch_name: %s", self._batch_branch_name)

# Cleanup previous batch work
self.remove_batch_branch()
self.close_batch_mr()
Expand All @@ -218,7 +223,7 @@ def execute(self):
remote_target_branch_sha = self._repo.get_commit_hash('origin/%s' % target_branch)

self._repo.checkout_branch(target_branch, 'origin/%s' % target_branch)
self._repo.checkout_branch(BatchMergeJob.BATCH_BRANCH_NAME, 'origin/%s' % target_branch)
self._repo.checkout_branch(self._batch_branch_name, 'origin/%s' % target_branch)

batch_mr = self.create_batch_mr(
target_branch=target_branch,
Expand All @@ -243,7 +248,7 @@ def execute(self):
)
# Update <batch> branch with MR changes
batch_mr_sha = self._repo.merge(
BatchMergeJob.BATCH_BRANCH_NAME,
self._batch_branch_name,
merge_request.source_branch,
'-m',
'Batch merge !%s into %s (!%s)' % (
Expand All @@ -257,13 +262,13 @@ def execute(self):
# Update <source_branch> on latest <batch> branch so it contains previous MRs
self.fuse(
merge_request.source_branch,
BatchMergeJob.BATCH_BRANCH_NAME,
self._batch_branch_name,
source_repo_url=source_repo_url,
local=True,
)
# Update <batch> branch with MR changes
batch_mr_sha = self._repo.fast_forward(
BatchMergeJob.BATCH_BRANCH_NAME,
self._batch_branch_name,
merge_request.source_branch,
local=True,
)
Expand Down

0 comments on commit f16d589

Please sign in to comment.