Reputation: 1277
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?
Upvotes: 3
Views: 3394
Reputation: 659
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
}
Upvotes: 5
Reputation: 30313
It is unachievable currently. There are three external link types(Pipelines/build, Found in build, Integrated in build) that are used to link a work item to a build as described in the Link types document.
However, i found above Build relation types is not listed in above returned Relation Types. I tried and only made it adding a build as a Hyperlink.
For a workaround,You can try adding the build to the workitem as Hyperlink currently.
You can report this problem to Microsoft Development team here. Hope they can look into this problem.
Upvotes: 0