diff --git a/src/functions/private/Auth/Cli/Connect-GithubCli.ps1 b/src/functions/private/Auth/Cli/Connect-GithubCli.ps1 new file mode 100644 index 000000000..ccba21046 --- /dev/null +++ b/src/functions/private/Auth/Cli/Connect-GithubCli.ps1 @@ -0,0 +1,57 @@ +filter Connect-GitHubCli { + <# + .SYNOPSIS + Authenticates to GitHub CLI using a secure token from the provided context. + + .DESCRIPTION + This function takes an input context containing authentication details and logs into GitHub CLI (`gh auth login`). + It extracts the token from the provided context and passes it securely to the authentication command. + If authentication fails, a warning is displayed, and `LASTEXITCODE` is reset to `0`. + + .EXAMPLE + $context = Connect-GitHubAccount + $context | Connect-GitHubCli + + Output: + ```powershell + (No output unless an error occurs) + ``` + + Logs into GitHub CLI using the provided authentication token and hostname from the context. + + .OUTPUTS + void + + .NOTES + The function does not return any output. It logs into GitHub CLI using the provided context. + #> + [OutputType([void])] + [CmdletBinding()] + param( + # The context to run the command in. + [Parameter( + Mandatory, + ValueFromPipeline + )] + [object] $Context + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + } + + process { + $Context.Token | ConvertFrom-SecureString -AsPlainText | gh auth login --with-token --hostname $Context.HostName + if ($LASTEXITCODE -ne 0) { + Write-Warning 'Unable to log on with the GitHub Cli.' + $Global:LASTEXITCODE = 0 + Write-Debug "Resetting LASTEXITCODE: $LASTEXITCODE" + return + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} diff --git a/src/functions/private/Auth/Context/Set-GitHubContext.ps1 b/src/functions/private/Auth/Context/Set-GitHubContext.ps1 index dee980fea..060e67604 100644 --- a/src/functions/private/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Set-GitHubContext.ps1 @@ -148,8 +148,11 @@ function Set-GitHubContext { if ($Default) { Set-GitHubDefaultContext -Context $contextObj['Name'] } - if ($contextObj['AuthType'] -eq 'IAT' -and $script:GitHub.EnvironmentType -eq 'GHA') { - Set-GitHubGitConfig -Context $contextObj['Name'] + if ($script:GitHub.EnvironmentType -eq 'GHA') { + if ($contextObj['AuthType'] -eq 'IAT') { + Set-GitHubGitConfig -Context $contextObj['Name'] + } + Connect-GitHubCli -Context $contextObj } if ($PassThru) { Get-GitHubContext -Context $($contextObj['Name']) diff --git a/src/loader.ps1 b/src/loader.ps1 index ff3be03fd..482284262 100644 --- a/src/loader.ps1 +++ b/src/loader.ps1 @@ -7,6 +7,8 @@ switch ($script:GitHub.EnvironmentType) { Write-Verbose 'Detected running on a GitHub Actions runner, preparing environment...' $env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY -replace '.+/' Set-GitHubEnv -Name 'GITHUB_REPOSITORY_NAME' -Value $env:GITHUB_REPOSITORY_NAME + $env:GITHUB_HOST_NAME = ($env:GITHUB_SERVER_URL ?? 'github.com') -replace '^https?://' + Set-GitHubEnv -Name 'GITHUB_HOST_NAME' -Value $env:GITHUB_HOST_NAME Import-GitHubEventData Import-GitHubRunnerData } diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index b9027da66..31997304e 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -59,6 +59,7 @@ Describe 'GitHub' { Context 'Auth' { It 'Connect-GitHubAccount - Connects GitHub Actions without parameters' { { Connect-GitHubAccount } | Should -Not -Throw + [string]::IsNullOrEmpty($(gh auth token)) | Should -Be $false } It 'Disconnect-GitHubAccount - Disconnects GitHub Actions' { { Disconnect-GitHubAccount } | Should -Not -Throw