Skip to content

Commit 7b954c9

Browse files
🩹 [Patch]: Minor refactor and docs (#92)
## Description - Added example for unattended flow of `Connect-GitHubAccount` - Remove comment and format `Invoke-GitHubAPI`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent da23ee1 commit 7b954c9

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/GitHub/public/API/Invoke-GitHubAPI.ps1

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,6 @@
101101
$URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '')
102102
}
103103

104-
# $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText
105-
# # Swap out this by using the -Authentication Bearer -Token $AccessToken
106-
# switch -Regex ($AccessTokenAsPlainText) {
107-
# '^ghp_|^github_pat_' {
108-
# $headers.authorization = "token $AccessTokenAsPlainText"
109-
# }
110-
# '^ghu_|^gho_' {
111-
# $headers.authorization = "Bearer $AccessTokenAsPlainText"
112-
# }
113-
# default {
114-
# $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*'
115-
# $errorMessage = "Unexpected AccessToken format: $tokenPrefix"
116-
# Write-Error $errorMessage
117-
# throw $errorMessage
118-
# }
119-
# }
120-
121104
$APICall = @{
122105
Uri = $URI
123106
Method = $Method
@@ -166,7 +149,7 @@ Request:
166149
$($APICall | ConvertFrom-HashTable | Format-List | Out-String)
167150
----------------------------------
168151
ResponseHeaders:
169-
$($responseHeaders.PSObject.Properties | Foreach-Object { $_ | Format-List | Out-String })
152+
$($responseHeaders.PSObject.Properties | ForEach-Object { $_ | Format-List | Out-String })
170153
----------------------------------
171154
172155
"@

src/GitHub/public/Auth/Connect-GitHubAccount.ps1

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
Connects to GitHub using a device flow login.
1919
If the user has already logged in, the access token will be refreshed.
2020
21+
.EXAMPLE
22+
$env:GH_TOKEN = 'ghx_1234567890'
23+
Connect-GitHubAccount
24+
25+
Connects to GitHub using the access token from environment variable, assuming unattended mode.
26+
2127
.EXAMPLE
2228
Connect-GitHubAccount -AccessToken
2329
! Enter your personal access token: *************
@@ -93,14 +99,14 @@
9399
)
94100

95101
$envVars = Get-ChildItem -Path 'Env:'
96-
Write-Verbose "Environment variables:"
102+
Write-Verbose 'Environment variables:'
97103
Write-Verbose ($envVars | Format-Table -AutoSize | Out-String)
98-
$systemToken = $envVars | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1
99-
Write-Verbose "System token: [$systemToken]"
100-
$systemTokenPresent = $systemToken.count -gt 0
101-
Write-Verbose "System token present: [$systemTokenPresent]"
102-
$AuthType = $systemTokenPresent ? 'sPAT' : $PSCmdlet.ParameterSetName
103-
WRite-Verbose "AuthType: [$AuthType]"
104+
$gitHubToken = $envVars | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1
105+
Write-Verbose "GitHub token: [$gitHubToken]"
106+
$gitHubTokenPresent = $gitHubToken.count -gt 0
107+
Write-Verbose "GitHub token present: [$gitHubTokenPresent]"
108+
$AuthType = $gitHubTokenPresent ? 'sPAT' : $PSCmdlet.ParameterSetName
109+
Write-Verbose "AuthType: [$AuthType]"
104110
switch ($AuthType) {
105111
'DeviceFlow' {
106112
Write-Verbose 'Logging in using device flow...'
@@ -197,11 +203,11 @@
197203
break
198204
}
199205
'sPAT' {
200-
Write-Verbose 'Logging in using system access token...'
206+
Write-Verbose 'Logging in using GitHub access token...'
201207
Reset-GitHubConfig -Scope 'Auth'
202-
$prefix = $systemToken.Value -replace '_.*$', '_*'
208+
$prefix = $gitHubToken.Value -replace '_.*$', '_*'
203209
$settings = @{
204-
AccessToken = ConvertTo-SecureString -AsPlainText $systemToken.Value
210+
AccessToken = ConvertTo-SecureString -AsPlainText $gitHubToken.Value
205211
AccessTokenType = $prefix
206212
ApiBaseUri = 'https://api.github.com'
207213
ApiVersion = '2022-11-28'

0 commit comments

Comments
 (0)