diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Extensions/Invoke-ExecExtensionTest.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Extensions/Invoke-ExecExtensionTest.ps1 index cd71c2024660..a0c36e11d199 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Extensions/Invoke-ExecExtensionTest.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Extensions/Invoke-ExecExtensionTest.ps1 @@ -83,9 +83,13 @@ Function Invoke-ExecExtensionTest { $Results = [pscustomobject]@{'Results' = 'Successfully Connected to HIBP' } } 'GitHub' { - $GitHubResponse = Invoke-GitHubApiRequest -Method 'GET' -Path 'user' + $GitHubResponse = Invoke-GitHubApiRequest -Method 'GET' -Path 'user' -ReturnHeaders if ($GitHubResponse.login) { - $Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login)" } + if ($GitHubResponse.Headers.'x-oauth-scopes') { + $Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login) with scopes: $($GitHubResponse.Headers.'x-oauth-scopes')" } + } else { + $Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login) using a Fine Grained PAT" } + } } else { $Results = [pscustomobject]@{ 'Results' = 'Failed to connect to GitHub. Check your API credentials and try again.' } } diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListBPATemplates.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListBPATemplates.ps1 index 25ef55b55f8c..26d39d4773c9 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListBPATemplates.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListBPATemplates.ps1 @@ -28,14 +28,15 @@ Function Invoke-ListBPATemplates { } $Filter = "PartitionKey eq 'BPATemplate'" - $Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter).JSON | ConvertFrom-Json + $Templates = Get-CIPPAzDataTableEntity @Table -Filter $Filter if ($Request.Query.RawJson) { $Templates } else { $Templates = $Templates | ForEach-Object { - $Template = $_ + $Template = $_.JSON | ConvertFrom-Json @{ + GUID = $_.GUID Data = $Template.fields Name = $Template.Name Style = $Template.Style diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecGitHubAction.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecGitHubAction.ps1 index cba461e61906..76c6c9e26bf6 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecGitHubAction.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecGitHubAction.ps1 @@ -43,8 +43,15 @@ function Invoke-ExecGitHubAction { $Results = @(Get-GitHubBranch @SplatParams) } 'GetOrgs' { - $Orgs = Invoke-GitHubApiRequest -Path 'user/orgs' - $Results = @($Orgs) + try { + $Orgs = Invoke-GitHubApiRequest -Path 'user/orgs' + $Results = @($Orgs) + } catch { + $Results = @{ + resultText = 'You may not have permission to view organizations, check your PAT scopes and try again - {0}' -f $_.Exception.Message + state = 'error' + } + } } 'GetFileTree' { $Files = (Get-GitHubFileTree @SplatParams).tree | Where-Object { $_.path -match '.json$' } | Select-Object *, @{n = 'html_url'; e = { "https://github.com/$($SplatParams.FullName)/tree/$($SplatParams.Branch)/$($_.path)" } } @@ -54,32 +61,42 @@ function Invoke-ExecGitHubAction { $Results = Import-CommunityTemplate @SplatParams } 'CreateRepo' { - $Repo = New-GitHubRepo @SplatParams - if ($Results.id) { - $Table = Get-CIPPTable -TableName CommunityRepos - $RepoEntity = @{ - PartitionKey = 'CommunityRepos' - RowKey = [string]$Repo.id - Name = [string]$Repo.name - Description = [string]$Repo.description - URL = [string]$Repo.html_url - FullName = [string]$Repo.full_name - Owner = [string]$Repo.owner.login - Visibility = [string]$Repo.visibility - WriteAccess = [bool]$Repo.permissions.push - DefaultBranch = [string]$Repo.default_branch - Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress) - } - Add-CIPPAzDataTableEntity @Table -Entity $RepoEntity -Force | Out-Null + try { + $Repo = New-GitHubRepo @SplatParams + if ($Results.id) { + $Table = Get-CIPPTable -TableName CommunityRepos + $RepoEntity = @{ + PartitionKey = 'CommunityRepos' + RowKey = [string]$Repo.id + Name = [string]$Repo.name + Description = [string]$Repo.description + URL = [string]$Repo.html_url + FullName = [string]$Repo.full_name + Owner = [string]$Repo.owner.login + Visibility = [string]$Repo.visibility + WriteAccess = [bool]$Repo.permissions.push + DefaultBranch = [string]$Repo.default_branch + Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress) + } + Add-CIPPAzDataTableEntity @Table -Entity $RepoEntity -Force | Out-Null + $Results = @{ + resultText = "Repository '$($Results.name)' created" + state = 'success' + } + } + } catch { $Results = @{ - resultText = "Repository '$($Results.name)' created" - state = 'success' + resultText = 'You may not have permission to create repositories, check your PAT scopes and try again - {0}' -f $_.Exception.Message + state = 'error' } } } default { - $Results = "Error: Unknown action '$Action'" + $Results = @{ + resultText = "Unknown action '$Action'" + state = 'error' + } } } } diff --git a/Modules/CippExtensions/Public/GitHub/Invoke-GitHubApiRequest.ps1 b/Modules/CippExtensions/Public/GitHub/Invoke-GitHubApiRequest.ps1 index 4cb649260d26..28c114d3d968 100644 --- a/Modules/CippExtensions/Public/GitHub/Invoke-GitHubApiRequest.ps1 +++ b/Modules/CippExtensions/Public/GitHub/Invoke-GitHubApiRequest.ps1 @@ -43,9 +43,10 @@ function Invoke-GitHubApiRequest { try { $Response = Invoke-RestMethod @RestMethod if ($ReturnHeaders.IsPresent) { - $ResponseHeaders + $Response | Add-Member -MemberType NoteProperty -Name Headers -Value $ResponseHeaders + return $Response } else { - $Response + return $Response } } catch { throw $_.Exception.Message