diff --git a/marge/app.py b/marge/app.py index db40ce1c..6d582f9f 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="marge_bot_batch_merge_job", + help='Branch name when batching is enabled\n', + ) parser.add_argument( '--add-part-of', action='store_true', @@ -351,6 +357,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..47b91a2c 100644 --- a/marge/batch_job.py +++ b/marge/batch_job.py @@ -15,16 +15,16 @@ class CannotBatch(Exception): class BatchMergeJob(MergeJob): - 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) + 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 +32,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 +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, @@ -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) @@ -218,7 +218,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 +243,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 +257,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..c63b5476 100644 --- a/tests/test_batch_job.py +++ b/tests/test_batch_job.py @@ -41,7 +41,8 @@ def get_batch_merge_job(self, api, mocklab, **batch_merge_kwargs): 'project': marge.project.Project.fetch_by_id(project_id, api), 'repo': create_autospec(marge.git.Repo, spec_set=True), 'options': MergeJobOptions.default(), - 'merge_requests': [merge_request] + 'merge_requests': [merge_request], + 'batch_branch_name': 'test_branch_name', } params.update(batch_merge_kwargs) return BatchMergeJob(**params) @@ -51,7 +52,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 +65,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 +87,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 +135,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, )