GitHub API: Enable Push Restrictions for branch

与世无争的帅哥 提交于 2019-12-23 03:42:21

问题


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 restrictions from a branch and later enable them with specific teams.

Replacing/adding existing team restrictions works via

PUT/POST /repos/:owner/:repo/branches/:branch/protection/restrictions/teams

And removing push restrictions also works like a charm using

DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions

But, if I remove the push restrictions, I have found no way how to enable it back again to add specific teams. If I try to add or replace teams, the message says 'Push restrictions not enabled'.

So how can I enable the checkbox Restrict who can push to this branch to add teams in a script? See screenshot for the desired outcome: Push Restrictions

The API documentation just presents me the options Get restrictions of protected branch and Remove restrictions of protected branch.

What I tried so far:

  • Just removing all teams without removing the restrictions does not work, because then nobody is able to push.
  • Sending PUT/POST to /repos/:owner/:repo/branches/:branch/protection/restrictions gives a 404.
  • Right now I have no other way than clicking the checkbox manually, then adding and replacing works via API.

回答1:


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())


来源:https://stackoverflow.com/questions/51020398/github-api-enable-push-restrictions-for-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!