Reputation: 303
How can I get the list of forks via Azure DevOps REST API related to a particular repo?
I was checking the doc on this:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryNameOrId}/forks/{collectionId}?api-version=5.1-preview.1
However, there are no examples provided. In particular, I cannot figure out what {collectionid}
is and where I can get it.
I contrast to this, for example, I have no issues with listing repos following the below GET method:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1
The PowerShell script:
$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
But the "Repositories - List" formula looks a bit differenly from "Forks - List".
Your help with listing Forks would be very appreciated.
Upvotes: 1
Views: 792
Reputation: 376
Using the logic from Merlin we can put together a worked example from your original 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
Upvotes: 1
Reputation: 19026
Just do more explanation here, so that you could get further familiar the parameters used on this api.
The api you are trying to use is used to retrieve the details information of those repos that forked from the corresponding repos you concerned ({repositoryNameOrId}
).
In Azure devops, we only support fork repo in organization scope. In another word, you can fork repo to from projectA
to projectB
, but cannot fork from orgA
to orgB
.
So, based on above work logic, when we want to check if the concerned repo has ever been forked, along with the details of where the fork went. We need tell the system which organization should be searched.
This is the parameter you should inject into api: collectionId
. Maybe we can call it organization id
now.
Except the apis that Sharmrai mentioned above, it is available but easily faced issue with PAT, because they are belong to account level. I would provide you another that just PAT token
would be ok.
POST https://dev.azure.com/{any-org-name-you-can-visit}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1
Request body:
{
"contributionIds": ["ms.vss-features.my-organizations-data-provider"],
"dataProviderContext":
{
"properties":{}
}
}
Then you will get all organizations info that you can access.
Upvotes: 2
Reputation: 16133
Really interesting question. {collectionid}
was moved from the TFS world and on Azure DevOps is the ID of your organization. The organization ID you can find with 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.
Upvotes: 0