diff --git a/src/functions/private/Environments/Get-GitHubEnvironmentByName.ps1 b/src/functions/private/Environments/Get-GitHubEnvironmentByName.ps1 index 5669f5fee..dd5368509 100644 --- a/src/functions/private/Environments/Get-GitHubEnvironmentByName.ps1 +++ b/src/functions/private/Environments/Get-GitHubEnvironmentByName.ps1 @@ -86,23 +86,26 @@ filter Get-GitHubEnvironmentByName { Uri = $Context.ApiBaseUri + "/repos/$Owner/$Repository/environments/$encodedName" Context = $Context } - - Invoke-GitHubAPI @inputObject | ForEach-Object { - Write-Output $_.Response | ForEach-Object { - [GitHubEnvironment]@{ - Name = $_.name - DatabaseID = $_.id - NodeID = $_.node_id - Url = $_.html_url - Owner = $Owner - Repository = $Repository - CreatedAt = $_.created_at - UpdatedAt = $_.updated_at - CanAdminsBypass = $_.can_admins_bypass - ProtectionRules = $_.protection_rules - DeploymentBranchPolicy = $_.deployment_branch_policy + try { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response | ForEach-Object { + [GitHubEnvironment]@{ + Name = $_.name + DatabaseID = $_.id + NodeID = $_.node_id + Url = $_.html_url + Owner = $Owner + Repository = $Repository + CreatedAt = $_.created_at + UpdatedAt = $_.updated_at + CanAdminsBypass = $_.can_admins_bypass + ProtectionRules = $_.protection_rules + DeploymentBranchPolicy = $_.deployment_branch_policy + } } } + } catch { + return } } diff --git a/src/functions/public/Environments/Get-GitHubEnvironment.ps1 b/src/functions/public/Environments/Get-GitHubEnvironment.ps1 index ef54ea8c3..f679a056c 100644 --- a/src/functions/public/Environments/Get-GitHubEnvironment.ps1 +++ b/src/functions/public/Environments/Get-GitHubEnvironment.ps1 @@ -104,8 +104,12 @@ filter Get-GitHubEnvironment { } process { - Get-GitHubEnvironmentList -Owner $Owner -Repository $Repository -PerPage $PerPage -Context $Context | - Where-Object { $_.Name -like $Name } + if ($Name.Contains('*')) { + Get-GitHubEnvironmentList -Owner $Owner -Repository $Repository -PerPage $PerPage -Context $Context | + Where-Object { $_.Name -like $Name } + } else { + Get-GitHubEnvironmentByName -Owner $Owner -Repository $Repository -Name $Name -Context $Context + } } end { diff --git a/tests/Environments.Tests.ps1 b/tests/Environments.Tests.ps1 index 0d69552ce..0fd0a6ddf 100644 --- a/tests/Environments.Tests.ps1 +++ b/tests/Environments.Tests.ps1 @@ -17,270 +17,262 @@ BeforeAll { $os = $env:RUNNER_OS } -Describe 'As a user - Fine-grained PAT token - user account access (USER_FG_PAT)' { - BeforeAll { - Connect-GitHubAccount -Token $env:TEST_USER_USER_FG_PAT - $owner = 'psmodule-user' - $guid = [guid]::NewGuid().ToString() - $repo = "$repoSuffix-$guid" - New-GitHubRepository -Name $repo -AllowSquashMerge - } - AfterAll { - Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount - } - Context 'Environments' { - It 'Get-GitHubEnvironment - lists all environments' { +Describe 'Environments' { + + Context 'As a user - Fine-grained PAT token - user account access (USER_FG_PAT)' { + BeforeAll { + Connect-GitHubAccount -Token $env:TEST_USER_USER_FG_PAT + $owner = 'psmodule-user' + $guid = [guid]::NewGuid().ToString() + $repo = "$repoSuffix-$guid" + New-GitHubRepository -Name $repo -AllowSquashMerge + } + AfterAll { + Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount + } + It 'Get-GitHubEnvironment: should return an empty list when no environments exist' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving a non-existent environment' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } - - It 'Set-GitHubEnvironment - creates an environment' { + It 'Set-GitHubEnvironment: should successfully create an environment with a wait timer of 10' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -WaitTimer 10 $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $environmentName $result.ProtectionRules.wait_timer | Should -Be 10 } - - It 'Set-GitHubEnvironment - creates an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment that was created' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $environmentName + } + It 'Set-GitHubEnvironment: should successfully create an environment with a slash in its name' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be "$environmentName/$os" - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -Not -BeNullOrEmpty } - - It 'Remove-GitHubEnvironment - deletes an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be "$environmentName/$os" + } + It 'Remove-GitHubEnvironment: should delete the environment with a slash in its name without errors' { { Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Remove-GitHubEnvironment -Confirm:$false } | Should -Not -Throw - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $environmentName + It 'Get-GitHubEnvironment: should return null when retrieving the deleted environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - lists all environments' { + It 'Get-GitHubEnvironment: should list one remaining environment' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result.Count | Should -Be 1 } - - It 'Remove-GitHubEnvironment - deletes an environment' { + It 'Remove-GitHubEnvironment: should delete the remaining environment without errors' { { Remove-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -Confirm:$false } | Should -Not -Throw } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving an environment that does not exist' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } } -} -Describe 'As a user - Fine-grained PAT token - organization account access (ORG_FG_PAT)' { - BeforeAll { - Connect-GitHubAccount -Token $env:TEST_USER_ORG_FG_PAT - $owner = 'psmodule-test-org2' - $guid = [guid]::NewGuid().ToString() - $repo = "$repoSuffix-$guid" - New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge - } - AfterAll { - Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount - } - Context 'Environments' { - It 'Get-GitHubEnvironment - lists all environments' { + Context 'As a user - Fine-grained PAT token - organization account access (ORG_FG_PAT)' { + BeforeAll { + Connect-GitHubAccount -Token $env:TEST_USER_ORG_FG_PAT + $owner = 'psmodule-test-org2' + $guid = [guid]::NewGuid().ToString() + $repo = "$repoSuffix-$guid" + New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge + } + AfterAll { + Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount + } + It 'Get-GitHubEnvironment: should return an empty list when no environments exist' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving a non-existent environment' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } - - It 'Set-GitHubEnvironment - creates an environment' { + It 'Set-GitHubEnvironment: should successfully create an environment with a wait timer of 10' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -WaitTimer 10 $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $environmentName $result.ProtectionRules.wait_timer | Should -Be 10 } - - It 'Set-GitHubEnvironment - creates an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment that was created' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $environmentName + } + It 'Set-GitHubEnvironment: should successfully create an environment with a slash in its name' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be "$environmentName/$os" } - - It 'Remove-GitHubEnvironment - deletes an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be "$environmentName/$os" + } + It 'Remove-GitHubEnvironment: should delete the environment with a slash in its name without errors' { { Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Remove-GitHubEnvironment -Confirm:$false } | Should -Not -Throw - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $environmentName + It 'Get-GitHubEnvironment: should return null when retrieving the deleted environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - lists all environments' { + It 'Get-GitHubEnvironment: should list one remaining environment' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result.Count | Should -Be 1 } - - It 'Remove-GitHubEnvironment - deletes an environment' { + It 'Remove-GitHubEnvironment: should delete the remaining environment without errors' { { Remove-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -Confirm:$false } | Should -Not -Throw } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving an environment that does not exist' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } } -} -Describe 'As a user - Classic PAT token (PAT)' -Skip {} + Context 'As a user - Classic PAT token (PAT)' -Skip {} -Describe 'As GitHub Actions (GHA)' -Skip {} + Context 'As GitHub Actions (GHA)' -Skip {} -Describe 'As a GitHub App - Enterprise (APP_ENT)' { - BeforeAll { - Connect-GitHubAccount -ClientID $env:TEST_APP_ENT_CLIENT_ID -PrivateKey $env:TEST_APP_ENT_PRIVATE_KEY - $owner = 'psmodule-test-org3' - $guid = [guid]::NewGuid().ToString() - $repo = "$repoSuffix-$guid" - Connect-GitHubApp -Organization $owner -Default - New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge - } - AfterAll { - Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount - } - Context 'Environments' { - It 'Get-GitHubEnvironment - lists all environments' { + Context 'As a GitHub App - Enterprise (APP_ENT)' { + BeforeAll { + Connect-GitHubAccount -ClientID $env:TEST_APP_ENT_CLIENT_ID -PrivateKey $env:TEST_APP_ENT_PRIVATE_KEY + $owner = 'psmodule-test-org3' + $guid = [guid]::NewGuid().ToString() + $repo = "$repoSuffix-$guid" + Connect-GitHubApp -Organization $owner -Default + New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge + } + AfterAll { + Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount + } + It 'Get-GitHubEnvironment: should return an empty list when no environments exist' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving a non-existent environment' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } - - It 'Set-GitHubEnvironment - creates an environment' { + It 'Set-GitHubEnvironment: should successfully create an environment with a wait timer of 10' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -WaitTimer 10 $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $environmentName $result.ProtectionRules.wait_timer | Should -Be 10 } - - It 'Set-GitHubEnvironment - creates an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment that was created' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $environmentName + } + It 'Set-GitHubEnvironment: should successfully create an environment with a slash in its name' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be "$environmentName/$os" - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -Not -BeNullOrEmpty } - - It 'Remove-GitHubEnvironment - deletes an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be "$environmentName/$os" + } + It 'Remove-GitHubEnvironment: should delete the environment with a slash in its name without errors' { { Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Remove-GitHubEnvironment -Confirm:$false } | Should -Not -Throw - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $environmentName + It 'Get-GitHubEnvironment: should return null when retrieving the deleted environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - lists all environments' { + It 'Get-GitHubEnvironment: should list one remaining environment' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result.Count | Should -Be 1 } - - It 'Remove-GitHubEnvironment - deletes an environment' { + It 'Remove-GitHubEnvironment: should delete the remaining environment without errors' { { Remove-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -Confirm:$false } | Should -Not -Throw } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving an environment that does not exist' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } } -} -Describe 'As a GitHub App - Organization (APP_ORG)' { - BeforeAll { - Connect-GitHubAccount -ClientID $env:TEST_APP_ORG_CLIENT_ID -PrivateKey $env:TEST_APP_ORG_PRIVATE_KEY - $owner = 'psmodule-test-org' - $guid = [guid]::NewGuid().ToString() - $repo = "$repoSuffix-$guid" - Connect-GitHubApp -Organization $owner -Default - New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge - } - AfterAll { - Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount - } - Context 'Environments' { - It 'Get-GitHubEnvironment - lists all environments' { + Context 'As a GitHub App - Organization (APP_ORG)' { + BeforeAll { + Connect-GitHubAccount -ClientID $env:TEST_APP_ORG_CLIENT_ID -PrivateKey $env:TEST_APP_ORG_PRIVATE_KEY + $owner = 'psmodule-test-org' + $guid = [guid]::NewGuid().ToString() + $repo = "$repoSuffix-$guid" + Connect-GitHubApp -Organization $owner -Default + New-GitHubRepository -Owner $owner -Name $repo -AllowSquashMerge + } + AfterAll { + Remove-GitHubRepository -Owner $owner -Name $repo -Confirm:$false + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount + } + It 'Get-GitHubEnvironment: should return an empty list when no environments exist' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving a non-existent environment' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } - - It 'Set-GitHubEnvironment - creates an environment' { + It 'Set-GitHubEnvironment: should successfully create an environment with a wait timer of 10' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -WaitTimer 10 $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $environmentName $result.ProtectionRules.wait_timer | Should -Be 10 } - - It 'Set-GitHubEnvironment - creates an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment that was created' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $environmentName + } + It 'Set-GitHubEnvironment: should successfully create an environment with a slash in its name' { $result = Set-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be "$environmentName/$os" - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -Not -BeNullOrEmpty } - - It 'Remove-GitHubEnvironment - deletes an environment with a slash in the name' { + It 'Get-GitHubEnvironment: should retrieve the environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be "$environmentName/$os" + } + It 'Remove-GitHubEnvironment: should delete the environment with a slash in its name without errors' { { Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Remove-GitHubEnvironment -Confirm:$false } | Should -Not -Throw - Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - retrieves a specific environment' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $environmentName + It 'Get-GitHubEnvironment: should return null when retrieving the deleted environment with a slash in its name' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name "$environmentName/$os" + $result | Should -BeNullOrEmpty } - - It 'Get-GitHubEnvironment - lists all environments' { + It 'Get-GitHubEnvironment: should list one remaining environment' { $result = Get-GitHubEnvironment -Owner $owner -Repository $repo $result.Count | Should -Be 1 } - - It 'Remove-GitHubEnvironment - deletes an environment' { + It 'Remove-GitHubEnvironment: should delete the remaining environment without errors' { { Remove-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName -Confirm:$false } | Should -Not -Throw } - - It 'Get-GitHubEnvironment - retrieves a specific environment that does not exist yet' { - $result = Get-GitHubEnvironment -Owner $owner -Repository $repo | Where-Object { $_.Name -eq $environmentName } + It 'Get-GitHubEnvironment: should return null when retrieving an environment that does not exist' { + $result = Get-GitHubEnvironment -Owner $owner -Repository $repo -Name $environmentName $result | Should -BeNullOrEmpty } }