GitHub API: 为分支启用推送限制

GitHub API: Enable Push Restrictions for branch

我正在尝试使用 GitHub API(版本 2.11)在 Python 脚本中禁用和启用 GitHub 项目的分支保护。更具体地说,我想从一个分支中删除所有推送限制,然后在特定团队中启用它们。

Replacing/adding 现有团队限制通过

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

并且使用

删除推送限制也很有效
DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions

但是,如果我删除推送限制,我找不到如何重新启用它以添加特定团队的方法。如果我尝试添加或替换团队,消息会显示 'Push restrictions not enabled'

那么如何启用复选框限制谁可以推送到此分支以在脚本中添加团队?查看所需结果的屏幕截图:Push Restrictions

API 文档仅向我提供了选项 Get restrictions of protected branch and Remove restrictions of protected branch.

到目前为止我尝试了什么:

检查 Github 的 Update branch protection 部分 API 其余:

PUT /repos/:owner/:repo/branches/:branch/protection

使用 &

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 设置为这些字段之一将禁用(取消选中)该功能

中:

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

我正在尝试将团队添加到受保护的分支。 我可以知道我的团队是否

  1. 鼻涕虫

  2. 姓名

  3. 是否也包括球队老板

         "teams": [
           "my-team"
         ]
    

在此先感谢您的帮助。