Change branch protection through API

Summary

When a branch is already protected with a certain setting (e.g. Developers can push to master), I don't see a way in the API docs to change those settings (e.g. to require Maintainer-level access for pushing to master). It would be great if that were added.

When I tried posting with the new protection settings I wanted, I got the response <Response [409]> - {'message': "Protected branch 'master' already exists"}.

Steps to reproduce

  1. Create a protected branch with initial settings using the GitLab API.
  2. Attempt to update the protected branch settings using the GitLab API.
  3. Observe the error response indicating the branch already exists.

Example:

# Create Protected Branch
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --header "Content-Type: application/json" \
--data '{
  "name": "master",
  "push_access_level": 40,
  "merge_access_level": 30
}' "https://gitlab.example.com/api/v4/projects/<your_project_id>/protected_branches"

# Attempt to Update Protected Branch
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --header "Content-Type: application/json" \
--data '{
  "name": "master",
  "push_access_level": 40,
  "merge_access_level": 30,
  "allow_force_push": true
}' "https://gitlab.example.com/api/v4/projects/<your_project_id>/protected_branches"

What is the current bug behavior?

Attempting to update the existing protected branch results in the following error message:

``

What is the expected correct behavior?

The API should allow updating the settings of an existing protected branch without requiring it to be deleted and re-added.

Relevant logs and/or screenshots

image.png

Results of GitLab environment info

Expand for output related to GitLab environment info

image

Possible fixes

Provide a direct method to update existing protected branch settings via the API, without requiring deletion and re-adding.

Edited by Arihant