如何通过 Azure DevOps REST 获取分叉列表 API - Forks - List

How to get the list of forks via Azure DevOps REST API - Forks - List

如何通过 Azure DevOps REST API 获取与特定存储库相关的 分支列表

我正在查看文档:

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/forks/list?view=azure-devops-server-rest-5.0#teamprojectcollectionreference

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

但是,没有提供示例。特别是,我无法弄清楚 {collectionid} 是什么以及在哪里可以获得它。

我与此形成对比,例如,按照以下 GET 方法列出回购没有问题:

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

PowerShell 脚本:

$AzureDevOpsPAT = "<PAT>"
$OrganizationName = "<OrganizationName>"
$Project = "<ProjectName>"

$UriOrga = "https://dev.azure.com/$($OrganizationName)/"
$UriProj = $UriOrga + "$($Project)/"
$RepoName = "<RepoName>"

$uriRepo = $UriProj + "_apis/git/repositories/$RepoName"

$uriListRepos = $uriRepo + "?api-version=5.1"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)"))}

$Forks = (Invoke-RestMethod -Uri $uriListRepos -Method get -Headers $AzureDevOpsAuthenicationHeader) | Write-Host

但是 "Repositories - List" 公式看起来与 "Forks - List".

有点不同

非常感谢您帮助列出 Forks。

非常有趣的问题。 {collectionid} 已从 TFS 世界移出,在 Azure DevOps 上是您的组织的 ID。您可以通过 Accounts - List. Accounts - List uses the ID of a team member (for the ownerId or memberId properties) that you can find with Teams - Get Team Members With Extended Properties.

找到的组织 ID

这里再多解释一下,让大家更熟悉这个api.

中使用的参数

您尝试使用的 api 用于 检索 从您关注的相应回购中分叉的那些回购的详细信息 ({repositoryNameOrId} ).

在 Azure devops 中,我们仅在组织范围内支持分支回购。换句话说,你可以将 repo 从 projectA 分叉到 projectB,但不能从 orgA 分叉到 orgB.

所以,基于上面的工作逻辑,当我们想检查相关的repo是否曾经被fork过,以及fork去向的详细信息时。我们需要告诉系统应该搜索哪个组织。

这是您应该注入 api 的参数:collectionId。也许我们现在可以称它为organization id


除了Sharmrai上面提到的apis,它是可用的但容易遇到PAT问题,因为它们属于帐户级别。我会为您提供另一个 PAT token 就可以的。

POST https://dev.azure.com/{any-org-name-you-can-visit}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1

请求正文:

{
    "contributionIds": ["ms.vss-features.my-organizations-data-provider"],
    "dataProviderContext":
        {
            "properties":{}
        }
}

然后您将获得您可以访问的所有组织信息。

使用 Merlin 的逻辑,我们可以将您的原始 PowerShell 中的有效示例组合在一起。

$AzureDevOpsUser = "<UsernameEmail>"
$AzureDevOpsPAT = "<PAT>"
$OrganizationName = "<OrganizationName>"
$Project = "<ProjectName>"
$RepoName = "<RepoName>"

$UriOrga = "https://dev.azure.com/$($OrganizationName)/"
$UriProj = $UriOrga + "$($Project)/"
$uriRepo = $UriProj + "_apis/git/repositories/$RepoName"


$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($AzureDevOpsUser):$($AzureDevOpsPAT)")))


$uriOrgId = $uriOrga + "_apis/Contribution/HierarchyQuery?api-version=5.0-preview"
$body = "{`"contributionIds`": [`"ms.vss-features.my-organizations-data-provider`"],`"dataProviderContext`": {`"properties`": {}}}"
$orgs = (Invoke-RestMethod $uriOrgId -Method 'POST' -Headers $headers -Body $body).dataProviders."ms.vss-features.my-organizations-data-provider".organizations
$collectionId = ($orgs.Where( { ($_.name) -eq $OrganizationName })).Id

$uriForks = $uriRepo + "/forks/$($collectionId)?api-version=5.1-preview" ;
$uriForks

$forks = (Invoke-RestMethod -Uri $uriForks -Method get -Headers $headers)

Write-Output $forks | ConvertTo-Json