Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/functions/private/Auth/Cli/Connect-GithubCli.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 5 additions & 2 deletions src/functions/private/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
2 changes: 2 additions & 0 deletions src/loader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down