I am trying to disable and enable branch protections for a GitHub project in a Python script using the GitHub API (Version 2.11). More specifically I want to remove all push res
Check Update branch protection section of Github API Rest :
PUT /repos/:owner/:repo/branches/:branch/protection
Using bash & curl :
ownerWithRepo="MyOrg/my-repo"
branch="master"
curl -X PUT \
-H 'Accept: application/vnd.github.luke-cage-preview+json' \
-H 'Authorization: Token YourToken' \
-d '{
"restrictions": {
"users": [
"bertrandmartel"
],
"teams": [
"my-team"
]
},
"required_status_checks": null,
"enforce_admins": null,
"required_pull_request_reviews": null
}' "https://api.github.com/repos/$ownerWithRepo/branches/$branch/protection"
Note that setting null
to one of those fields will disable(uncheck) the feature
In python :
import requests
repo = 'MyOrg/my-repo'
branch = 'master'
access_token = 'YourToken'
r = requests.put(
'https://api.github.com/repos/{0}/branches/{1}/protection'.format(repo, branch),
headers = {
'Accept': 'application/vnd.github.luke-cage-preview+json',
'Authorization': 'Token {0}'.format(access_token)
},
json = {
"restrictions": {
"users": [
"bertrandmartel"
],
"teams": [
"my-team"
]
},
"required_status_checks": None,
"enforce_admins": None,
"required_pull_request_reviews": None
}
)
print(r.status_code)
print(r.json())