API call merge_request/<iid>/merge returns code 406, but there is no conflict

Summary

My company uses GitLab Enterprise Edition 12.2.5-ee

We wrote a small automation tool that uses the Gitlab API for creating and auto-merging merge requests. The tool gets input from Gitlab webhooks (merge request events).

So what we are doing the following (on some selected gitlab projects):

A merge request is automatically created (using the gitlab api) Then the tool receives a gitlab merge request webhook and invokes a gitlab API call PUT projects//merge_requests//merge. That fails very often, it works only about half of the time.

The problem is that we get back code 406 (meaning merge conflict), but there is no merge conflict.

Invoking the exact same PUT request manually (using curl or postman) works.

On the timeline that looks for example like this: 2020-01-14T09:55:52,654 [qtp1296518442-19] DEBUG com.qitasc.devbot.Devbot - Function createGitlabMergeRequest ....

2020-01-14T09:55:53,823 [qtp1296518442-13] DEBUG com.qitasc.devbot.Devbot - Received by webhook ....

We get back merge request Iid, merge request merge status: unchecked, merge request state: opened 2020-01-14T09:55:53,823 [qtp1296518442-13] DEBUG com.qitasc.devbot.Devbot - gitlab API Call: PUT ....

2020-01-14T09:55:53,867 [qtp1296518442-13] ERROR com.qitasc.devbot.Devbot - return code 406 .....

I assume it is a timing problem, we try to merge the merge request very soon after it was created. I don't want to put sleeps in the program, because it is stateless, it just listens to events and reacts on it.

The workaround is now to simply merge merge request in the GUI. To be clear, of course we should get a return code 406, when there is a merge conflict. But the problem is that we get a return code 406, while the merge request IS mergable without any problems.

Edited by Thiago Presa