请求协助获取Azure Git仓库分支统计及分支作者信息
Hey Gosfly, sorry to hear you couldn't find a built-in way to pull this data through Analytics Views or Dashboards—those tools excel at high-level pipeline/PR metrics but don't cover granular branch-level author stats out of the box. Let me walk you through two reliable approaches to get this info for all 150 of your Azure Repos Git repositories.
1. Recommended: Azure DevOps CLI + PowerShell Script
This is the simplest way to batch-process all your repos. The Azure DevOps CLI lets you interact with Azure Repos programmatically, and PowerShell makes it easy to loop through repos and format output.
Step 1: Set Up Azure DevOps CLI
If you haven't already, install and configure the CLI:
- Install the Azure DevOps extension:
az extension add --name azure-devops - Log in to your organization:
az devops login - Set your default organization (avoids repeating it in commands):
az devops configure --defaults organization=https://dev.azure.com/YourOrgName/
Step 2: Run the PowerShell Script
Copy this script, replace YourOrgName and YourProjectName with your actual values, and run it in PowerShell. It will output non-default branch counts and the last commit author for each branch (adjust the default branch filter if your repos use main instead of master):
# Configure your Azure DevOps details $orgName = "YourOrgName" $projectName = "YourProjectName" $defaultBranches = @("master", "main") # Add other default branches if needed # Get all Git repositories in the project $repos = az repos list --org "https://dev.azure.com/$orgName" --project "$projectName" --output json | ConvertFrom-Json # Initialize array to store results (for CSV export) $results = @() # Loop through each repository foreach ($repo in $repos) { Write-Host "`n=== Repository: $($repo.name) ===" -ForegroundColor Cyan # Fetch all branches and filter out default ones $branches = az repos ref list --org "https://dev.azure.com/$orgName" --project "$projectName" --repository "$($repo.name)" --filter "heads" --output json | ConvertFrom-Json $nonDefaultBranches = $branches | Where-Object { $_.name -notin $defaultBranches } # Display non-default branch count Write-Host "Non-default branch count: $($nonDefaultBranches.Count)" # Get last commit author for each non-default branch Write-Host "Branch author details:" foreach ($branch in $nonDefaultBranches) { # Get the latest commit on the branch $latestCommit = az repos commit list --org "https://dev.azure.com/$orgName" --project "$projectName" --repository "$($repo.name)" --branch "$($branch.name)" --top 1 --output json | ConvertFrom-Json # Create result object for CSV $resultObj = [PSCustomObject]@{ RepositoryName = $repo.name BranchName = $branch.name -replace "refs/heads/", "" # Clean up branch name LastAuthorName = $latestCommit.author.name LastAuthorEmail = $latestCommit.author.email } Write-Host "- Branch: $($resultObj.BranchName) | Last Author: $($resultObj.LastAuthorName)" $results += $resultObj } } # Export results to CSV for easy analysis $results | Export-Csv -Path "AzureRepoBranchStats.csv" -NoTypeInformation Write-Host "`nResults exported to AzureRepoBranchStats.csv" -ForegroundColor Green
Notes:
- If you need the branch creator instead of the last commit author, this requires more work—you'd need to trace the branch's creation commit (which isn't directly exposed via CLI). Let me know if you need guidance on that!
- The script exports results to a CSV file, which you can open in Excel to sort/filter the data for your 150 repos.
2. Alternative: Azure DevOps REST API
If you prefer to build a custom tool or integrate this into an existing system, use the Azure DevOps REST API directly. Here are the key endpoints:
- List all repositories:
GET https://dev.azure.com/{org}/{project}/_apis/git/repositories?api-version=7.1-preview.1 - List branches for a repo:
GET https://dev.azure.com/{org}/{project}/_apis/git/repositories/{repoId}/refs?filter=heads&api-version=7.1-preview.1 - Get latest commit for a branch:
GET https://dev.azure.com/{org}/{project}/_apis/git/repositories/{repoId}/commits?searchCriteria.itemVersion.version={branchName}&$top=1&api-version=7.1-preview.1
You'd need to authenticate with a PAT (Personal Access Token) with Code Read permissions for the API calls.
内容的提问来源于stack exchange,提问作者Gosfly

