diff --git a/examples/Actions/Variables.ps1 b/examples/Actions/Variables.ps1 new file mode 100644 index 000000000..2c510693d --- /dev/null +++ b/examples/Actions/Variables.ps1 @@ -0,0 +1,17 @@ +$owner = 'PSModule' +$repo = 'GitHub' +$environment = 'test' + +Set-GitHubEnvironment -Owner $Owner -Repository $Repo -Name $environment + +Set-GitHubVariable -Owner $Owner -Name 'TestVariable' -Value 'Organization' -Visibility all +Get-GitHubVariable -Owner $Owner -IncludeInherited # Should have the value 'Organization' + +Set-GitHubVariable -Owner $Owner -Repository $Repo -Name 'TestVariable' -Value 'Repository' +Get-GitHubVariable -Owner $Owner -Repository $Repo -IncludeInherited # Should have the value 'Repository' + +Set-GitHubVariable -Owner $Owner -Repository $Repo -Environment $environment -Name 'TestVariable' -Value 'Environment' +Get-GitHubVariable -Owner $Owner -Repository $Repo -Environment $environment -IncludeInherited # Should have the value 'Environment' +Get-GitHubVariable -Owner $Owner -Repository $Repo -Environment $environment -IncludeInherited -All + +Get-GitHubVariable -Owner $Owner -Repository $Repo -Environment $environment -IncludeInherited -All -Name 'Test*' | Remove-GitHubVariable diff --git a/src/functions/public/Variables/Get-GitHubVariable.ps1 b/src/functions/public/Variables/Get-GitHubVariable.ps1 index ceb091599..cdd9e5a09 100644 --- a/src/functions/public/Variables/Get-GitHubVariable.ps1 +++ b/src/functions/public/Variables/Get-GitHubVariable.ps1 @@ -150,6 +150,14 @@ function Get-GitHubVariable { [Parameter()] [switch] $IncludeInherited, + # List all variables, including those that are overwritten by inheritance. + [Parameter()] + [switch] $All, + + # Set the environment variables locally for the current session. + [Parameter()] + [switch] $SetLocalEnvironment, + # The context to run the command in. Used to get the details for the API call. # Can be either a string or a GitHubContext object. [Parameter()] @@ -175,7 +183,9 @@ function Get-GitHubVariable { $variables += Get-GitHubVariableOwnerList @params | Where-Object { $_.Name -like $Name } } else { - $variables += Get-GitHubVariableOwnerByName @params -Name $Name + try { + $variables += Get-GitHubVariableOwnerByName @params -Name $Name + } catch { $null } } break } @@ -189,7 +199,9 @@ function Get-GitHubVariable { $variables += Get-GitHubVariableRepositoryList @params | Where-Object { $_.Name -like $Name } } else { - $variables += Get-GitHubVariableRepositoryByName @params -Name $Name + try { + $variables += Get-GitHubVariableRepositoryByName @params -Name $Name + } catch { $null } } break } @@ -202,7 +214,9 @@ function Get-GitHubVariable { $variables += Get-GitHubVariableRepositoryList @params | Where-Object { $_.Name -like $Name } } else { - $variables += Get-GitHubVariableRepositoryByName @params -Name $Name + try { + $variables += Get-GitHubVariableRepositoryByName @params -Name $Name + } catch { $null } } } $params['Environment'] = $Environment @@ -210,11 +224,38 @@ function Get-GitHubVariable { $variables += Get-GitHubVariableEnvironmentList @params | Where-Object { $_.Name -like $Name } } else { - $variables += Get-GitHubVariableEnvironmentByName @params -Name $Name + try { + $variables += Get-GitHubVariableEnvironmentByName @params -Name $Name + } catch { $null } } break } } + + if ($IncludeInherited -and -not $All) { + $variables = $variables | Group-Object -Property Name | ForEach-Object { + $group = $_.Group + $envVar = $group | Where-Object { $_.Environment } + if ($envVar) { + $envVar + } else { + $repoVar = $group | Where-Object { $_.Repository -and (-not $_.Environment) } + if ($repoVar) { + $repoVar + } else { + $group | Where-Object { (-not $_.Repository) -and (-not $_.Environment) } + } + } + } + } + + if ($SetLocalEnvironment) { + Write-Debug 'Set local environment variables:' + $variables | ForEach-Object { + [System.Environment]::SetEnvironmentVariable($_.Name, $_.Value, 'Process') + Write-Debug "$($_.Name) = $($_.Value)" + } + } $variables } diff --git a/tests/Variables.Tests.ps1 b/tests/Variables.Tests.ps1 index c1ca2c444..82edd7347 100644 --- a/tests/Variables.Tests.ps1 +++ b/tests/Variables.Tests.ps1 @@ -200,12 +200,7 @@ Describe 'Environments' { $after.Count | Should -Be 0 } - Context 'SelectedRepository' { - BeforeEach { - LogGroup 'Sleep 15 seconds' { - Start-Sleep -Seconds 15 - } - } + Context 'SelectedRepository' -Tag 'Flaky' { It 'Get-GitHubVariableSelectedRepository - gets a list of selected repositories' { LogGroup "SelectedRepositories - [$varName]" { $result = Get-GitHubVariableSelectedRepository -Owner $owner -Name $varName