powershell 如何在Azure DevOps中获取所有回购

yc0p9oo0  于 2022-11-29  发布在  Shell
关注(0)|答案(3)|浏览(142)

我在Azure DevOps中有许多项目。我希望能够迭代Azure DevOps中的所有Repo,并获取Repo的名称、Repo的创建者和上次更新/提交时间。
并得到一个通知时,有人创建了新的回购?

wdebmtf2

wdebmtf21#

我们可以通过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
}
63lcw9qa

63lcw9qa2#

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

az login -t $tenant

1.首先为组织和项目配置默认值,这有助于您不在每个az devops/repos命令中指定--project--organization参数:

az devops configure -d organization=$organizationUrl project=$project

1.列出您的订阅以及默认项目和组织的存储库

az repos list --subscription $subscription
epfja78i

epfja78i3#

Install-Module -Name VSTeam 
$pat="YYYY"
Set-VSTeamAccount https://dev.azure.com/dXXXX/ -PersonalAccessToken $pat
Get-VSTeamGitRepository

相关问题