我正在尝试使用 GitHub API(版本 2.11)在 Python 脚本中为 GitHub 项目禁用和启用分支保护。更具体地说,我想从分支中删除所有推送限制,然后在特定团队中启用它们。
替换/添加现有的团队限制工作通过
PUT/POST /repos/:owner/:repo/branches/:branch/protection/restrictions/teams
删除推送限制也很有效,使用
DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions
但是,如果我取消推送限制,我找不到如何重新启用它以添加特定团队的方法。如果我尝试添加或替换团队,消息会显示 “未启用推送限制” .
那么如何启用复选框限制谁可以推送到此分支以在脚本中添加团队?查看所需结果的屏幕截图:Push Restrictions
API 文档只是为我提供了选项 Get restrictions of protected branch和 Remove restrictions of protected branch .
到目前为止我尝试过的:
最佳答案
查询 Update branch protection Github API Rest 部分:
PUT /repos/:owner/:repo/branches/:branch/protection
使用 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"
注意设置
null
这些字段之一将禁用(取消选中)该功能在 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())
关于python - GitHub API : Enable Push Restrictions for branch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020398/