python-gitlab

Python-GitLab Unprotect master


In the previous version of Python-GitLab after creating the project and adding a user as a developer, we have been using:

# Unprotect master
branch = project.branches.get('master')
branch.unprotect()

as we wish to allow developer access to the master.

unprotect is no longer available in version 3.2.0 can anyone guide me on how to achieve the same result using the new version

Thank you


Solution

  • You need to use the Protected Branches API (python-gitlab docs) for this.

    protected_branch = project.protectedbranches.get("master")
    protected_branch.delete()
    
    # or simply
    project.protectedbranches.delete("master")
    

    Note: the delete() method in this case simply unprotects it, it does not delete the branch.

    To re-protect it, you can then create it again. You can also control access levels directly:

    protected_branch = project.protectedbranches.create({
        'name': 'master',
        'merge_access_level': gitlab.const.DEVELOPER_ACCESS,
        'push_access_level': gitlab.const.MAINTAINER_ACCESS
    })