Skip to content
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

Fixes squash merge request for Single Job Mode #369

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 6 additions & 1 deletion marge/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ 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:
auto_squash = (
self._project.squash_enforced or
merge_request.squash
)

if auto_squash and self._options.requests_commit_tagging:
raise CannotMerge(
"Sorry, merging requests marked as auto-squash would ruin my commit tagging!"
)
Expand Down
15 changes: 9 additions & 6 deletions marge/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,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
8 changes: 8 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.get('squash_option', None)

@property
def only_allow_merge_if_pipeline_succeeds(self):
return self.info['only_allow_merge_if_pipeline_succeeds']
Expand All @@ -112,6 +116,10 @@ def access_level(self):
assert effective_access is not None, "GitLab failed to provide user permissions on project"
return AccessLevel(effective_access['access_level'])

@property
def squash_enforced(self):
return self.squash_option == 'always'


# pylint: disable=invalid-name
@unique
Expand Down
3 changes: 3 additions & 0 deletions marge/single_merge_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ def update_merge_request_and_accept(self, approvals):

self.ensure_mergeable_mr(merge_request)

auto_squash = True if target_project.squash_enforced else None
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not True if target_project.squash_enforced else False?

Choose a reason for hiding this comment

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

Again from my previous comment, a user is allowed to choose if he / she wants squash at 3 different places.

  • in the project settings
  • in the merge request settings
  • while merging the merge request

Out of all the case only one case we had to solve for when squash is set to required at project level. In this case we are supposed to pass this extra parameter squash: True which more or less actually seems like a bug to me from gitlab side, remaining cases we are not sending this parameter and letting gitlab take the decision based on it's business logic based on user's input.

Hence a distinction between False & None. It has been sometime since I have worked in python, so not sure if this the best way to write it. But this is intentional, I can add a comment over this line to make it more clear.


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