Search code examples
azureazure-devopsazure-devops-rest-apiazure-repos

How to disable the Branch Policies in Azure DevOps using REST API?


I am trying to disable and again re-enable the branch policy created for a branch using Azure DevOps REST API.

The Branch Policy that I have manually created: Branch Policy

Using CURL I was able to get the list of Branch Policies that has been created in the repository.

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

Output:

{
    "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"
            }
        }
    ]
}

Now I am trying to disable the policy created above using the below CURL command:

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}'

But I end up with the error:

{"$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}

Is there anything I am missing out in the JSON data to be passed for the PUT method?


Solution

  • There are many branch policies (reviews, builds etc.) and every policy the behavior is different.

    For reviewers policy you can use the DELETE API:

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

    In curl the --request should be DELETE.

    You can get the policy-id with the GET api you did.