405 error when trying to merge MR via API

Problem to solve

I create a merge request from my CI job using the REST API. This works correctly and I get a response containing the MR’s iid.

I then try to use the (PUT) API to set the merge_when_pipeline_succeeds=true flag for this MR. This works when I use my own personal access token, but if I use a project access token (with the same privileges), it fails with a 405 Method not allowed error.

The same token works for creating the merge request and for fetching merge request details.

Steps to reproduce

I followed instructions from Merge requests API | GitLab and made the following request:

curl --header "PRIVATE-TOKEN: glpat-********" -v -X PUT "$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$MR_IID/merge?merge_when_pipeline_succeeds=true"

This works when I provide my own PRIVATE_TOKEN but does not work when I provide a project access token.

I’ve verified that the user associated with the project access token does have permissions to merge to the target branch.

Configuration

NA

Versions

Please select whether options apply, and add the version information.

  • Self-managed
  • GitLab.com SaaS
  • Dedicated
  • Self-hosted Runners

Versions

  • GitLab (Web: /help or self-managed system information sudo gitlab-rake gitlab:env:info):
  • GitLab Runner, if self-hosted (Web /admin/runners or CLI gitlab-runner --version):

Hi, any resolution on this? Same exact issue.

In my case issue was that pipeline was not yet created when I sent PUT request to merge API, when I added check if pipeline started and then merged a merge request, it worked.