向 PBI 添加部署状态

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

我想通过 powershell 调用 azure devops api 添加部署状态,如下所示

这是将作为发布管道(经典管道)的一部分运行的 powershell 脚本

# Define your Azure DevOps organization, project, and personal access token (PAT)
$organizationUrl = "https://dev.azure.com/ORG"
$organization = "ORG"
$projectName = "PROJ"

# Define the work item ID you want to retrieve
$workItemId = 14846
$accessToken = $env:SYSTEM_ACCESSTOKEN
$basicAuthValue = "Bearer $accessToken"
$headers = @{
    Authorization = $basicAuthValue
    "Content-Type" = "application/json-patch+json"
}
$headers
# Define the URL for the PATCH request
$apiUrl = "$organizationUrl/$projectName/_apis/wit/workitems/14846?api-version=7.0"
$apiUrl

# Define the JSON body for the PATCH request
$jsonBody = @(
    @{
      op = "test"
      path = "/rev"
      value = 46
    }
    @{
      op= "add"
      path= "/relations/-"
      value= @{
        rel= "ArtifactLink"
        url= "vstfs:///ReleaseManagement/ReleaseEnvironment/41d718f0-8259-494b-9ad0-5a72348ff1fc:1450:6941"
      }
    }
)

# Convert the JSON body to a string
$jsonString = $jsonBody | ConvertTo-Json -Depth 5
$jsonString

# Send the PATCH request
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Patch -Body $jsonString
   $response
    Write-Host "Work item updated successfully."
} catch {
    Write-Host "Error occurred: $($_.Exception.Message)"
    $_.Exception
}

当运行上面的脚本时,我收到 400 bad request,并且没有指示请求的哪一部分是错误的。

azure-devops-rest-api
1个回答
0
投票

使用相同的 PowerShell 脚本示例进行测试,我可以重现 400 错误。

问题的原因是您缺少 jsonbody 中的 attributes 参数值。

要解决这个问题,可以修改jsonbody,内容如下:

$jsonBody = @(
    @{
      op = "test"
      path = "/rev"
      value = 4
    }
    @{
      op= "add"
      path= "/relations/-"
      value= @{
        rel= "ArtifactLink"
        url= "vstfs:///ReleaseManagement/ReleaseEnvironment/41d718f0-8259-494b-9ad0-5a72348ff1fc:1450:6941"
        attributes= @{
          comment= "Link to release"
          name= "Integrated in release environment"
      }
      }
    }
)

然后 PowerShell 脚本就可以按预期运行了。

如果400错误仍然存在,您可能需要检查工作项链接页面中是否已存在该链接。

另一方面,操作成功后,Rest API 会在工作项中添加 Integrated in Release 阶段的链接。

例如:

但是仍然没有按预期显示 Deployment 状态。要在工作项中显示部署状态,您需要按照文档中的步骤手动配置管道:将工作项链接到 Azure Boards 中的构建和部署

Rest API 恐怕暂时不支持在工作项中设置部署状态。

© www.soinside.com 2019 - 2024. All rights reserved.