Skip to content

Commit

Permalink
Align para and logging/debug
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Dec 15, 2024
1 parent 77ac2d7 commit e5cb53c
Show file tree
Hide file tree
Showing 85 changed files with 2,241 additions and 1,213 deletions.
24 changes: 14 additions & 10 deletions src/functions/private/Apps/Get-GitHubAppByName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Debug "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}

process {
$inputObject = @{
Context = $Context
APIEndpoint = "/apps/$AppSlug"
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
try {
$inputObject = @{
Context = $Context
APIEndpoint = "/apps/$AppSlug"
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
} catch {
throw $_
}
}
end {
Write-Verbose "[$commandName] - End"
Write-Debug "[$commandName] - End"
}
}
32 changes: 24 additions & 8 deletions src/functions/private/Apps/Get-GitHubAuthenticatedApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,31 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
APIEndpoint = '/app'
Method = 'GET'
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
process {
try {
$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
APIEndpoint = '/app'
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
} catch {
throw $_
}
}
end {
Write-Debug "[$commandName] - End"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Debug "[$commandName] - Start"
}

process {
Expand All @@ -39,6 +39,6 @@
}

end {
Write-Verbose "[$commandName] - End"
Write-Debug "[$commandName] - End"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ filter Remove-GitHubContext {

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Debug "[$commandName] - Start"
$null = Get-GitHubConfig
}

Expand All @@ -44,6 +44,6 @@ filter Remove-GitHubContext {
}

end {
Write-Verbose "[$commandName] - End"
Write-Debug "[$commandName] - End"
}
}
48 changes: 26 additions & 22 deletions src/functions/private/Auth/Context/Resolve-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,41 @@

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Debug "[$commandName] - Start"
Initialize-GitHubConfig
}

process {
if ($Context -is [string]) {
$contextName = $Context
Write-Verbose "Getting context: [$contextName]"
$Context = Get-GitHubContext -Context $contextName
}
try {
if ($Context -is [string]) {
$contextName = $Context
Write-Debug "Getting context: [$contextName]"
$Context = Get-GitHubContext -Context $contextName
}

if (-not $Context) {
throw "Please provide a valid context or log in using 'Connect-GitHub'."
}
if (-not $Context) {
throw "Please provide a valid context or log in using 'Connect-GitHub'."
}

# switch ($Context.Type) {
# 'App' {
# $availableContexts = Get-GitHubContext -ListAvailable |
# Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID }
# $params = Get-FunctionParameter -Scope 2
# Write-Verbose 'Resolving parameters used in called function'
# Write-Verbose ($params | Out-String)
# if ($params.Keys -in 'Owner', 'Organization') {
# $Context = $availableContexts | Where-Object { $_.Owner -eq $params.Owner }
# }
# }
# }
# switch ($Context.Type) {
# 'App' {
# $availableContexts = Get-GitHubContext -ListAvailable |
# Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID }
# $params = Get-FunctionParameter -Scope 2
# Write-Debug 'Resolving parameters used in called function'
# Write-Debug ($params | Out-String)
# if ($params.Keys -in 'Owner', 'Organization') {
# $Context = $availableContexts | Where-Object { $_.Owner -eq $params.Owner }
# }
# }
# }
} catch {
throw $_
}
}

