GitHub API:为分支机构启用推送限制 [英] GitHub API: Enable Push Restrictions for branch
问题描述
我正在尝试使用GitHub API(版本2.11)在Python脚本中禁用和启用GitHub项目的分支保护.更具体地说,我想从分支中删除所有推送限制,然后在特定团队中启用它们.
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
API文档仅向我展示了选项 获取受保护分支的限制 和
The API documentation just presents me the options Get restrictions of protected branch and Remove restrictions of protected branch.
到目前为止我尝试过的:
What I tried so far:
- 仅删除所有团队而不删除限制是行不通的,因为那样一来没人能推动.
- 将PUT/POST发送到/repos/:owner/:repo/branches/:branch/protection/restrictions 会得到404.
- 现在,除了手动单击复选框,然后通过API添加和替换作品外,我别无选择.
- 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.
推荐答案
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"
请注意,将null
设置为这些字段之一将禁用(取消选中)该功能
Note that setting null
to one of those fields will disable(uncheck) the feature
在 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())
这篇关于GitHub API:为分支机构启用推送限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!