如何在 Azure DevOps 中获取所有存储库

How to get all repos in Azure DevOps

我在 Azure DevOps 中有多个项目。我希望能够遍历 Azure DevOps 中的所有 Repos 并获取 Repo 的名称、Repo 的创建者和 Last Updated/commit.

当有人创建新的 Repo 时收到通知?

我们可以通过 REST 列出回购信息 API

List all repositories and get the name of the repo:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.1-preview.1

Get the Creator:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=6.1-preview.1

注意:我们可以通过这个 API 获取分支创建者,我没有找到任何 API 来获取回购创建者。

Get latest commit:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?searchCriteria.$top=1&api-version=6.1-preview.1

Get a notification when some one created new Repo

我们无法创建此通知,我们可以在有人更新回购代码时收到通知。请参阅此 link 了解更多详情:Supported event types

更新1

//List project name
$connectionToken="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$ProjectUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.1-preview.1" 
$Repo = (Invoke-RestMethod -Uri $ProjectUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$RepoName= $Repo.value.name
Write-Host  $RepoName

//get latest commit info and branch creator
$RepoID=$Repo.value.id
Write-Host  $RepoID
ForEach ($Id in $RepoID)
{

//Get latest commit info
$ProjectUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/commits?api-version=6.1-preview.1" 
$CommitInfo = (Invoke-RestMethod -Uri $ProjectUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$CommitID = $CommitInfo.value.commitId | Select-Object -first 1
Write-Host $CommitID
$CommitUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/commits/$($CommitID)?api-version=6.0-preview.1"
$LatestCommitInfo = (Invoke-RestMethod -Uri $CommitUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
Write-Host "LatestCommitInfo = $($LatestCommitInfo | ConvertTo-Json -Depth 100)"

//Get branch name and creatot
$BarchCreatorUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/refs?api-version=6.1-preview.1"
$CreateorInfo = (Invoke-RestMethod -Uri $BarchCreatorUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
Write-Host $CreateorInfo.value.name
Write-Host $CreateorInfo.value.creator.displayName
}

另一个选项(使用 Auth 部分更容易)是通过 AZ CLI 使用以下命令检索您的 Azure 存储库:

  1. 登录 Azure

    az login -t $tenant
    
  2. 首先配置organization和project的默认值,这个 将帮助您不指定 --project--organization 参数 在每个 az devops/repos 命令中:

    az devops configure -d organization=$organizationUrl project=$project
    
  3. 列出您的订阅和默认项目和组织的存储库

    az repos list --subscription $subscription