smarkets / marge-bot

A merge-bot for GitLab
BSD 3-Clause "New" or "Revised" License
693 stars 136 forks source link

"Branch cannot be merged" still happening #353

Open willemv opened 1 year ago

willemv commented 1 year ago

I think I found the root cause for #263 and #286

The relevant log extract:

2022-10-19 14:10:02,794 INFO Got 1 requests to merge;
2022-10-19 14:10:02,794 INFO Attempting to merge the oldest MR...
2022-10-19 14:10:02,795 INFO Processing !1501 - '<redacted commit message>'
2022-10-19 14:10:03,000 INFO Ensuring MR !1501 is mergeable
2022-10-19 14:10:03,171 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w fetch --prune origin
2022-10-19 14:10:04,741 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w checkout -B redacted_branch_name origin/redacted_branch_name --
2022-10-19 14:10:04,892 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w rebase origin/main
2022-10-19 14:10:05,381 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w rev-parse HEAD
2022-10-19 14:10:05,387 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w rev-parse origin/main
2022-10-19 14:10:05,392 INFO Adding trailers for MR !1501
2022-10-19 14:10:05,393 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w checkout redacted_branch_name --
2022-10-19 14:10:05,556 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w diff-index --quiet HEAD
2022-10-19 14:10:05,636 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w ls-files --others
2022-10-19 14:10:06,108 INFO Running git -C /tmpvy7cgfnc/tmpjqx7pb1w push --force origin redacted_branch_name:redacted_branch_name
2022-10-19 14:10:07,677 INFO Commit id to merge 'cafebabe12345678cafebabe12345678cafebabe' into: 'deadbeef987654321deadbeef987654321deadbe' (updated sha: 'cafebabe12345678cafebabe12345678cafebabe')
2022-10-19 14:10:12,789 INFO Waiting for CI to pass for MR !1501
2022-10-19 14:10:12,896 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:10:12,896 WARNING Suspicious CI status: None
2022-10-19 14:10:23,013 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:10:23,013 WARNING Suspicious CI status: None
2022-10-19 14:10:33,151 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:10:33,151 WARNING Suspicious CI status: None
2022-10-19 14:10:43,301 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:10:43,302 WARNING Suspicious CI status: None
2022-10-19 14:10:53,416 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:10:53,416 WARNING Suspicious CI status: None
2022-10-19 14:11:03,563 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:03,563 WARNING Suspicious CI status: None
2022-10-19 14:11:13,682 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:13,683 WARNING Suspicious CI status: None
2022-10-19 14:11:23,800 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:23,800 WARNING Suspicious CI status: None
2022-10-19 14:11:33,927 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:33,927 WARNING Suspicious CI status: None
2022-10-19 14:11:44,063 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:44,063 WARNING Suspicious CI status: None
2022-10-19 14:11:54,166 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:11:54,166 WARNING Suspicious CI status: None
2022-10-19 14:12:04,272 WARNING No pipeline listed for cafebabe12345678cafebabe12345678cafebabe on branch redacted_branch_name
2022-10-19 14:12:04,272 WARNING Suspicious CI status: None
2022-10-19 14:23:20,161 INFO CI for MR !1501 passed
2022-10-19 14:23:22,163 INFO Waiting for MR !1501 to have merge_status can_be_merged
2022-10-19 14:23:22,338 INFO MR !1501 can be merged on attempt 0
2022-10-19 14:23:22,479 INFO Ensuring MR !1501 is mergeable
2022-10-19 14:23:22,629 ERROR Unanticipated ApiError from GitLab on merge attempt
Traceback (most recent call last):
  File "/nix/store/p3p2psjgggn4n854d5zkmwkz9rx0k2dl-python3.6-marge-0.10.1/lib/python3.6/site-packages/marge/single_merge_job.py", line 99, in update_merge_request_and_accept
    merge_when_pipeline_succeeds=bool(target_project.only_allow_merge_if_pipeline_succeeds),
  File "/nix/store/p3p2psjgggn4n854d5zkmwkz9rx0k2dl-python3.6-marge-0.10.1/lib/python3.6/site-packages/marge/merge_request.py", line 209, in accept
    sha=sha or self.sha,  # if provided, ensures what is merged is what we want (or fails)
  File "/nix/store/p3p2psjgggn4n854d5zkmwkz9rx0k2dl-python3.6-marge-0.10.1/lib/python3.6/site-packages/marge/gitlab.py", line 65, in call
    raise error(response.status_code, err_message)
marge.gitlab.Unprocessable: (422, {'message': 'Branch cannot be merged'})
2022-10-19 14:23:22,629 WARNING I couldn't merge this branch: had some issue with GitLab, check my logs...
2022-10-19 14:23:22,629 INFO Unassigning from MR !1501

So, from the logfile, the timeline looks like this:

  1. 14:10 : marge bot sees MR 1501 and that it needs to be rebased, so rebases and pushes
  2. 14:10 : starts waiting for pipeline info
  3. 14:12 : picks up pipeline info
  4. 14:23 : notices that pipeline completed for this MR (1501) completed
  5. 14:23 : waits for the merge_status of the MR to be can_be_merged, which it is immediately
  6. 14:23 : ensures that MR 1501 is mergeable
  7. 14:23 : decides to that the MR is mergeable and calls the ../merge API endpoint

However: outside of margebots control, another MR is also merged in between 14:12 and 14:23 14:16 : another MR is merged outside of marge-bots control (1499)

So the problem seems to be step 5 : checking the merge_status to see whether the merge can proceed. The problem is that merge_status is usually can_be_merged immediately. The can_be_merged value simply indicates that there will be no conflicts on the merge/rebase.

So what you really want to check is whether the MR needs to be rebased again.

You can request that information by adding the include_diverged_commits_count=true query parameter when you refresh the merge request info. When you do, the merge request information will include a field diverged_commits_count which will be different from 0 in case the MR needs to be rebased again.

willemv commented 1 year ago

Note: I'm assuming that only fast-forward merges are allowed in the gitlab project here. I haven't investigated what happens if you allow merge commits.