GitHub API: Enable Push Restrictions for branch

前端 未结 1 1145
误落风尘
误落风尘 2021-02-10 18:39

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

相关标签:
1条回答
  • 2021-02-10 19:28

    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())
    
    0 讨论(0)
提交回复
热议问题