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 May 19, 2023
1 parent d3c4409 commit 5881e4e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
7 changes: 7 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="marge_bot_batch_merge_job",
help='Branch name when batching is enabled\n',
)
parser.add_argument(
'--add-part-of',
action='store_true',
Expand Down Expand Up @@ -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,
)

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

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

Expand Down

0 comments on commit 5881e4e

Please sign in to comment.