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
2 changes: 2 additions & 0 deletions .github/workflows/Process-PSModule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ jobs:
secrets: inherit
with:
SkipTests: SourceCode
Debug: true
Verbose: true
4 changes: 4 additions & 0 deletions src/classes/public/Context/GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
# github.com/Octocat
[string] $Name

# The display name of the context.
# Octocat
[string] $DisplayName

# The context type
# User / App / Installation
[string] $Type
Expand Down
12 changes: 12 additions & 0 deletions src/classes/public/Context/GitHubContext/AppGitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
# Client ID for GitHub Apps
[string] $ClientID

# Owner of the GitHub App
[string] $OwnerName

# Type of the owner of the GitHub App
[string] $OwnerType

# The permissions that the app is requesting on the target
[string[]] $Permissions

# The events that the app is subscribing to once installed
[string[]] $Events

# Creates a context object from a hashtable of key-vaule pairs.
AppGitHubContext([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
# The installation ID.
[int] $InstallationID

# The permissions that the app is requesting on the target
[string[]] $Permissions

# The events that the app is subscribing to once installed
[string[]] $Events

# The target type of the installation.
[string] $TargetType

# The target login of the installation.
[string] $TargetName

# Creates a context object from a hashtable of key-vaule pairs.
InstallationGitHubContext([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
Expand Down
10 changes: 5 additions & 5 deletions src/functions/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@
Write-Debug 'Response:'
$response | Out-String -Stream | ForEach-Object { Write-Debug $_ }
Write-Debug '---------------------------'
Write-Debug $headers.'Content-Type'
switch -Regex ($headers.'Content-Type') {
'application/json' {
$results = $response.Content | ConvertFrom-Json
}
'application/vnd.github.v3+json' {
'application/.*json' {
$results = $response.Content | ConvertFrom-Json
}
'text/plain' {
Expand All @@ -198,6 +194,10 @@
$results = [System.Text.Encoding]::UTF8.GetString($byteArray)
}
default {
if (-not $response.Content) {
$results = $null
break
}
Write-Warning "Unknown content type: $($headers.'Content-Type')"
Write-Warning 'Please report this issue!'
[byte[]]$byteArray = $response.Content
Expand Down
282 changes: 152 additions & 130 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,25 @@
InstallationID = $installation.id
Token = $token.Token
TokenExpirationDate = $token.ExpiresAt
Permissions = $installation.permissions
Events = $installation.events
TargetType = $installation.target_type
}
switch ($installation.target_type) {
'User' {
$contextParams['Owner'] = $installation.account.login
$contextParams += @{
TargetName = $installation.account.login
}
}
'Organization' {
$contextParams['Owner'] = $installation.account.login
$contextParams += @{
TargetName = $installation.account.login
}
}
'Enterprise' {
$contextParams['Enterprise'] = $installation.account.slug
$contextParams += @{
TargetName = $installation.account.slug
}
}
}
Write-Verbose 'Logging in using an installation access token...'
Expand Down
31 changes: 20 additions & 11 deletions src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ function Set-GitHubContext {
switch -Regex ($($Context['AuthType'])) {
'PAT|UAT|IAT' {
$viewer = Get-GitHubViewer -Context $Context
$login = $viewer.login
$viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$login = [string]$viewer.login
$Context += @{
Username = $login
NodeID = $viewer.id
DatabaseID = ($viewer.databaseId).ToString()
DisplayName = [string]$viewer.name
Username = $login
NodeID = [string]$viewer.id
DatabaseID = [string]$viewer.databaseId
}
}
'PAT|UAT' {
Expand All @@ -79,11 +81,16 @@ function Set-GitHubContext {
$app = Get-GitHubApp -Context $Context
$ContextName = "$($Context['HostName'])/$($app.slug)"
$Context += @{
Name = $ContextName
Username = $app.slug
NodeID = $app.node_id
DatabaseID = $app.id
Type = 'App'
Name = $ContextName
DisplayName = [string]$app.name
Username = [string]$app.slug
NodeID = [string]$app.node_id
DatabaseID = [string]$app.id
Permissions = [string]$app.permissions
Events = [string]$app.events
OwnerName = [string]$app.owner.login
OwnerType = [string]$app.owner.type
Type = 'App'
}
}
default {
Expand All @@ -92,8 +99,9 @@ function Set-GitHubContext {
}
Write-Verbose "Found [$($Context['Type'])] with login: [$($Context['Name'])]"
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }

Write-Verbose '----------------------------------------------------'
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
Write-Verbose "Saving context: [$($script:GitHub.Config.ID)/$($Context['Name'])]"
Set-Context -ID "$($script:GitHub.Config.ID)/$($Context['Name'])" -Context $Context
if ($Default) {
Set-GitHubDefaultContext -Context $Context['Name']
Expand All @@ -106,7 +114,8 @@ function Set-GitHubContext {
}
}
} catch {
throw ($_ -join ';')
Write-Error $_ | Select-Object *
throw 'Failed to set the GitHub context.'
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Auth/Get-GitHubViewer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
param(
# The fields to return.
[Parameter()]
[string[]] $Fields = @('login', 'id', 'databaseId'),
[string[]] $Fields = @('name', 'login', 'id', 'databaseId'),

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
Expand Down
34 changes: 22 additions & 12 deletions src/functions/public/GraphQL/Invoke-GitHubGraphQLQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
APIEndpoint = '/graphql'
Method = 'Post'
Body = @{
'query' = $query
'variables' = $variables
} | ConvertTo-Json
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
process {
$inputObject = @{
Context = $Context
APIEndpoint = '/graphql'
Method = 'Post'
Body = @{
'query' = $Query
'variables' = $Variables
} | ConvertTo-Json
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

end {
Write-Verbose "[$commandName] - End"
}
}