Skip to content

Commit 968e822

Browse files
🚀 [Refactor]: Add null checks for Permissions and App properties in GitHubAppContext constructor
1 parent 7e39c35 commit 968e822

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

‎src/classes/public/Context/GitHubContext/GitHubAppContext.ps1‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@
5050
$this.KeyVaultKeyReference = $Object.KeyVaultKeyReference
5151
$this.OwnerName = $Object.OwnerName
5252
$this.OwnerType = $Object.OwnerType
53-
$this.Permissions = [GitHubPermission]::NewPermissionList($Object.Permissions)
53+
if ($Object.Permissions) {
54+
$this.Permissions = [GitHubPermission]::NewPermissionList($Object.Permissions)
55+
}
5456
$this.Events = , ($Object.Events)
55-
$this.App = [GitHubApp]::New($Object.App)
57+
if ($Object.App) {
58+
$this.App = [GitHubApp]::New($Object.App)
59+
}
5660
}
5761
}

‎src/functions/private/Auth/Context/Set-GitHubContext.ps1‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
}
4545

4646
process {
47-
Write-Debug 'Context:'
48-
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
47+
if ($DebugPreference -eq 'Continue') {
48+
Write-Debug 'Context:'
49+
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
50+
Write-Debug "Getting info on the context [$($contextObj['AuthType'])]."
51+
}
4952

5053
# Run functions to get info on the temporary context.
5154
try {
52-
Write-Debug "Getting info on the context [$($contextObj['AuthType'])]."
5355
switch -Regex (($contextObj['AuthType'])) {
5456
'PAT|UAT|IAT' {
5557
$viewer = Get-GitHubViewer -Context $contextObj
@@ -79,9 +81,9 @@
7981
try {
8082
$app = Get-GitHubApp -Slug $contextObj['Username'] -Context $contextObj
8183
$contextObj['DisplayName'] = [string]$app.Name
82-
# TODO: $contextObj['App'] = $app
84+
$contextObj['App'] = $app
8385
} catch {
84-
Write-Warning "Failed to get the GitHub App with the slug: [$($contextObj['Username'])]."
86+
Write-Warning "Unable to get the GitHub App: [$($contextObj['Username'])]."
8587
}
8688
}
8789
if ($script:IsGitHubActions) {
@@ -116,9 +118,6 @@
116118
}
117119
$contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" +
118120
"$($contextObj['InstallationType'])/$($contextObj['InstallationName'])"
119-
} else {
120-
$contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" +
121-
"$($contextObj['InstallationType'])/$($contextObj['InstallationName'])"
122121
}
123122
}
124123
'App' {
@@ -132,18 +131,20 @@
132131
$contextObj['Events'] = [string[]]$app.Events
133132
$contextObj['OwnerName'] = [string]$app.Owner.Name
134133
$contextObj['OwnerType'] = [string]$app.Owner.Type
135-
$contextObj['App'] = $app
134+
$contextObj['App'] = [GitHubApp]$app
136135
$contextObj['Type'] = 'App'
137136
}
138137
default {
139138
throw 'Failed to get info on the context. Unknown logon type.'
140139
}
141140
}
142-
Write-Debug "Found [$($contextObj['Type'])] with login: [$($contextObj['Name'])]"
143-
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
144-
Write-Debug '----------------------------------------------------'
145-
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
141+
if ($DebugPreference -eq 'Continue') {
142+
Write-Debug "Found [$($contextObj['Type'])] with login: [$($contextObj['Name'])]"
143+
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
144+
Write-Debug '----------------------------------------------------'
146145
Write-Debug "Saving context: [$($contextObj['Name'])]"
146+
}
147+
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
147148
Set-Context -ID $($contextObj['Name']) -Context $contextObj -Vault $script:GitHub.ContextVault
148149
if ($Default) {
149150
Switch-GitHubContext -Context $contextObj['Name']

0 commit comments

Comments
 (0)