1

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

1 Answers1

1

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:

enter image description here

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: enter image description here

Felix
  • 1,104
  • 3
  • 6
  • I already tried this. It does exactly opposite of what I expect. On pull request page, I want to see the linked work item. Above solution puts PR link on workitem page. – Aniket Karajgikar Mar 17 '21 at 06:25
  • Hi @AniketKarajgikar, The above way also will show the work item link in your pull request page. I update the test result about my PR page. And also, you can try to open the F12 and try to catch the network, the Web UI also use the same api. – Felix Mar 17 '21 at 08:43
  • Thanks. But I am still facing same issue. I posted my code by editing the question above, could you please take a look and let me know. Must be a silly mistake from my side. – Aniket Karajgikar Mar 17 '21 at 16:55
  • Hi @AniketKarajgikar, I help to create a demo script with powershell. And please note:1. We need to use the api: https://dev.azure.com/{Organization}/_apis/wit/$batch with the post option. 2. In the json, we need to use the '%2F' instead of the '/' . – Felix Mar 18 '21 at 02:18
  • You are awesome. Thanks for all the help! I was using names of project and repo, instead of using their IDs and that was making this issue. – Aniket Karajgikar Mar 18 '21 at 07:21