【问题标题】:How to disable the Branch Policies in Azure DevOps using REST API?如何使用 REST API 在 Azure DevOps 中禁用分支策略?
【发布时间】:2021-10-30 00:57:27
【问题描述】:

我正在尝试禁用并重新启用使用 Azure DevOps REST API 为分支创建的分支策略。

我手动创建的分支策略:

使用CURL 我能够获得已在存储库中创建的分支策略列表。

curl --url "https://dev.azure.com/{ORG}/{PROJ}/_apis/policy/configurations?api-version=6.0" --user "username:password" --request GET --header "Accept: application/json"

输出:

{
    "count":1,
    "value":[
        {
            "createdBy":{
                "displayName":"Akshay B",
                "url":"XXXX",
                "_links":{
                    "avatar":{
                        "href":"XXXX"
                    }
                },
                "id":"XXXX-XXXX-XXXX-XXXX-e0fdec2c1636",
                "uniqueName":"XXXX@XXXX.com",
                "imageUrl":"XXXX",
                "descriptor":"XXXX"
            },
            "createdDate":"2021-08-30T12:24:43.0238821Z",
            "isEnabled":true,
            "isBlocking":true,
            "isDeleted":false,
            "settings":{
                "minimumApproverCount":2,
                "creatorVoteCounts":false,
                "allowDownvotes":false,
                "resetOnSourcePush":false,
                "requireVoteOnLastIteration":false,
                "resetRejectionsOnSourcePush":false,
                "blockLastPusherVote":false,
                "scope":[
                    {
                        "refName":"refs/heads/master",
                        "matchKind":"Exact",
                        "repositoryId":"XXXX-XXXX-XXXX-XXXX-cd2a5c3167b3"
                    }
                ]
            },
            "isEnterpriseManaged":false,
            "_links":{
                "self":{
                    "href":"XXXX"
                },
                "policyType":{
                    "href":"XXXX"
                }
            },
            "revision":1,
            "id":2,
            "url":"XXXX",
            "type":{
                "id":"XXXX-XXXX-XXXX-XXXX-4906e5d171dd",
                "url":"XXXX",
                "displayName":"Minimum number of reviewers"
            }
        }
    ]
}

现在我正在尝试使用以下CURL 命令禁用上面创建的策略:

curl --url "https://dev.azure.com/{ORG}/{PROJ}/_apis/policy/configurations/2?api-version=6.0" --user "username:password" --request PUT --header "Content-Type: application/json" --data '{\"isEnabled\":false}'

但我最终得到了错误:

{"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: xxxx-xxxx-xxxx-xxxx-70e5364888b7.","typeName":"Newtonsoft.Json.JsonReaderException, Newtonsoft.Json","typeKey":"JsonReaderException","errorCode":0,"eventId":0}

在要为PUT 方法传递的 JSON 数据中是否遗漏了什么?

【问题讨论】:

    标签: azure azure-devops azure-devops-rest-api azure-repos


    【解决方案1】:

    有许多分支策略(审查、构建等),每个策略的行为都不同。

    对于审阅者政策,您可以使用DELETE API:

    https://dev.azure.com/{org}/{project}/_apis/policy/Configurations/{policy-id}?api-version=6.0
    

    curl 中,--request 应该是DELETE

    您可以使用您所做的GET api 获取policy-id

    【讨论】:

    • 谢谢@Shayki。有效。我能够使用 REST API 删除然后重新创建它。
    • @B.Akshay 太好了!您可以将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2020-12-23
    • 1970-01-01
    • 2019-03-01
    • 2021-10-20
    • 2017-03-20
    相关资源
    最近更新 更多