Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add build link to work item using REST API

I have a build which is triggered by another build. The triggering build has work items linked to it. For better visibility I want to link all the work items that are linked to triggering build also to the triggered build. I already have everything in place to pull the list of work items but I can't find the way to link the work items to a build using REST API Trying to use the Work Items - Update add a link option https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-5.1#add-a-link

The Work Items Relation Types - List returns: https://learn.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

Except the last 3, all seems to be WI to WI relation. How do I add a link to a build?

like image 962
Mickey Cohen Avatar asked Mar 14 '26 01:03

Mickey Cohen


1 Answers

The correct Relation Type is ArtifactLink.

I used the following powershell code in my pipeline to create these links:

$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
}
like image 176
Tad Avatar answered Mar 15 '26 17:03

Tad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!