Skip to content

Commit

Permalink
Support custom batch_branch_name from parameter
Browse files Browse the repository at this point in the history
If nothing is set, we use old default branch name
  • Loading branch information
nanonyme committed Dec 16, 2022
1 parent d3c4409 commit 83b08ae
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
9 changes: 9 additions & 0 deletions marge/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
)

Expand Down
26 changes: 15 additions & 11 deletions marge/batch_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ 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

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 +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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -243,7 +247,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 +261,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
4 changes: 3 additions & 1 deletion marge/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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


Expand Down
10 changes: 5 additions & 5 deletions tests/test_batch_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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',
Expand All @@ -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,
Expand Down Expand Up @@ -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,
)

Expand Down

0 comments on commit 83b08ae

Please sign in to comment.