How to toggle "merge when pipeline succeeds" via GitLab API

Hi All,

Actually my first post on the forums, so please let me know if something is not ok.

so here goes :

I would like to set the “Merge when pipeline succeeds” flag while a pipeline is running (imagine it being the last step after all build / test has been completed). The method of doing so would be via the Gitlab API (V3/V4).

what i want is to flag this “button” in the interface of a Merge Request:

This is the same button which transforms into a green “Accept Merge Request” button when a pipeline succeeds (screenshot has been made afterwards to avoind confusion):

Furthermore i have set the follwing setting in general Settings according to the documentation:

nRICr

According to the Gitlab API documentation I should use the following endpoint in for flagging this feature:

PUT /projects/:id/merge_requests/:merge_request_iid/merge

when using the parameter ?merge_when_pipleline_succeeds=true it should flag the button.

However when i call this endpoint v(ia a powershelll wrapper) when the pipeline is still running (i built in a wait for 10 mins while testing this) i get the following result:

i am getting a Method Not Allowed. My assumption is that the endpoint i am using is correct because otherwise i would’ve gotten a bad request / not found return code.

when checking the gitlab merge request i am seeing that indeed the flag is not set to true:

However, when i manually click the blue button the mergerequest looks like this:

Also if i let the pipeline finish and then proceed to call the merge api (w/ or w/o the merge when pipeline succeeds flag) it will accept the merge. It just does not work when the pipeline is running (which is odd because even the button itself only shows when the pipeline is running)

so i am wondering what I am doing wrong here.

I am using the same credentials for the API w/ a personal access token to authenticate to the API. Other functionality of the API work with this token like creating merge requests, retrieving status of a current MR and accepting a MR when the pipeline is finished. The MR for which i want to flag this feature was also made via the API with the same token / credentials.

I have tried the following variants :

  • Use the V3 api with merge_when_build_succeeds=true --> nets the same result
  • Uncheck the “Only allow merge request to be merged if the pipeline succeeds” --> nets the same result
  • Use ID of the merge request instead of IID
  • use /merge_when_pipeline_succeeds instead of ?merge_when_pipeline_succeeds=true
  • use True instead of true --> nets the same result

So i’m a bit lost on why the API gives me this result even though other features appear to be executing as normal.

Thoughts / suggestions ? also if you feel i missed out on information let me know. I’m happy to provide more if needed.

Have the same issue, was you successful with it?

Hi Victor,

I am still facing the same issues. No luck here. Also posted a topic on Stackoverflow but no replies there either.

If you find a solution obviously i am interested in it.

Regards,

Tobi

Ok, sure, I will let you know

Did you figure out a solution for this? I’m having exactly the same issue. One would expect the api provides at least the same functionality as pushing the “merge when pipeline succeeds” button.

any updates on this? i use this to auto release an entire stack and it only works sometimes. would love to have this working

Any update? e are waiting for a fix…

Waiting for a fix on this too. Anyone find a workaround?

Hi,
I just faced the same issue.
You can activate automerge by passing “auto_merge_strategy”: “merge_when_pipeline_succeeds”
to when you call the api.
Here is sample of shell code I use:

mr_data="{
“id”: {pid}, \"merge_request_iid\": {mriid},
“should_remove_source_branch”: true,
“merge_when_pipeline_succeeds”: true,
“auto_merge_strategy”: “merge_when_pipeline_succeeds”
}"

accept_mr_url="$GITLAB_API/projects/$pid/merge_requests/$mriid/merge"

curl -s --request PUT --header “PRIVATE-TOKEN: $GITLAB_TOKEN” --header “Content-Type: application/json” “accept_mr_url" --data "{mr_data}” | jq ‘.message’ 2> /dev/null | sed -e ‘s/"//g’)"

Be careful: auto_merge_strategy is not documented in api docs.
Kind regards

2 Likes

Thanks @glennie i’m gonna use that too… I tried to add the info into the doc but I did not find if I can do it by myself so I created issue feat: Update API documentation on MR (auto_merge_strategy) (#354739) · Issues · GitLab.org / GitLab · GitLab for the doc update.

EDIT: However seems I cannot get it work with python-gitlab T.T Still not toggled with

                    if merge_when_pipeline_succeeds:
                        merge_dict_data = {
                            "auto_merge_strategy": "merge_when_pipeline_succeeds"
                        }
                    else:
                        merge_dict_data = {}
                    mr.merge(
                        should_remove_source_branch=should_remove_source_branch,
                        **merge_dict_data,
                    )

(Also tried to use with direct arg without success…).