How to link a work item to a pull request using REST API in Azure DevOps?

Viewed 1198

Within a release pipeline a new Pull Requested is created using REST API.

How to link a specific (already existing) Work Item to the Pull Request using REST API?

In the current version (DevOps 2019) it is not supported to link Work Items using Pull Request API. (See also related community issue.)

1 Answers

Using PowerShell the following snipped may help.

$requestUri = "$tfsCollectionUri/$teamProject/_apis/wit/workitems/$workItemId" + "?api-version=5.0"
$json = '
[ {
  "op": "add", "path": "/relations/-",
  "value": {
    "rel": "ArtifactLink",
    "url": "$pullRequestArtifact",
    "attributes": { "name": "pull request" }
  }
} ]'
$response = Invoke-RestMethod -Uri $requestUri -UseDefaultCredentials -ContentType "application/json-patch+json" -Method Post -Body $json

Note, $pullRequestArtifact needs to be set. You can get it e.g. from get request.

Related