From 0ac424736e646bfbd6564c3ea52f79d1673f8970 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 20:17:29 +0200 Subject: [PATCH 1/4] Added welcome + ensure that connect allows setting default, even if logged in --- src/GitHub/public/Auth/Connect-GitHubAccount.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index a9f8f77ba..dc1a874fa 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -95,7 +95,7 @@ if ($accessTokenValidity.TotalHours -gt 4) { Write-Host '✓ ' -ForegroundColor Green -NoNewline Write-Host "Access token is still valid for $accessTokenValidityText ..." - return + break } else { Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..." @@ -181,8 +181,11 @@ } } + $user = Get-GitHubUser + Set-GitHubConfig -UserName $user.login + Write-Host '✓ ' -ForegroundColor Green -NoNewline - Write-Host 'Logged in to GitHub!' + Write-Host "Logged in as $($user.login)!" if ($Owner) { Set-GitHubConfig -Owner $Owner From 69b6ea198651a9cf1346798be30be78d10ab5738 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 20:17:34 +0200 Subject: [PATCH 2/4] test --- tools/utilities/GitHubAPI.ps1 | 24 +++++++++++++++++++----- tools/utilities/Local-Testing.ps1 | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 530c90274..49bfe0808 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -15,11 +15,11 @@ $response.'x-webhooks' # Webhooks/event docs $response.paths.psobject.Properties | Select-Object ` Name, ` - @{n = 'Get'; e = { (($_.value.psobject.Properties.Name) -contains 'Get') } }, ` - @{n = 'Post'; e = { (($_.value.psobject.Properties.Name) -contains 'Post') } }, ` - @{n = 'Delete'; e = { (($_.value.psobject.Properties.Name) -contains 'Delete') } }, ` - @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` - @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | format-table +@{n = 'Get'; e = { (($_.value.psobject.Properties.Name) -contains 'Get') } }, ` +@{n = 'Post'; e = { (($_.value.psobject.Properties.Name) -contains 'Post') } }, ` +@{n = 'Delete'; e = { (($_.value.psobject.Properties.Name) -contains 'Delete') } }, ` +@{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` +@{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table $path = '/user/blocks/{username}' $method = 'get' @@ -39,3 +39,17 @@ $response.components.parameters.username # -> Param $response.paths.$path.$method.responses # -> Could be used to decide error handling within the function $response.paths.$path.$method.responses.'200'.content.'application/json'.schema # -> OutputType qualifyer $response.paths.$path.$method.responses.'200'.content.'application/json'.schema.items # -> OutputType + + +$response.components.schemas.PSobject.Properties | ForEach-Object { + [pscustomobject]@{ + Name = $_.Name + Title = $_.Value.title + Type = $_.Value.type + Properties = $_.Value.properties + Required = $_.Value.required + } +} + + +$response.components.schemas.'issue-comment' | ConvertTo-Json diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index 567aa43ee..dd395151d 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -16,6 +16,7 @@ Import-Module -Name GitHub -Verbose Get-Command -Module GitHub Clear-Host Connect-GitHubAccount +Connect-GitHubAccount -Owner 'MariusStorhaug' -Repo 'ResourceModules' Connect-GitHubAccount -Mode OAuthApp Connect-GitHubAccount -AccessToken Get-GitHubConfig From 45f474a648bbd041ff5578b6e5d85f5e531ba5eb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 20:33:30 +0200 Subject: [PATCH 3/4] Auto select runner repo if not specified --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 6 ++-- .../public/Auth/Connect-GitHubAccount.ps1 | 28 +++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 6945c056e..374cf2868 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -62,11 +62,8 @@ # The GitHub API version to be used. By default, it pulls from a configuration script variable. [Parameter()] - [string] $Version = (Get-GitHubConfig -Name ApiVersion), + [string] $Version = (Get-GitHubConfig -Name ApiVersion) - # Declares the state of a resource by passing all parameters/body properties to Invoke-RestMethod, even if empty - [Parameter()] - [switch] $Declare ) $functionName = $MyInvocation.MyCommand.Name @@ -109,6 +106,7 @@ StatusCodeVariable = 'APICallStatusCode' ResponseHeadersVariable = 'APICallResponseHeaders' } + $APICall | Remove-HashTableEntries -NullOrEmptyValues if ($Body) { diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index dc1a874fa..d4a687702 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -73,9 +73,10 @@ [string] $Repo ) - $envVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 - $envVarPresent = $envVar.count -gt 0 - $AuthType = $envVarPresent ? 'sPAT' : $PSCmdlet.ParameterSetName + $envVars = Get-ChildItem -Path 'Env:' + $systemToken = $envVars | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 + $systemTokenPresent = $systemToken.count -gt 0 + $AuthType = $systemTokenPresent ? 'sPAT' : $PSCmdlet.ParameterSetName switch ($AuthType) { 'DeviceFlow' { @@ -169,9 +170,9 @@ 'sPAT' { Write-Verbose 'Logging in using system access token...' Reset-GitHubConfig -Scope 'Auth' - $prefix = $envVar.Value -replace '_.*$', '_*' + $prefix = $systemToken.Value -replace '_.*$', '_*' $settings = @{ - AccessToken = ConvertTo-SecureString -AsPlainText $envVar.Value + AccessToken = ConvertTo-SecureString -AsPlainText $systemToken.Value AccessTokenType = $prefix ApiBaseUri = 'https://api.github.com' ApiVersion = '2022-11-28' @@ -181,18 +182,29 @@ } } - $user = Get-GitHubUser - Set-GitHubConfig -UserName $user.login + if ($AuthType -ne 'sPAT') { + $user = Get-GitHubUser + Set-GitHubConfig -UserName $user.login + } Write-Host '✓ ' -ForegroundColor Green -NoNewline Write-Host "Logged in as $($user.login)!" + + $systemRepo = $envVars | Where-Object Name -EQ 'GITHUB_REPOSITORY' + $systemRepoPresent = $systemRepo.count -gt 0 + if ($Owner) { Set-GitHubConfig -Owner $Owner + } elseif ($systemRepoPresent) { + $owner = $systemRepo.Value.Split('/')[0] + Set-GitHubConfig -Owner $owner } if ($Repo) { Set-GitHubConfig -Repo $Repo + } elseif ($systemRepoPresent) { + $repo = $systemRepo.Value.Split('/')[-1] + Set-GitHubConfig -Repo $repo } - } From 4885e7b625593f02fe60a947fe419d4328fe0739 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 20:36:45 +0200 Subject: [PATCH 4/4] Fix login for system --- src/GitHub/public/Auth/Connect-GitHubAccount.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index d4a687702..cb935b8e8 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -184,11 +184,14 @@ if ($AuthType -ne 'sPAT') { $user = Get-GitHubUser - Set-GitHubConfig -UserName $user.login + $username = $user.login + Set-GitHubConfig -UserName $username + } else { + $username = 'system' } Write-Host '✓ ' -ForegroundColor Green -NoNewline - Write-Host "Logged in as $($user.login)!" + Write-Host "Logged in as $username!" $systemRepo = $envVars | Where-Object Name -EQ 'GITHUB_REPOSITORY'