From 83b08aefcb3ee5a0cd49adf8f99a8a09174d0100 Mon Sep 17 00:00:00 2001 From: Seppo Yli-Olli Date: Fri, 16 Dec 2022 21:05:27 +0200 Subject: [PATCH] Support custom batch_branch_name from parameter If nothing is set, we use old default branch name --- marge/app.py | 9 +++++++++ marge/batch_job.py | 26 +++++++++++++++----------- marge/bot.py | 4 +++- tests/test_batch_job.py | 10 +++++----- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/marge/app.py b/marge/app.py index db40ce1c..55165d21 100644 --- a/marge/app.py +++ b/marge/app.py @@ -137,6 +137,12 @@ def regexp(str_regex): action='store_true', help='Enable processing MRs in batches\n', ) + parser.add_argument( + '--batch-branch-name', + type=str, + default=None, + help='Branch name when batching is enabled\n', + ) parser.add_argument( '--add-part-of', action='store_true', @@ -249,6 +255,8 @@ def regexp(str_regex): raise MargeBotCliArgError('--use-merge-strategy and --batch are currently mutually exclusive') if config.use_merge_strategy and config.add_tested: raise MargeBotCliArgError('--use-merge-strategy and --add-tested are currently mutually exclusive') + if config.batch_branch_name and not config.batch: + raise MargeBotCliArgError('Setting --batch-branch-name without enabling --batch is not supported') if config.rebase_remotely: conflicting_flag = [ '--use-merge-strategy', @@ -351,6 +359,7 @@ def main(args=None): guarantee_final_pipeline=options.guarantee_final_pipeline, ), batch=options.batch, + batch_branch_name=options.batch_branch_name cli=options.cli, ) diff --git a/marge/batch_job.py b/marge/batch_job.py index faf6b55f..24010a92 100644 --- a/marge/batch_job.py +++ b/marge/batch_job.py @@ -15,16 +15,20 @@ class CannotBatch(Exception): class BatchMergeJob(MergeJob): - BATCH_BRANCH_NAME = 'marge_bot_batch_merge_job' + DEFAULT_BATCH_BRANCH_NAME = 'marge_bot_batch_merge_job' - def __init__(self, *, api, user, project, repo, options, merge_requests): + def __init__(self, *, api, user, project, repo, options, merge_requests, batch_branch_name): super().__init__(api=api, user=user, project=project, repo=repo, options=options) + if not batch_branch_name: + self.batch_branch_name = DEFAULT_BATCH_BRANCH_NAME + else: + self.batch_branch_name = batch_branch_name self._merge_requests = 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 @@ -32,7 +36,7 @@ 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', @@ -50,10 +54,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, @@ -96,7 +100,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) @@ -218,7 +222,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, @@ -243,7 +247,7 @@ def execute(self): ) # Update 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)' % ( @@ -257,13 +261,13 @@ def execute(self): # Update on latest 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 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, ) diff --git a/marge/bot.py b/marge/bot.py index 465e960d..b209d98e 100644 --- a/marge/bot.py +++ b/marge/bot.py @@ -167,6 +167,7 @@ def _process_merge_requests(self, repo_manager, project, merge_requests): merge_requests=merge_requests, repo=repo, options=self._config.merge_opts, + batch_branch_name=self._options.batch_branch_name, ) try: batch_merge_job.execute() @@ -199,7 +200,8 @@ def _get_single_job(self, project, merge_request, repo, options): class BotConfig(namedtuple('BotConfig', 'user use_https auth_token ssh_key_file project_regexp merge_order merge_opts ' + - 'git_timeout git_reference_repo branch_regexp source_branch_regexp batch cli')): + 'git_timeout git_reference_repo branch_regexp source_branch_regexp batch cli ' + + 'batch_branch_name')): pass diff --git a/tests/test_batch_job.py b/tests/test_batch_job.py index 4ac2c5bc..12081116 100644 --- a/tests/test_batch_job.py +++ b/tests/test_batch_job.py @@ -51,7 +51,7 @@ def test_remove_batch_branch(self, api, mocklab): batch_merge_job = self.get_batch_merge_job(api, mocklab, repo=repo) batch_merge_job.remove_batch_branch() repo.remove_branch.assert_called_once_with( - BatchMergeJob.BATCH_BRANCH_NAME, + batch_merge_job.batch_branch_name, ) def test_close_batch_mr(self, api, mocklab): @@ -64,7 +64,7 @@ def test_close_batch_mr(self, api, mocklab): params = { 'author_id': batch_merge_job._user.id, - 'labels': BatchMergeJob.BATCH_BRANCH_NAME, + 'labels': batch_merge_job.batch_branch_name, 'state': 'opened', 'order_by': 'created_at', 'sort': 'desc', @@ -86,10 +86,10 @@ def test_create_batch_mr(self, api, mocklab): r_batch_mr = batch_merge_job.create_batch_mr(target_branch) params = { - 'source_branch': BatchMergeJob.BATCH_BRANCH_NAME, + 'source_branch': batch_merge_job.batch_branch_name, 'target_branch': target_branch, 'title': 'Marge Bot Batch MR - DO NOT TOUCH', - 'labels': BatchMergeJob.BATCH_BRANCH_NAME, + 'labels': batch_merge_job.batch_branch_name, } mr_class.create.assert_called_once_with( api=ANY, @@ -134,7 +134,7 @@ def test_push_batch(self, api, mocklab): batch_merge_job = self.get_batch_merge_job(api, mocklab) batch_merge_job.push_batch() batch_merge_job._repo.push.assert_called_once_with( - BatchMergeJob.BATCH_BRANCH_NAME, + batch_merge_job.batch_branch_name, force=True, )