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