skip to Main Content

Is there an az cli command that gets the threads and comments on a PR?

PR’s can be listed with:
az repos pr list --organization $organization --project "$project" --repository $repo

docs

The REST API has this endpoint, which gets the threads and comments:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/threads?api-version=7.1-preview.1

docs

example output cropped:

    "value": [
        {
            "pullRequestThreadContext": null,
            "id": 111111,
            "publishedDate": "2024-02-19T16:00:00.000Z",
            "lastUpdatedDate": "2024-02-19T16:00:00.000Z",
            "comments": [],
            "status": "active",
            "threadContext": null,
            "etc": "etc"
        },
        {
            "pullRequestThreadContext": null,
            "id": 123456,
            "publishedDate": "2024-02-19T16:00:00.000Z",
            "lastUpdatedDate": "2024-02-19T16:00:00.000Z",
            "comments": [
                    "content": "Some comment on the PR appears in this field",
                    "publishedDate": "2024-02-19T16:00:00.000Z",
                    "lastUpdatedDate": "2024-02-19T16:00:00.00Z",
                    "lastContentUpdatedDate": "2024-02-19T16:00:00.000Z",
                    "commentType": "text",
                    "etc": "etc"`
            ]
        }
            ]
            "etc"
            "etc"
}

But there does not seem to be a way to use az repos to get threads and comments.

(I am specifically looking for PR’s that have open comments, with "status": "active" and not "status": "fixed": since our automation should not close these PR’s. Atm we have no way of differentiating between PR’s except for using the REST API, ideally we do not use both REST API and az cli, az cli is more user friendly, so has my preference.)

2

Answers


  1. Instead of using az repos pr list, Use az repos pr show command to get the comments from the PR ID of the repository. Unfortunately, According to this MS Document there’s no direct command to get the comments from az repos.

    Commands:-

    az login
    az repos pr show --id 11 --org https://dev.azure.com/sid24desai0738/
    

    Output:-

    enter image description here

    enter image description here

    Also, You can also use Powershell command to easily call any Azure Rest API, to call the Rest API mentioned in the question to list Repos by PR ID:-

    Commands:-

    $PAT = "qxxxxxxxxizikif6js4o37sbt7a"
    $OrgName = "xxxxi0738"
    $ProjectName = "AzureDevops"
    $ApiVersion = "7.1-preview.1"
    
    $threads = Invoke-RestMethod -Uri "https://dev.azure.com/$OrgName/$ProjectName/_apis/git/repositories/xxxxxxb1880/pullRequests/11/threads?api-version=$ApiVersion" -Method Get -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))}
    
    $threads.value
    

    Output:-

    enter image description here

    Login or Signup to reply.
  2. To get threads with az devops, you may use invoke subcommand. To get threads:

    az devops invoke --area git --resource pullRequestThreads --org https://dev.azure.com/<your_org> --route-parameters project=<your_project> repositoryId=<repo_name> pullRequestId=<pr_id> --http-method GET --api-version 5.1-preview -o json 
    

    To get comments in threads:

    az devops invoke --area git --resource pullRequestThreadComments --org https://dev.azure.com/<your_org> --route-parameters project=<your_project> repositoryId=<repo_name> pullRequestId=<pr_id> threadId=<id_from_previous_cmd> --http-method GET --api-version 5.1-preview -o json 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search