Skip to content
17 changes: 17 additions & 0 deletions examples/Actions/Variables.ps1
Original file line number Diff line number Diff line change
@@ -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
49 changes: 45 additions & 4 deletions src/functions/public/Variables/Get-GitHubVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -202,19 +214,48 @@ 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
if ($Name.Contains('*')) {
$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
}

Expand Down
7 changes: 1 addition & 6 deletions tests/Variables.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down