gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:14:12 25 4
gpt4 key购买 nike

我在 Azure DevOps 中有很多项目。我希望能够遍历 Azure DevOps 中的所有存储库并获取存储库的名称、存储库的创建者和上次更新/提交。
并在有人创建新 Repo 时收到通知?

最佳答案

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

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 来获取 repo 创建者。

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


我们无法创建此通知,当有人更新 repo 代码时,我们可以收到通知。详情请引用此链接: 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
}

关于powershell - 如何在 Azure DevOps 中获取所有存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63887546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com