How to add a link "parent/child" between 2 certain already created work item - Azure Boards

2k views Asked by At

Currently, we use Veracode in our ci enviroment(Azure Pipelines) for security check. Veracode has a azuredevops plugin, that creates flaws as work items(either bug, task etc) in azure boards. However, we created a certain epic, "Security Issues", and we expect that there's a way to link via pipeline, a already created work item to it as a parent/child.

I've been searching for methods (first time working with Azure API's), and I didnt found any specific way to achieve this integration( I did see this, but in this case, they create a work item, instead of linking 2 that already exist How to add work item as child to parent?).

I did talk to Veracode team, however this feature isn't currently available. We can add custom Tags however.

TL:DR: How can I, via pipeline automation, query for certain work items in azureboards, and link them as a parent/child to a certain created epic.

Thanks in advance!

1

There are 1 answers

0
Walter On BEST ANSWER

You can use update work item REST API to add a link between two work items. Since you are using REST API in the pipeline, you can use run your REST API in a PowerShell task. Here is the sample of adding a child link to an existing work item:

$organization = "{Organization name}" 
$project = "{Project name}" 
$pat = "{PAT}"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$url = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/{Work item id}?api-version=5.1" 
$contentType = "application/json-patch+json"
$body= @'
[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "System.LinkTypes.Hierarchy-Forward",//Add a child link
      ##"rel": "System.LinkTypes.Hierarchy-Reverse",//Add a parent link
      "url": "https://dev.azure.com/{Organization name}/{Project name}/_apis/wit/workItems/{Work item id}"
    }
  }
]
'@
Invoke-RestMethod -Uri $url -Method PATCH -ContentType $contentType -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body  

You can find the Work Item Relation Types with this API.

If you want to query the work items, you can try to use Wiql. Please refer to this example.