如何在 Azure DevOps 中获取代码的简单指标

问题描述 投票:0回答:1

我刚刚负责大约 100 个不同的存储库(应用程序),分布在 Azure DevOps 中的大约 20 个项目上。

我想获得每个项目的一些(非常)基本指标:

  • 每个项目中存储库的数量和名称
  • 存储库中的分支数量
  • 分支中的文件数量
  • 分支中的代码行数
  • 参与项目的开发人员姓名
  • 参与某个项目的开发人员数量
  • 开发人员对项目的第一个和最后一个提交/拉取请求
  • 等等

只是简单的计数和列出。没有什么像测试覆盖率等奇特的东西。到目前为止,我所有的搜索只给出了有关代码分析和其他高级内容的结果。这不是我现在的任务所在。

有没有办法在 Azure DevOps 中做到这一点?

git azure-devops metrics
1个回答
0
投票

Azure DevOps 不直接提供内置功能来汇总您需要的信息。如果您需要更自动化的方式,请考虑使用 Azure DevOps REST API。

以下是使用 Azure DevOps Git API 获取存储库和分支编号的示例 PowerShell 脚本:

# Define organization, PAT
$orgUrl = "https://dev.azure.com/orgname"
$pat = "xxx"
$APIVersion = "api-version=6.1-preview.1"

$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
}

# Function to make REST API requests
function Invoke-AzureDevOpsAPI {
    param (
        [string]$url,
        [string]$method
    )
    try {
        $response = Invoke-RestMethod -Uri $url -Headers $headers -Method $method
        return $response
    }
    catch {
        Write-Host "Error calling API: $($_.Exception.Message)"
        return $null
    }
}


# Get the list of all projects
$projectsUrl = "$orgUrl/_apis/projects?$APIVersion"
$projectsResponse = Invoke-AzureDevOpsAPI -url $projectsUrl -method "GET"

if ($projectsResponse) {
    Write-Host "Projects in your organization:"
    foreach ($project in $projectsResponse.value) {
        Write-Host "  $($project.name)"
        
        # Get repositories for the current project
        $reposUrl = "$orgUrl/$($project.name)/_apis/git/repositories?$APIVersion"
        $reposResponse = Invoke-AzureDevOpsAPI -url $reposUrl -method "GET"
        
        if ($reposResponse) {
            Write-Host "    Repositories in project:"
            foreach ($repo in $reposResponse.value) {
                Write-Host "      $($repo.name)"
                
                # Get branches for the current repository
                $branchesUrl = "$orgUrl/$($project.name)/_apis/git/repositories/$($repo.id)/refs?filter=heads&api-version=6.0"
                $branchesResponse = Invoke-AzureDevOpsAPI -url $branchesUrl -method "GET"
                
                if ($branchesResponse) {
                    $branchCount = $branchesResponse.count
                    Write-Host "        Number of branches: $branchCount"
                }
                else {
                    Write-Host "        Failed to retrieve branch information for $($repo.name)."
                }

            }
        }
        else {
            Write-Host "    Failed to retrieve repository information for $($project.name)."
        }
    }
}
else {
    Write-Host "Failed to retrieve project information."
}

有关开发人员信息,您可以尝试使用此 API Commits - 获取提交

对于分支中的文件数和分支中的代码行数,似乎没有API与之相关。您可以克隆存储库并运行 git 命令来获取信息。

您还可以查看 Azure DevOps 扩展市场 来查找合适的扩展。如果没有您满意的扩展,您也可以开发自己的扩展

© www.soinside.com 2019 - 2024. All rights reserved.