@@ -42,6 +42,55 @@ Function Get-AzDevOpsRepos {
4242Export-ModuleMember - Function Get-AzDevOpsRepos
4343# End of Function Get-AzDevOpsRepos
4444
45+ <#
46+ . SYNOPSIS
47+ Get Azure DevOps branches for a repo
48+
49+ . DESCRIPTION
50+ Get Azure DevOps branches for a repo using Azure DevOps Rest API
51+
52+ . PARAMETER Project
53+ Project name for Azure DevOps
54+
55+ . PARAMETER Repository
56+ Repository name for Azure DevOps
57+
58+ . EXAMPLE
59+ Get-AzDevOpsBranches -Project $Project -Repository $Repository
60+ #>
61+ Function Get-AzDevOpsBranches {
62+ [CmdletBinding ()]
63+ [OutputType ([System.Object []])]
64+ param (
65+ [Parameter (Mandatory )]
66+ [string ]
67+ $Project ,
68+ [Parameter (Mandatory )]
69+ [string ]
70+ $Repository
71+ )
72+ if ($null -eq $script :connection ) {
73+ throw " Not connected to Azure DevOps. Run Connect-AzDevOps first"
74+ }
75+ $Organization = $script :connection.Organization
76+ $header = $script :connection.GetHeader ()
77+ Write-Verbose " Getting branches for repo $Repository in project $Project "
78+ $uri = " https://dev.azure.com/$Organization /_apis/git/repositories/$Repository /refs?filter=heads&api-version=7.2-preview.2"
79+ Write-Verbose " URI: $uri "
80+ try {
81+ $response = Invoke-RestMethod - Uri $uri - Method Get - Headers $header
82+ # If the response is a string and not an object, throw an exception for authentication failure or project not found
83+ if ($response -is [string ]) {
84+ throw " Authentication failed or project not found"
85+ }
86+ }
87+ catch {
88+ throw $_.Exception.Message
89+ }
90+ return @ ($response.value )
91+ }
92+ Export-ModuleMember - Function Get-AzDevOpsBranches
93+
4594<#
4695 . SYNOPSIS
4796 Get Azure DevOps branch policy for a branch in a repo
@@ -381,6 +430,26 @@ function Export-AzDevOpsReposAndBranchPolicies {
381430 $readmeExists = ((Test-AzDevOpsFileExists - Project $Project - Repository $repo.id - Path " README.md" ) -or (Test-AzDevOpsFileExists - Project $Project - Repository $repo.id - Path " README" ))
382431 $repo | Add-Member - MemberType NoteProperty - Name ReadmeExists - Value $readmeExists
383432
433+ # Get all branches for the repo
434+ $branches = Get-AzDevOpsBranches - Project $Project - Repository $repo.id
435+ # add branch policies for each branch to the branches object
436+ $branches = $branches | ForEach-Object {
437+ $branch = $_
438+ $branchPolicy = @ (Get-AzDevOpsBranchPolicy - Project $Project - Repository $repo.id - Branch $branch.name )
439+ $branch | Add-Member - MemberType NoteProperty - Name BranchPolicy - Value $branchPolicy
440+ $branch
441+ }
442+ # Add an ObjectType Azure.DevOps.Repo.Branch to each branch object
443+ $branches = $branches | ForEach-Object {
444+ $branch = $_
445+ $branch | Add-Member - MemberType NoteProperty - Name ObjectType - Value " Azure.DevOps.Repo.Branch"
446+ # Add ObjectName to branch object
447+ $branch | Add-Member - MemberType NoteProperty - Name ObjectName - Value (" {0}.{1}.{2}.{3}" -f $Organization , $Project , $repo.name , $branch.name )
448+ $branch
449+ }
450+
451+
452+
384453 # Add a property indicating if a file named LICENSE or LICENSE.md exists in the repo
385454 $licenseExists = ((Test-AzDevOpsFileExists - Project $Project - Repository $repo.id - Path " LICENSE" ) -or (Test-AzDevOpsFileExists - Project $Project - Repository $repo.id - Path " LICENSE.md" ))
386455 $repo | Add-Member - MemberType NoteProperty - Name LicenseExists - Value $licenseExists
@@ -404,8 +473,9 @@ function Export-AzDevOpsReposAndBranchPolicies {
404473 }
405474
406475 # Export repo object to JSON file
407- Write-Verbose " Exporting repo $ ( $repo.name ) to JSON as file $ ( $repo.name ) .ado.repo.json"
408- $repo | ConvertTo-Json - Depth 100 | Out-File - FilePath " $OutputPath \$ ( $repo.name ) .ado.repo.json"
476+ Write-Verbose " Exporting repo $ ( $repo.name ) and its branches to JSON as file $ ( $repo.name ) .ado.repo.json"
477+ $branches += $repo
478+ $branches | ConvertTo-Json - Depth 100 | Out-File - FilePath " $OutputPath \$ ( $repo.name ) .ado.repo.json"
409479 }
410480 }
411481}
0 commit comments