Skip to content

Fixes squash merge request for Single Job Mode #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion marge/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def opts(self):
def execute(self):
raise NotImplementedError

def is_auto_squash_enabled(self, merge_request):
return self._project.squash_option == "always" or merge_request.squash

def ensure_mergeable_mr(self, merge_request):
merge_request.refetch_info()
log.info('Ensuring MR !%s is mergeable', merge_request.iid)
Expand All @@ -47,7 +50,7 @@ def ensure_mergeable_mr(self, merge_request):
if merge_request.work_in_progress:
raise CannotMerge("Sorry, I can't merge requests marked as Work-In-Progress!")

if merge_request.squash and self._options.requests_commit_tagging:
if self.is_auto_squash_enabled(merge_request) and self._options.requests_commit_tagging:
raise CannotMerge(
"Sorry, merging requests marked as auto-squash would ruin my commit tagging!"
)
Expand Down
18 changes: 12 additions & 6 deletions marge/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ def blocking_discussions_resolved(self):
def force_remove_source_branch(self):
return self.info['force_remove_source_branch']

def auto_squash(self, project):
return project.squash_option == "always" or self.squash

def update_sha(self, sha):
"""record the updated sha. We don't use refetch_info instead as it may hit cache."""
self._info['sha'] = sha
Expand Down Expand Up @@ -200,14 +203,17 @@ def rebase(self):

raise TimeoutError('Waiting for merge request to be rebased by GitLab')

def accept(self, remove_branch=False, sha=None, merge_when_pipeline_succeeds=True):
def accept(self, remove_branch=False, sha=None, merge_when_pipeline_succeeds=True, auto_squash=None):
params = dict(
should_remove_source_branch=remove_branch,
merge_when_pipeline_succeeds=merge_when_pipeline_succeeds,
sha=sha or self.sha, # if provided, ensures what is merged is what we want (or fails)
)
if auto_squash is not None:
params['squash'] = auto_squash
return self._api.call(PUT(
'/projects/{0.project_id}/merge_requests/{0.iid}/merge'.format(self),
dict(
should_remove_source_branch=remove_branch,
merge_when_pipeline_succeeds=merge_when_pipeline_succeeds,
sha=sha or self.sha, # if provided, ensures what is merged is what we want (or fails)
),
params
))

def close(self):
Expand Down
4 changes: 4 additions & 0 deletions marge/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def http_url_to_repo(self):
def merge_requests_enabled(self):
return self.info['merge_requests_enabled']

@property
def squash_option(self):
return self.info['squash_option']

@property
def only_allow_merge_if_pipeline_succeeds(self):
return self.info['only_allow_merge_if_pipeline_succeeds']
Expand Down
5 changes: 5 additions & 0 deletions marge/single_merge_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ def update_merge_request_and_accept(self, approvals):

self.ensure_mergeable_mr(merge_request)

auto_squash = None
if self.is_auto_squash_enabled(merge_request):
auto_squash = True

try:
ret = merge_request.accept(
remove_branch=merge_request.force_remove_source_branch,
sha=actual_sha,
merge_when_pipeline_succeeds=bool(target_project.only_allow_merge_if_pipeline_succeeds),
auto_squash=auto_squash
)
log.info('merge_request.accept result: %s', ret)
except gitlab.NotAcceptable as err:
Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ max-line-length=110
[DESIGN]
max-args=10
max-attributes=15
max-public-methods=35
max-public-methods=40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is possibly something that the project owners will want to look at, but IMO it's fine in this repo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this seems to be reverted in the next commit?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes so there were 2 approaches in which this could have done. Earlier I was merging the project config with the MR config and then taking a decision.

But then I thought that there is only one case we need to address which is when at project level we have sqaush_option: always, so we are not taking any decision on behalf of the user or gitlab, instead we are respecting user's configuration and only sending squash: true when the project level configuration is set to always because in that case even gitlab won't allow the user to change the configuration at MR level.

For the earlier case I had to add a method to merge_request class which was exceeding the number of methods, but in this case we don't have to deal with this class at all, hence reverted.

# Maximum number of locals for function / method body
max-locals=25

Expand Down
1 change: 1 addition & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'default_branch': 'master',
'only_allow_merge_if_pipeline_succeeds': True,
'only_allow_merge_if_all_discussions_are_resolved': False,
'squash_option': 'never',
'permissions': {
'project_access': {
'access_level': AccessLevel.developer.value,
Expand Down