From 83002e072589dcdac9237a8e4095da2a2904da62 Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@garz-fricke.com> Date: Wed, 1 Jun 2022 15:54:11 +0200 Subject: [PATCH] accept_merge_request: explicitly check if MR has merge conflicts According to the documentation [1] we should get a 406 error from the API if we try to merge a merge request which has a merge conflict. We are experiencing a different behaviour, though, where we are getting a 405 error in this case. There is an open GitLab issue on this topic [2]. In our error handling we assumed that the reason for a 405 error is most likely a required pipeline which is still running. This assumtion, however, leads to wrong behaviour in cases where no pipeline is started at all. Fixing this now by explicitly checking for merge conflicts at the very beginning of the error handling sequence. [1]: https://docs.gitlab.com/ee/api/merge_requests.html#merge-a-merge-request [2]: https://gitlab.com/gitlab-org/gitlab/-/issues/364102 --- accept_merge_request.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/accept_merge_request.py b/accept_merge_request.py index 7500bbe..4e35baf 100755 --- a/accept_merge_request.py +++ b/accept_merge_request.py @@ -56,6 +56,17 @@ def accept_merge_request(project, mr, rebase=False, should_remove_source_branch= if e.response_code == 405: # Not allowed (draft, closed, pipeline pending or failed) + # Contrary to the documentation, this response is also issued when the + # merge failed due to a merge conflict. See GitLab issue: + # https://gitlab.com/gitlab-org/gitlab/-/issues/364102 + + if mr.has_conflicts: + # Merge conflict, automatic rebase not possible + if pipeline_pending: + print("") + print("Merge not possible, has to be rebased manually") + return False, mr.sha + # If pipeline is running, wait for completion if not mr.head_pipeline: # No pipeline created yet @@ -69,10 +80,10 @@ def accept_merge_request(project, mr, rebase=False, should_remove_source_branch= print(".", end="", flush=True) time.sleep(1) else: - # Merge conflict, automatic rebase not possible + # Merge failed due to some other reason if pipeline_pending: print("") - print("Merge not possible, has to be rebased manually") + print("Merge not possible for unkown reason") return False, mr.sha elif e.response_code == 406: -- GitLab