如何使用 REST API 在 Azure DevOps 中禁用分支策略?
How to disable the Branch Policies in Azure DevOps using REST API?
我正在尝试禁用并重新启用为使用 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 数据中是否遗漏了什么?
有很多分支策略(审查、构建等),每个策略的行为都不同。
对于审稿人政策,您可以使用 DELETE
API:
https://dev.azure.com/{org}/{project}/_apis/policy/Configurations/{policy-id}?api-version=6.0
在 curl
中 --request
应该是 DELETE
。
您可以用 GET
api 获得 policy-id
。
我正在尝试禁用并重新启用为使用 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 数据中是否遗漏了什么?
有很多分支策略(审查、构建等),每个策略的行为都不同。
对于审稿人政策,您可以使用 DELETE
API:
https://dev.azure.com/{org}/{project}/_apis/policy/Configurations/{policy-id}?api-version=6.0
在 curl
中 --request
应该是 DELETE
。
您可以用 GET
api 获得 policy-id
。