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
6 changes: 2 additions & 4 deletions src/GitHub/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,6 +106,7 @@
StatusCodeVariable = 'APICallStatusCode'
ResponseHeadersVariable = 'APICallResponseHeaders'
}

$APICall | Remove-HashTableEntries -NullOrEmptyValues

if ($Body) {
Expand Down
34 changes: 26 additions & 8 deletions src/GitHub/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand All @@ -95,7 +96,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..."
Expand Down Expand Up @@ -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'
Expand All @@ -181,15 +182,32 @@
}
}

if ($AuthType -ne 'sPAT') {
$user = Get-GitHubUser
$username = $user.login
Set-GitHubConfig -UserName $username
} else {
$username = 'system'
}

Write-Host '✓ ' -ForegroundColor Green -NoNewline
Write-Host 'Logged in to GitHub!'
Write-Host "Logged in as $username!"


$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
}

}
24 changes: 19 additions & 5 deletions tools/utilities/GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
1 change: 1 addition & 0 deletions tools/utilities/Local-Testing.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down