Skip to content

Commit 7d908e3

Browse files
authored
Fix issue with unintialized repos (#120)
* Fix issue with unintialized repos
1 parent 19e334b commit 7d908e3

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Repos.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ Function Get-AzDevOpsBranches {
8080
Write-Verbose "URI: $uri"
8181
try {
8282
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
83-
$statsResponse = Invoke-RestMethod -Uri $statsUri -Method Get -Headers $header
8483
# If the response is a string and not an object, throw an exception for authentication failure or project not found
8584
if ($response -is [string]) {
8685
throw "Authentication failed or project not found"
@@ -89,9 +88,19 @@ Function Get-AzDevOpsBranches {
8988
catch {
9089
throw $_.Exception.Message
9190
}
91+
try {
92+
$statsResponse = Invoke-RestMethod -Uri $statsUri -Method Get -Headers $header
93+
}
94+
catch {
95+
$statsResponse = $null
96+
}
9297
$result = @($response.value | ForEach-Object {
9398
$branch = $_
94-
$branchStats = $statsResponse.value | Where-Object {$_.name -eq ($branch.name -replace "refs/heads/","")}
99+
If($null -eq $statsResponse) {
100+
$branchStats = $null
101+
} else {
102+
$branchStats = $statsResponse.value | Where-Object {$_.name -eq ($branch.name -replace "refs/heads/","")}
103+
}
95104
$branch | Add-Member -MemberType NoteProperty -Name Stats -Value $branchStats
96105
$branch
97106
})

tests/DevOps.Repos.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ Describe "Functions: DevOps.Repos.Tests" {
8787
}
8888
}
8989

90+
Context " Get-AzDevOpsBranches on an empty repository" {
91+
BeforeAll {
92+
Connect-AzDevOps -Organization $env:ADO_ORGANIZATION -PAT $env:ADO_PAT
93+
$repos = Get-AzDevOpsRepos -Project "empty-project"
94+
$branches = Get-AzDevOpsBranches -Project "empty-project" -Repository $repos[0].id
95+
}
96+
97+
It " should return an empty list of branches" {
98+
$branches | Should -BeNullOrEmpty
99+
}
100+
}
101+
90102
Context " Get-AzDevOpsBranches with wrong parameters" {
91103
It " should throw an error with a wrong PAT" {
92104
Connect-AzDevOps -Organization $env:ADO_ORGANIZATION -PAT $env:ADO_PAT

0 commit comments

Comments
 (0)