Search code examples
azureazure-devopsazure-cliazure-devops-rest-api

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


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.)


Solution

  • 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