如何在DevOps的Git项目中获取所有拉取请求的全部评论?
I totally get the frustration—TFS had that handy Visual Studio integration for pulling PR comment data easily, but Azure DevOps Git takes a slightly different approach. Here are three solid methods to get all your pull request comments, depending on your workflow needs:
1. Azure DevOps CLI (Best for Bulk Exports)
The CLI is perfect if you need to script or bulk-export comments. First, make sure you’ve installed the Azure DevOps CLI and authenticated with your org:
az login az devops configure --defaults organization=YourOrgName project=YourProjectName
Then follow these steps:
- Export a list of all pull requests (filter by state like
completedorall):az repos pr list --state all --output json > all-prs.json - Loop through each PR to fetch its comments (example PowerShell script):
$pullRequests = Get-Content all-prs.json | ConvertFrom-Json foreach ($pr in $pullRequests) { az repos pr comment list --id $pr.id --output json >> all-pr-comments.json }
This will compile all comments into a single JSON file you can parse or analyze later.
2. Azure DevOps REST API (Customizable Automation)
If you need fine-grained control over the data you retrieve, the REST API is your friend. You’ll need a Personal Access Token (PAT) with Code (Read) permissions.
First, fetch all pull requests:
GET https://dev.azure.com/{YourOrg}/{YourProject}/_apis/git/pullrequests?api-version=7.1-preview.1
Then, for each PR ID returned, fetch its comments:
GET https://dev.azure.com/{YourOrg}/{YourProject}/_apis/git/pullrequests/{PRId}/comments?api-version=7.1-preview.1
You can use tools like Postman or write a simple Python/JS script to iterate through PRs and aggregate comments into a structured format.
3. Visual Studio (Ad-Hoc Access)
While bulk exports aren’t as direct as TFS, you can still access PR comments in Visual Studio:
- Open your Git repo in VS, go to the Pull Requests tab (under the Git menu).
- Use the filter dropdown to show all PRs (active, completed, abandoned).
- For each PR, open it and navigate to the Comments tab to view all discussions.
- Note: This works great for checking individual PRs, but isn’t ideal for bulk data retrieval.
Bonus: Azure DevOps Analytics (Reporting & Dashboards)
If you need to build reports or track comment metrics over time, use the Analytics service. You can connect Power BI to Azure DevOps via the Analytics connector, then create queries to pull PR comments alongside other metrics like author, date, and PR status for deeper analysis.
Pro tip: When using CLI or API, add filters like
--type userto only fetch comments from team members (excluding system-generated ones) or--created-after "YYYY-MM-DD"to narrow results to a specific date range.
内容的提问来源于stack exchange,提问作者Buck Uy

