pythonpython-gitlab

How to update a protected branch in python-gitlab?


I am using python-gitlab to help configure projects. I'm trying to automate going into GitLab Settings > Repository > Protected branches, then for the existing master branch, changing "Allowed to merge" from "Maintainers" to "Developers + Maintainers". Here's a code snippet:

import gitlab
gl = gitlab.Gitlab.from_config()
project = project = gl.projects.get("my-team/my_project")
master_branch = project.protectedbranches.get("master")
print(master_branch.merge_access_levels)

The data type is just is a list of dicts; there doesn't appear to be a way to update the setting like other settings in this API. Even if you just update it:

master_branch.merge_access_levels[0]['access_level'] = 30
project.save()

nothing happens. Is there a way to do this with python-gitlab?


Solution

  • The data type is just is a list of dicts; there doesn't appear to be a way to update the setting like other settings in this API. Even is you just update it:

    master_branch.merge_access_levels[0]['access_level'] = 30
    project.save() 
    

    nothing happens. Is there a way to do this with python-gitlab?

    python-gitlab previous did not support updating protected branches, only creating/deleting them. Support for updating them was recently added, though this is not yet a published release (4.4.0 is the latest release as of this post), though it will likely be the next release, after which this example should work:

    import gitlab
    gl = gitlab.Gitlab.from_config()
    project = gl.projects.get("my-team/my_project")
    protected_master_branch = project.protectedbranches.get("master")
    protected_master_branch.merge_access_levels[0]['access_level'] = 30
    protected_master_branch.save()