Skip to main content

Using Azure DevOps REST API with PowerShell - Part 2

· 4 min read
Hasan Gural

In our previous article, we did some pre-coding to access Azure DevOps REST API. We got all the projects of a DevOps Organization as a list by executing an elementary request. Now, we should enlarge our knowledge with further examples.

Let's do an example where we try to access all the repositories as a list in an Azure DevOps Project. This is how the repository list looks from the interface of the AzDo.

We will continue to use the PowerShell script we created in the first part of the series. You can see the endpoints for the repository on the Azure DevOps API Reference page.

Operation types for Repositories

Operation NameOperation Detail
CreateCreate a git repository in a team project.
DeleteDelete a git repository
RecycleBinRetrieve and estore soft-deleted Git repository.
GetRetrieve a git repository.
ReletedrepositoriesRetrieve deleted git repositories.
ListRetrieve git repositories.
RestoreRepository From Recycle Bin
UpdateUpdates the Git repository with either a new repo name or a new default branch.
info

You can reach the details of all endpoints in the link address below. For now, we will use the List operation in the example. https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories?view=azure-devops-rest-6.0

Get all repository for spesific AzDo Project

This is our first query on API in this example.

$OrganizationName = "hasangural" # Azure DevOps OrganizationName
$ProjectName = "GBC-TerraformDemo"
$AzDoToken = "PAT-TOKEN-HERE" # Azure DevOps Personal Access Token (PAT)

# Authentcation Header for AzDo REST Endpoint.
$AuthenicationHeader = @{Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$AzDoToken")) }

# Generating environment variables

Set-Variable -Name "azDoAuthHeader" -Value $AuthenicationHeader -Scope Global -ErrorAction Stop
Set-Variable -Name "azDoBaseUrl" -Value "https://dev.azure.com/$OrganizationName" -Scope Global -ErrorAction Stop

$reposURI = "$azDoBaseUrl/$($ProjectName)/_apis/git/repositories?api-version=6.0"

# Rest API params
$restParams = @{

"URI" = $reposURI
"Headers" = $azDoAuthHeader
"Method" = "GET"
"ErrorAction" = "Stop"

}

$getResponse = Invoke-RestMethod @restParams

$getResponse.value | Select-Object id, name, defaultBranch

In the blue section, we created a request by assigning reposURI to the repository endpoint of REST API in PowerShell. After this query, we can get the complete list of repositories in an Azure DevOps Project. We can get the RepositoryId, Name, and defaultBranch sections on the PowerShell console by configuring the parameters.

Query all branches for all repositories in AzDo Project

In this example, we try to execute different REST API requests to get the branches of repositories in an Azure DevOps Project.

caution

The PowerShell script block I added below starts with the same lines and contains two different REST API requests. Thus we can see all the repositories in an Azure DevOps Project first, and then we get the list of all branches by entering a different endpoint refs.

$OrganizationName = "hasangural" # Azure DevOps OrganizationName
$ProjectName = "GBC-TerraformDemo"
$AzDoToken = "PAT-TOKEN-HERE" # Azure DevOps Personal Access Token (PAT)

# Authentcation Header for AzDo REST Endpoint.
$AuthenicationHeader = @{Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$AzDoToken")) }

# Generating environment variables

Set-Variable -Name "azDoAuthHeader" -Value $AuthenicationHeader -Scope Global -ErrorAction Stop
Set-Variable -Name "azDoBaseUrl" -Value "https://dev.azure.com/$OrganizationName" -Scope Global -ErrorAction Stop

$reposURI = "$azDoBaseUrl/$($ProjectName)/_apis/git/repositories?api-version=6.0"

# Rest API params
$restParams = @{

"URI" = $reposURI
"Headers" = $azDoAuthHeader
"Method" = "GET"
"ErrorAction" = "Stop"

}

$getRepos = Invoke-RestMethod @restParams

ForEach ($repo in $getRepos.value) {

$RepoId = $repo.id
$RepoName = $repo.name

$branchRequestUri = "$azDoBaseUrl/$($ProjectName)/_apis/git/repositories/$($repoId)/refs?api-version=6.0"

$getOutput = Invoke-RestMethod -Uri $branchRequestUri -Method Get -Headers $azDoAuthHeader

$getOutput.value | ForEach-Object {

Write-Output '````````````````````````````````````````````````````````````'
Write-Output "[INFO] RepositoryId: $($repoId)"
Write-Output "[INFO] RepositoryName: $($RepoName)"
Write-Output "[INFO] BranchName: $($_.Name)"
Write-Output '````````````````````````````````````````````````````````````'

}
}

You first notice that we reach the repository endpoint afterward; we send all the repository names to the refs endpoint to return the branch names. If you pay attention, it is clear that we use two ForEach blocks to display needed information.

The result on the PowerShell Console should be like the image below. We can improve these examples to get more specific information. For instance, for the sake of clean details, we can eliminate branches that had no commits more than 90 days automatically.