python - GitHub API : Enable Push Restrictions for branch

标签 python git github github-api

我正在尝试使用 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 branchRemove restrictions of protected branch .

到目前为止我尝试过的:
  • 只是在不取消限制的情况下删除所有团队是行不通的,因为这样就没有人能够 push 了。
  • 将 PUT/POST 发送到/repos/:owner/:repo/branches/:branch/protection/restrictions 给出 404。
  • 现在我除了手动单击复选框,然后通过 API 添加和替换作品之外别无他法。
  • 最佳答案

    查询 Update branch protection Github API Rest 部分:

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

    关于python - GitHub API : Enable Push Restrictions for branch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020398/

    相关文章:

    python - 用两个三角形定义菱形

    python - 注释 block /缩进干扰代码

    ssh - 如何从第二台机器访问 Github 存储库?

    git - 您如何在命令行上重新创建 GitHub pull 请求差异?

    xcode - 将 pod 文件和应用程序扩展推送到 Github 的问题

    python,pandas,按条件删除行

    python - 为什么Python中的属性查找是这样设计的(优先链)?

    git - 如何在gitlab中管理多个maven项目

    git - 鬼魂,你从哪里来?

    Git 提交提交暂存和未暂存文件