diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index ff2c897fd..0bc23c040 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -101,23 +101,6 @@ $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') } - # $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText - # # Swap out this by using the -Authentication Bearer -Token $AccessToken - # switch -Regex ($AccessTokenAsPlainText) { - # '^ghp_|^github_pat_' { - # $headers.authorization = "token $AccessTokenAsPlainText" - # } - # '^ghu_|^gho_' { - # $headers.authorization = "Bearer $AccessTokenAsPlainText" - # } - # default { - # $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*' - # $errorMessage = "Unexpected AccessToken format: $tokenPrefix" - # Write-Error $errorMessage - # throw $errorMessage - # } - # } - $APICall = @{ Uri = $URI Method = $Method @@ -166,7 +149,7 @@ Request: $($APICall | ConvertFrom-HashTable | Format-List | Out-String) ---------------------------------- ResponseHeaders: -$($responseHeaders.PSObject.Properties | Foreach-Object { $_ | Format-List | Out-String }) +$($responseHeaders.PSObject.Properties | ForEach-Object { $_ | Format-List | Out-String }) ---------------------------------- "@ diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index a54d58268..d3e824479 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -18,6 +18,12 @@ Connects to GitHub using a device flow login. If the user has already logged in, the access token will be refreshed. + .EXAMPLE + $env:GH_TOKEN = 'ghx_1234567890' + Connect-GitHubAccount + + Connects to GitHub using the access token from environment variable, assuming unattended mode. + .EXAMPLE Connect-GitHubAccount -AccessToken ! Enter your personal access token: ************* @@ -93,14 +99,14 @@ ) $envVars = Get-ChildItem -Path 'Env:' - Write-Verbose "Environment variables:" + Write-Verbose 'Environment variables:' Write-Verbose ($envVars | Format-Table -AutoSize | Out-String) - $systemToken = $envVars | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 - Write-Verbose "System token: [$systemToken]" - $systemTokenPresent = $systemToken.count -gt 0 - Write-Verbose "System token present: [$systemTokenPresent]" - $AuthType = $systemTokenPresent ? 'sPAT' : $PSCmdlet.ParameterSetName - WRite-Verbose "AuthType: [$AuthType]" + $gitHubToken = $envVars | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 + Write-Verbose "GitHub token: [$gitHubToken]" + $gitHubTokenPresent = $gitHubToken.count -gt 0 + Write-Verbose "GitHub token present: [$gitHubTokenPresent]" + $AuthType = $gitHubTokenPresent ? 'sPAT' : $PSCmdlet.ParameterSetName + Write-Verbose "AuthType: [$AuthType]" switch ($AuthType) { 'DeviceFlow' { Write-Verbose 'Logging in using device flow...' @@ -197,11 +203,11 @@ break } 'sPAT' { - Write-Verbose 'Logging in using system access token...' + Write-Verbose 'Logging in using GitHub access token...' Reset-GitHubConfig -Scope 'Auth' - $prefix = $systemToken.Value -replace '_.*$', '_*' + $prefix = $gitHubToken.Value -replace '_.*$', '_*' $settings = @{ - AccessToken = ConvertTo-SecureString -AsPlainText $systemToken.Value + AccessToken = ConvertTo-SecureString -AsPlainText $gitHubToken.Value AccessTokenType = $prefix ApiBaseUri = 'https://api.github.com' ApiVersion = '2022-11-28'