Reputation: 7
I am working on an approach to attach a work item to a Pull Request via an API in Azure DevOps.
I have attempted the following method, but it has not successfully tagged the work item.
$requestUri = "https://dev.azure.com/$org/$project/_apis/wit/workitems/12345?api-version=4.1"
$prapi1 = "https://dev.azure.com/$org/$project/_apis/git/repositories/$repo/pullRequests/12345"
$json = @"
[ {
"op": "add",
"path": "/relations/-",
"value": {
"rel": "ArtifactLink",
"url": "$pr",
"attributes": { "name": "pull request" }
}
} ]
"@
$response = Invoke-RestMethod -Uri $requestUri -Headers $headers -ContentType "application/json-patch+json" -Method Patch -Body $json
However, when using the specified API, I encounter an error instructing me to:
{"$id":"1","innerException":null,"message":"Invalid Resource Link Target: 'https://dev.azure.com/org/project/_apis/git/repositories/repo/pullRequests/12345'.","typeName":"Microsoft.TeamFoundation.WorkItemTracking.Server.WorkItemResourceLinkException, Microsoft.TeamFoundation.WorkItemTracking.Server","typeKey":"WorkItemResourceLinkException","errorCode":0,"eventId":3200}
Upvotes: 0
Views: 595
Reputation: 35194
Based on your script sample, the cause of the issue is that the pr url is not correct.
The correct format: vstfs:///Git/PullRequestId/{projectId}/{repositoryId}/{pullRequestId}
You can get the Pull Request URL in the Rest API: Pull Requests - Get Pull Request
It will show the artifactId in the response.
For example:
Here is the PowerShell sample:
$token = "PAT"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$PullRequest="https://dev.azure.com/{org}/{projectname}/_apis/git/repositories/{reponame}/pullrequests/{PullRequestID}?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $PullRequest -Headers @{Authorization = "Basic $token"} -ContentType "application/json-patch+json" -Method Get
$url= $response.artifactId
echo $url
$requestUri = " https://dev.azure.com/{orgname}/_apis/wit/workitems/{workitemid}?api-version=7.1-preview.3"
$json = @"
[ {
"op": "add",
"path": "/relations/-",
"value": {
"rel": "ArtifactLink",
"url": "$url",
"attributes": { "name": "pull request" }
}
} ]
"@
$response1 = Invoke-RestMethod -Uri $requestUri -Headers @{Authorization = "Basic $token"} -ContentType "application/json-patch+json" -Method Patch -Body $json
Upvotes: 1