通过API批准管道阶段azure devops

问题描述 投票:0回答:2

我正在尝试批准 azure devops 中的多阶段 yaml 管道的各个阶段,但我对端点的 REST 请求详情此处返回错误

{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: updateParameters","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}

我遵循的策略是从管道的时间线中获取批准 ID(记录在此处),然后获取批准详细信息(记录在此处),这给了我 2 个对象:

时间线的批准阶段

{
"previousAttempts":  [

                     ],
"id":  "e23aa595-7745-4015-9fdd-be7863faeda1",
"parentId":  "4ae2b754-452d-5a7c-1d05-4887a615523a",
"type":  "Checkpoint.Approval",
"name":  "Checkpoint.Approval",
"startTime":  "2023-11-15T12:36:50.5633333Z",
"finishTime":  null,
"currentOperation":  null,
"percentComplete":  null,
"state":  "inProgress",
"result":  null,
"resultCode":  null,
"changeId":  71,
"lastModified":  "0001-01-01T00:00:00",
"workerName":  null,
"details":  null,
"errorCount":  0,
"warningCount":  0,
"url":  null,
"log":  null,
"task":  null,
"attempt":  1,
"identifier":  "e23aa595-7745-4015-9fdd-be7863faeda1"
}

并直接从 GET 请求获得批准

{
"id":  "e23aa595-7745-4015-9fdd-be7863faeda1",
"steps":  [

          ],
"status":  "pending",
"createdOn":  "2023-11-15T12:36:50.53Z",
"lastModifiedOn":  "2023-11-15T12:36:50.531529Z",
"executionOrder":  "anyOrder",
"minRequiredApprovers":  1,
"blockedApprovers":  [

                     ],
"_links":  {
               "self":  {
                            "href":  "https://dev.azure.com/<orgName>/8f6c8014-5c15-49aa-8efc-adb274f30be3/_apis/pipelines/approvals/e23aa595-7745-4015-9fdd-be7863faeda1"
                        }
           },
"pipeline":  {
                 "owner":  {
                               "_links":  "@{web=; self=}",
                               "id":  23633,
                               "name":  "20231115.7"
                           },
                 "id":  "64",
                 "name":  "Traveller"
             }
}

然后我构建了 PATCH 主体

[
{
    "status":  "approved",
    "approvalId":  "e23aa595-7745-4015-9fdd-be7863faeda1",
    "comment":  ""
}
]

并将其发送到端点

https://dev.azure.com/<orgName>/<projectname>/_apis/pipelines/approvals?api-version=7.1-preview.

有关此主题的所有文档和其他问题都表明我做得正确,但我无法克服此错误。

咨询的其他问题包括 这个这个

azure azure-devops azure-devops-rest-api azure-pipelines-yaml
2个回答
0
投票

我可以重现同样的问题。

问题的原因是格式:

$body | ConvertTo-Json
不会在请求体中添加[]。

要解决此问题,您可以更改为使用以下格式:

ConvertTo-Json $body

$response = Invoke-RestMethod -Method PATCH -Uri  $uri -Headers $headers  -Body ( ConvertTo-Json $body  ) -ContentType "application/json"

然后它会将正确的请求体传递给Request。


0
投票

我直接在请求中使用了json内容。这是我的样本供您参考。

$organization = "Org"
$project = "Project"
$MyPat = 'xxxxx'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$URL = "https://dev.azure.com/$organization/$project/_apis/pipelines/approvals?api-version=7.1-preview.1"
$header = @{
'Authorization' = 'Basic ' + $B64Pat
'Content-Type' = 'application/json'
}
$body = @"
[
{
    "status": "approved",
    "approvalId": "2c8f92ed-6c78-4ccb-8cb6-59afec19cec2",
    "comment": ""

}
]
"@

$response = Invoke-RestMethod -Method PATCH -Uri $URL -Headers $header -Body $body | ConvertTo-Json
© www.soinside.com 2019 - 2024. All rights reserved.