Reputation: 305
I am trying to auto-approve the Pull request programmatically using PATCH method of REST Api. But it is giving below error: "$id":"1","innerException":null,"message":"The pull request must be linked to a work item before it can be completed."
I tried below approach, but it didn't work for me. How to link a work item to a pull request using REST API in Azure DevOps?
It is linking the PR to work item, but not the other way around. Please let me know if we can link work item to PR while creating it (I am using REST POST method) or while updating PR (I am using REST PATCH method to make its Status:Completed. My JSON body looks like below for PATCH:
$JSONBody= @"
{
"status": "completed",
"lastMergeSourceCommit": {
"commitId": "$CommitId"
},
"completionOptions": {
"bypassPolicy": true,
"deleteSourceBranch": true
}
}
"@
Edit#1:
I am using below code in powershell. Below code is updating work item, but not updating the PR page. Please help:
[uri] $PRUri = "https://dev.azure.com/{Organization****}/{Project_ID***}/_apis/wit/workitems/****" + "?api-version=4.0-preview"
$JSONBody= '
[
{
"op": 0,
"path": "/relations/-",
"value": {
"attributes": {
"name": "Pull Request"
},
"rel": "ArtifactLink",
"url": "vstfs:///Git/PullRequestId/{Project_ID***}/{REPO_ID**}/PR_ID***"
}
}
]'
enter code here
$User = "" # Not needed when using PAT, can be set to anything
$PAT="****"
$Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$PAT)))
$Response=Invoke-RestMethod -Uri $PRUri
-Method PATCH
-ContentType "application/json-patch+json"
-Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)}
-Body $JSONBody
Upvotes: 1
Views: 3641
Reputation: 1152
We can use the api:
Post https://dev.azure.com/{Organization}/_apis/wit/$batch
to help us link the workItem to PR. Here is the request body:
[
{
"method": "PATCH",
"uri": "/_apis/wit/workItems/{WorkItemId}?api-version=4.0-preview",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": 0,
"path": "/relations/-",
"value": {
"attributes": {
"name": "Pull Request"
},
"rel": "ArtifactLink",
"url": "vstfs:///Git/PullRequestId/{ProjectID}%2F{repositoryID}%2F{PullRequestId}"
}
}
]
}
]
By the way, we can use the api: Projects - List And Repositories - List to help us find the projectId and the repositoryID.
Update:
Update2(with my demo script):
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic {YourPATxxxx}")
$headers.Add("Content-Type", "application/json")
$JSON = @'
[
{
"method": "PATCH",
"uri": "/_apis/wit/workItems/471?api-version=4.0-preview",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": 0,
"path": "/relations/-",
"value": {
"attributes": {
"name": "Pull Request"
},
"rel": "ArtifactLink",
"url": "vstfs:///Git/PullRequestId/{ProjectID}%2F{repositoryID}%2F{PullRequestId}"
}
}
]
}
]
'@
$response = Invoke-RestMethod 'https://dev.azure.com/{orgName}/_apis/wit/$batch' -Method 'POST' -Headers $headers -Body $JSON
$response | ConvertTo-Json
Here I use the workItem 471 to test:
Upvotes: 2