How to Retrieve a List of Azure DevOps Repositories Sharing Similar Branch Policies on their Develop Branch?

95 views Asked by At

I'm working with Azure DevOps and have multiple repositories with various branches, each having specific branch policies. Specifically, I want to identify repositories that share the same branch policies on their develop branch. What would be the most effective way to retrieve a list of Azure DevOps repositories that have identical branch policies applied to their develop branch? Any insights or guidance on achieving this would be greatly appreciated

For example we had a branch policy named build validation, now I need list of repo which has the build validation branch policy.

Got a tough that we can achieve this via Rest api but I am not sure

2

There are 2 answers

0
yoav lax On

You can get this using Azure DevOps Rest API. I would do this in the following way:

  1. Create a $Desired_Policy argument as example to compare to (A repository you know that has the policies), you should use the following API to retrieve:

    "{org_url}/_apis/policy/configurations?repositoryId={repo_id}&api-version=6.0"

  2. Get all repositories using the following API:

    "{org_url}/_apis/git/repositories?api-version=6.0"

  3. Loop over all repositories from step 2 , get their branch policies as described in step 1 and compare to the desired example from step 1, the results you can add to an array that will store all similar repositories.

If you don't know, a "desired" example, you can go with a different implementation using the API's above.

1
Alvin Zhao - MSFT On

We can use the API below to list all types of branch policies.

GET https://dev.azure.com/$organization/$project/_apis/policy/types

Per the branch policy type of Build Validation, its unique type.id should be 0609b952-1397-4640-95ec-e00a01b2c241.

Thus we can use APIs to Get all repos from a project -> Get all the branch policies configured for develop branch from each repo in a loop -> If any of the develop branch is enabled with Build Validation type of policy, we can proceed to print the repo name out.

Here is a PowerShell Script for your reference.

$organization = "YourADOOrgName"
$project = "ProjectName"
$refName = "refs/heads/develop"
$MyPat = 'xxxxxx'
# Convert PAT to Base64
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))

# Define headers
$headers = @{
    'Authorization' = 'Basic ' + $B64Pat
    'Content-Type' = 'application/json'
}

# Define API URL
$reposUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=7.2-preview.1"

# Get all repositories in the project
$reposResponse = Invoke-RestMethod -Method Get -Uri $reposUrl -Headers $headers


Write-Host "The develop branch of each repo below enables Build Validation Policy:"
# Loop through each repository
foreach ($repo in $reposResponse.value) {
    $repoName = $repo.name
    $repoId = $repo.id

    # Get branch policies for the develop branch of each repo
    $policiesUrl = "https://dev.azure.com/$organization/$project/_apis/git/policy/configurations?repositoryId=$repoId&refName=$refName&api-version=7.2-preview.1"
    $policiesResponse = Invoke-RestMethod -Method Get -Uri $policiesUrl -Headers $headers

    # Check if any policy matches the specified type ID
    foreach ($policy in $policiesResponse.value) {
        if ($policy.Type.id -eq "0609b952-1397-4640-95ec-e00a01b2c241") {
            Write-Host "Repository Name: $($repoName), Repository ID: $($repoId)"
            break  # Exit the loop after finding the first matching policy
        }
    }
}

enter image description here