使用 REST API 将构建 link 添加到工作项

Add build link to work item using REST API

我有一个构建由另一个构建触发。触发构建有工作项 linked 到它。 为了更好的可见性,我想 link 所有 link 触发构建的工作项也被触发构建。我已经准备好提取工作项列表的所有内容,但我找不到 link 使用 REST API 构建工作项的方法 尝试使用工作项 - 更新添加 link 选项 https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-5.1#add-a-link

工作项关系类型 - 列表 returns: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20item%20relation%20types/list?view=azure-devops-rest-5.1

System.LinkTypes.Remote.Dependency-Forward
System.LinkTypes.Remote.Dependency-Reverse
System.LinkTypes.Duplicate-Forward
System.LinkTypes.Duplicate-Reverse
Microsoft.VSTS.TestCase.SharedParameterReferencedBy-Forward
Microsoft.VSTS.TestCase.SharedParameterReferencedBy-Reverse
Microsoft.VSTS.Common.Affects-Forward
Microsoft.VSTS.Common.Affects-Reverse
Microsoft.VSTS.TestCase.SharedStepReferencedBy-Forward
Microsoft.VSTS.TestCase.SharedStepReferencedBy-Reverse
Microsoft.VSTS.Common.TestedBy-Forward
Microsoft.VSTS.Common.TestedBy-Reverse
System.LinkTypes.Dependency-Forward
System.LinkTypes.Dependency-Reverse
System.LinkTypes.Hierarchy-Forward
System.LinkTypes.Hierarchy-Reverse
System.LinkTypes.Related
System.LinkTypes.Remote.Related
AttachedFile
Hyperlink
ArtifactLink

除了最后3个,好像都是WI对WI的关系。如何将 link 添加到构建中?

目前无法实现。有三种外部 link 类型(Pipelines/build,在构建中找到,在构建中集成)用于 link 工作项到构建,如 Link types 文档中所述.

但是,我发现上面的 Build 关系类型没有在上面返回的关系类型中列出。我试过了,只是让它添加了一个构建作为 Hyperlink.

对于解决方法,您可以尝试将构建添加到工作项中作为当前 Hyperlink

您可以将此问题报告给 Microsoft 开发团队 here。希望他们能调查一下这个问题。

正确的关系类型是 ArtifactLink。

我在管道中使用了以下 powershell 代码来创建这些链接:

$personalToken = "<insert personal token here>"
 
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}

$buildId = $(Build.BuildId)
$azureDevOpsOrg = "<insert your organization name here>"
$projectName = "<insert your project name here>"

$buildWorkItemsUrl = "https://dev.azure.com/${azureDevOpsOrg}/${projectName}/_apis/build/builds/${buildId}/workitems?api-version=5.0"

$body = "[
  {
    ""op"": ""add"",
    ""path"": ""/relations/-"",
    ""value"": {
      ""rel"": ""ArtifactLink"",
      ""url"": ""vstfs:///Build/Build/${buildId}"",
      ""attributes"": {
        ""name"": ""Integrated in build""
      }
    }
  }
]"

$workItems = Invoke-RestMethod -Uri $buildWorkItemsUrl -Method Get -ContentType "application/json" -Headers $header

$workItems.value | ForEach-Object {
  $workItemId = $_.id
  Write-Host $workItemId
  $updateWorkItemurl = "https://dev.azure.com/${azureDevOpsOrg}/${projectName}/_apis/wit/workitems/${workItemId}?api-version=6.0"
  Write-Host $updateWorkItemurl
  Invoke-RestMethod -Uri $updateWorkItemurl -Method Patch -ContentType "application/json-patch+json" -Headers $header -Body $body
}