end {
Write-Verbose "[$commandName] - End"
Write-Debug "[$commandName] - End"
Write-Output $Context
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ function Set-GitHubContext {

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Debug "[$commandName] - Start"
$null = Get-GitHubConfig
$contextObj = @{} + $Context
}

process {
Write-Verbose 'Context:'
$contextObj | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
Write-Debug 'Context:'
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }

# Run functions to get info on the temporary context.
try {
Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]."
Write-Debug "Getting info on the context [$($contextObj['AuthType'])]."
switch -Regex ($($contextObj['AuthType'])) {
'PAT|UAT|IAT' {
$viewer = Get-GitHubViewer -Context $contextObj
$viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$viewer | Out-String -Stream | ForEach-Object { Write-Debug $_ }
if ([string]::IsNullOrEmpty($contextObj['DisplayName'])) {
$contextObj['DisplayName'] = [string]$viewer.name
}
Expand Down Expand Up @@ -94,12 +94,12 @@ function Set-GitHubContext {
$owner = $gitHubEvent.repository.owner.login
$repo = $gitHubEvent.repository.name
$gh_sender = $gitHubEvent.sender.login # sender is an automatic variable in Powershell
Write-Verbose "Enterprise: $enterprise"
Write-Verbose "Organization: $organization"
Write-Verbose "Repository: $repo"
Write-Verbose "Repository Owner: $owner"
Write-Verbose "Repository Owner Type: $targetType"
Write-Verbose "Sender: $gh_sender"
Write-Debug "Enterprise: $enterprise"
Write-Debug "Organization: $organization"
Write-Debug "Repository: $repo"
Write-Debug "Repository Owner: $owner"
Write-Debug "Repository Owner Type: $targetType"
Write-Debug "Sender: $gh_sender"
if ([string]::IsNullOrEmpty($contextObj['Enterprise'])) {
$contextObj['Enterprise'] = [string]$enterprise
}
Expand Down Expand Up @@ -139,11 +139,11 @@ function Set-GitHubContext {
throw 'Failed to get info on the context. Unknown logon type.'
}
}
Write-Verbose "Found [$($contextObj['Type'])] with login: [$($contextObj['Name'])]"
$contextObj | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
Write-Verbose '----------------------------------------------------'
Write-Debug "Found [$($contextObj['Type'])] with login: [$($contextObj['Name'])]"
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
Write-Debug '----------------------------------------------------'
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
Write-Verbose "Saving context: [$($script:GitHub.Config.ID)/$($contextObj['Name'])]"
Write-Debug "Saving context: [$($script:GitHub.Config.ID)/$($contextObj['Name'])]"
Set-Context -ID "$($script:GitHub.Config.ID)/$($contextObj['Name'])" -Context $contextObj
if ($Default) {
Set-GitHubDefaultContext -Context $contextObj['Name']
Expand All @@ -164,6 +164,6 @@ function Set-GitHubContext {
}

end {
Write-Verbose "[$commandName] - End"
Write-Debug "[$commandName] - End"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,40 @@
[securestring] $RefreshToken
)

do {
if ($RefreshToken) {
$tokenResponse = Wait-GitHubAccessToken -ClientID $ClientID -RefreshToken $RefreshToken -HostName $HostName
} else {
$deviceCodeResponse = Request-GitHubDeviceCode -ClientID $ClientID -Scope $Scope -HostName $HostName
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
}

$deviceCode = $deviceCodeResponse.device_code
$interval = $deviceCodeResponse.interval
$userCode = $deviceCodeResponse.user_code
$verificationUri = $deviceCodeResponse.verification_uri
process {
try {
do {
if ($RefreshToken) {
$tokenResponse = Wait-GitHubAccessToken -ClientID $ClientID -RefreshToken $RefreshToken -HostName $HostName
} else {
$deviceCodeResponse = Request-GitHubDeviceCode -ClientID $ClientID -Scope $Scope -HostName $HostName

Write-Host '! ' -ForegroundColor DarkYellow -NoNewline
Write-Host "We added the code to your clipboard: [$userCode]"
$userCode | Set-Clipboard
Read-Host "Press Enter to open $HostName in your browser..."
Start-Process $verificationUri
$deviceCode = $deviceCodeResponse.device_code
$interval = $deviceCodeResponse.interval
$userCode = $deviceCodeResponse.user_code
$verificationUri = $deviceCodeResponse.verification_uri

$tokenResponse = Wait-GitHubAccessToken -DeviceCode $deviceCode -ClientID $ClientID -Interval $interval -HostName $HostName
Write-Host '! ' -ForegroundColor DarkYellow -NoNewline
Write-Host "We added the code to your clipboard: [$userCode]"
$userCode | Set-Clipboard
Read-Host "Press Enter to open $HostName in your browser..."
Start-Process $verificationUri

$tokenResponse = Wait-GitHubAccessToken -DeviceCode $deviceCode -ClientID $ClientID -Interval $interval -HostName $HostName
}
} while ($tokenResponse.error)
$tokenResponse
} catch {
throw $_
}
} while ($tokenResponse.error)
$tokenResponse
}

end {
Write-Debug "[$commandName] - End"
}
}
63 changes: 37 additions & 26 deletions src/functions/private/Auth/DeviceFlow/Request-GitHubAccessToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,51 @@
[string] $HostName
)

$body = @{
'client_id' = $ClientID
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
}

if ($PSBoundParameters.ContainsKey('RefreshToken')) {
$body += @{
'refresh_token' = (ConvertFrom-SecureString $RefreshToken -AsPlainText)
'grant_type' = 'refresh_token'
process {
$body = @{
'client_id' = $ClientID
}
}

if ($PSBoundParameters.ContainsKey('DeviceCode')) {
$body += @{
'device_code' = $DeviceCode
'grant_type' = 'urn:ietf:params:oauth:grant-type:device_code'
if ($PSBoundParameters.ContainsKey('RefreshToken')) {
$body += @{
'refresh_token' = (ConvertFrom-SecureString $RefreshToken -AsPlainText)
'grant_type' = 'refresh_token'
}
}
}

$RESTParams = @{
Uri = "https://$HostName/login/oauth/access_token"
Method = 'POST'
Body = $body
Headers = @{ 'Accept' = 'application/json' }
}
if ($PSBoundParameters.ContainsKey('DeviceCode')) {
$body += @{
'device_code' = $DeviceCode
'grant_type' = 'urn:ietf:params:oauth:grant-type:device_code'
}
}

try {
Write-Debug ($RESTParams.GetEnumerator() | Out-String)
$RESTParams = @{
Uri = "https://$HostName/login/oauth/access_token"
Method = 'POST'
Body = $body
Headers = @{ 'Accept' = 'application/json' }
}

$tokenResponse = Invoke-RestMethod @RESTParams -Verbose:$false
try {
Write-Debug ($RESTParams.GetEnumerator() | Out-String)

$tokenResponse = Invoke-RestMethod @RESTParams -Verbose:$false

Write-Debug ($tokenResponse | ConvertTo-Json | Out-String)
return $tokenResponse
} catch {
Write-Error $_
throw $_
}
}

Write-Debug ($tokenResponse | ConvertTo-Json | Out-String)
return $tokenResponse
} catch {
Write-Error $_
throw $_
end {
Write-Debug "[$commandName] - End"
}
}
Loading

0 comments on commit e5cb53c

Please sign in to comment.