diff --git a/src/functions/public/Git/Get-GitHubGitConfig.ps1 b/src/functions/public/Git/Get-GitHubGitConfig.ps1 index c2af75eed..823b0aa4e 100644 --- a/src/functions/public/Git/Get-GitHubGitConfig.ps1 +++ b/src/functions/public/Git/Get-GitHubGitConfig.ps1 @@ -13,7 +13,11 @@ #> [OutputType([pscustomobject])] [CmdletBinding()] - param() + param( + [Parameter()] + [ValidateSet('local', 'global', 'system')] + [string] $Scope = 'local' + ) begin { $stackPath = Get-PSCallStackPath @@ -26,7 +30,7 @@ $gitExists = Get-Command -Name 'git' -ErrorAction SilentlyContinue Write-Debug "GITEXISTS: $gitExists" if (-not $gitExists) { - Write-Verbose "Git is not installed. Cannot get git configuration." + Write-Verbose 'Git is not installed. Cannot get git configuration.' return } @@ -40,14 +44,23 @@ return } - git config --local --list | ForEach-Object { - ( - [pscustomobject]@{ - Name = $_.Split('=')[0] - Value = $_.Split('=')[1] - } - ) + $config = @{} + git config --$Scope --list | ConvertFrom-StringData | ForEach-Object { + $config += $_ } + $result = @{} + $config.GetEnumerator() | ForEach-Object { + $name = $_.Key + $value = $_.Value + if ($value -match '(?i)AUTHORIZATION:\s*(?[^\s]+)\s+(?.*)') { + $secret = $matches['token'] + Add-GitHubMask -Value $secret + } + $result += @{ + $name = $value + } + } + [pscustomobject]$result } catch { throw $_ } diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 76f783313..5aac16676 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -769,10 +769,26 @@ Describe 'As GitHub Actions (GHA)' { } } Context 'Git' { + It "Get-GitHubGitConfig gets the 'local' (default) Git configuration (GHA)" { + $gitConfig = Get-GitHubGitConfig + Write-Verbose ($gitConfig | Format-List | Out-String) -Verbose + $gitConfig | Should -Not -BeNullOrEmpty + } + It "Get-GitHubGitConfig gets the 'global' Git configuration (GHA)" { + git config --global advice.pushfetchfirst false + $gitConfig = Get-GitHubGitConfig -Scope 'global' + Write-Verbose ($gitConfig | Format-List | Out-String) -Verbose + $gitConfig | Should -Not -BeNullOrEmpty + } + It "Get-GitHubGitConfig gets the 'system' Git configuration (GHA)" { + $gitConfig = Get-GitHubGitConfig -Scope 'system' + Write-Verbose ($gitConfig | Format-List | Out-String) -Verbose + $gitConfig | Should -Not -BeNullOrEmpty + } It 'Set-GitHubGitConfig sets the Git configuration (GHA)' { { Set-GitHubGitConfig } | Should -Not -Throw $gitConfig = Get-GitHubGitConfig - Write-Verbose ($gitConfig | Format-Table | Out-String) -Verbose + Write-Verbose ($gitConfig | Format-List | Out-String) -Verbose $gitConfig | Should -Not -BeNullOrEmpty $gitConfig.'user.name' | Should -Not -BeNullOrEmpty