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

时间:2020-09-14 15:40:23

标签: powershell azure-devops azure-devops-rest-api

我在Azure DevOps中有许多项目。我希望能够遍历Azure DevOps中的所有存储库,并获得存储库的名称,存储库的创建者和上次更新/提交。

并在有人创建新回购通知时收到通知?

1 个答案:

答案 0 :(得分:1)

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

List all repositories并获取存储库的名称:

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

有人创建新的仓库时得到通知

我们无法创建此通知,当有人更新回购代码时我们会收到通知。请参考此链接以获取更多详细信息: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
}