From f52da7f794d327b2a7fd9f9f1aee7f15f4caff51 Mon Sep 17 00:00:00 2001 From: John Duprey Date: Tue, 11 Feb 2025 09:05:47 -0500 Subject: [PATCH 1/2] fix search query Add default branch to repo list Adjust table object for community repo to include default branch --- CommunityRepos.json | 3 ++ .../Tools/GitHub/Invoke-ExecCommunityRepo.ps1 | 44 ++++++++++--------- .../Tools/GitHub/Invoke-ExecGitHubAction.ps1 | 14 +++++- .../GitHub/Invoke-ListCommunityRepos.ps1 | 22 +++++----- 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/CommunityRepos.json b/CommunityRepos.json index 0d76fe4a39f9..e34aafb8c821 100644 --- a/CommunityRepos.json +++ b/CommunityRepos.json @@ -8,6 +8,7 @@ "Owner": "CyberDrain", "Visibility": "public", "WriteAccess": false, + "DefaultBranch": "main", "RepoPermissions": { "admin": false, "maintain": false, @@ -25,6 +26,7 @@ "Owner": "j0eyv", "Visibility": "public", "WriteAccess": false, + "DefaultBranch": "main", "RepoPermissions": { "admin": false, "maintain": false, @@ -42,6 +44,7 @@ "Owner": "SkipToTheEndpoint", "Visibility": "public", "WriteAccess": false, + "DefaultBranch": "main", "RepoPermissions": { "admin": false, "maintain": false, diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecCommunityRepo.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecCommunityRepo.ps1 index 174254ddbb4b..ddd63ce66e32 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecCommunityRepo.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecCommunityRepo.ps1 @@ -23,16 +23,17 @@ function Invoke-ExecCommunityRepo { 'Add' { $Repo = Invoke-GitHubApiRequest -Path "repositories/$($Id)" $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 - Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress) + 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 @@ -46,17 +47,18 @@ function Invoke-ExecCommunityRepo { if ($RepoEntity) { $Repo = Invoke-GitHubApiRequest -Path "repositories/$($Id)" $Update = @{ - 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 - Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress) - ETag = $RepoEntity.ETag + 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) + ETag = $RepoEntity.ETag } Update-CIPPAzDataTableEntity @Table -Entity $Update 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 8b8aba3381f1..c84bd7d91ab7 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 @@ -13,11 +13,21 @@ function Invoke-ExecGitHubAction { param($Request, $TriggerMetadata) $Action = $Request.Query.Action ?? $Request.Body.Action - $SplatParams = ($Request.Query ?? $Request.Body) | Select-Object -ExcludeProperty Action, TenantFilter | ConvertTo-Json | ConvertFrom-Json -AsHashtable + + if ($Request.Query.Action) { + $Parameters = $Request.Query + } else { + $Parameters = $Request.Body + } + + $SplatParams = $Parameters | Select-Object -ExcludeProperty Action, TenantFilter | ConvertTo-Json | ConvertFrom-Json -AsHashtable + + Write-Information ($SplatParams | ConvertTo-Json) switch ($Action) { 'Search' { - $Results = (Search-GitHub @SplatParams).items + $SearchResults = Search-GitHub @SplatParams + $Results = @($SearchResults.items) $Metadata = $SearchResults | Select-Object -Property total_count, incomplete_results } 'GetFileContents' { diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ListCommunityRepos.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ListCommunityRepos.ps1 index 58e03e6b196a..4053ee48a676 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ListCommunityRepos.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ListCommunityRepos.ps1 @@ -23,16 +23,17 @@ function Invoke-ListCommunityRepos { foreach ($Repo in $DefaultCommunityRepos) { if ($Repos.Url -notcontains $Repo.Url) { $Entity = [PSCustomObject]@{ - PartitionKey = 'CommunityRepos' - RowKey = $Repo.Id - Name = $Repo.Name - Description = $Repo.Description - URL = $Repo.URL - FullName = $Repo.FullName - Owner = $Repo.Owner - Visibility = $Repo.Visibility - WriteAccess = $Repo.WriteAccess - Permissions = [string]($Repo.RepoPermissions | ConvertTo-Json) + PartitionKey = 'CommunityRepos' + RowKey = $Repo.Id + Name = $Repo.Name + Description = $Repo.Description + URL = $Repo.URL + FullName = $Repo.FullName + Owner = $Repo.Owner + Visibility = $Repo.Visibility + WriteAccess = $Repo.WriteAccess + DefaultBranch = $Repo.DefaultBranch + Permissions = [string]($Repo.RepoPermissions | ConvertTo-Json) } Add-CIPPAzDataTableEntity @Table -Entity $Entity $DefaultsMissing = $true @@ -52,6 +53,7 @@ function Invoke-ListCommunityRepos { Owner = $_.Owner Visibility = $_.Visibility WriteAccess = $_.WriteAccess + DefaultBranch = $_.DefaultBranch RepoPermissions = $_.Permissions | ConvertFrom-Json } } From 22bd3560dfc64be6e763142be839fce4e5064577 Mon Sep 17 00:00:00 2001 From: John Duprey Date: Tue, 11 Feb 2025 09:28:14 -0500 Subject: [PATCH 2/2] clean up comments --- .../Tools/GitHub/Invoke-ExecGitHubAction.ps1 | 2 +- .../Public/GitHub/Get-GitHubFileContents.ps1 | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) 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 c84bd7d91ab7..cf0d810d921a 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 @@ -22,7 +22,7 @@ function Invoke-ExecGitHubAction { $SplatParams = $Parameters | Select-Object -ExcludeProperty Action, TenantFilter | ConvertTo-Json | ConvertFrom-Json -AsHashtable - Write-Information ($SplatParams | ConvertTo-Json) + #Write-Information ($SplatParams | ConvertTo-Json) switch ($Action) { 'Search' { diff --git a/Modules/CippExtensions/Public/GitHub/Get-GitHubFileContents.ps1 b/Modules/CippExtensions/Public/GitHub/Get-GitHubFileContents.ps1 index aee61b0c69f0..684bf99927ba 100644 --- a/Modules/CippExtensions/Public/GitHub/Get-GitHubFileContents.ps1 +++ b/Modules/CippExtensions/Public/GitHub/Get-GitHubFileContents.ps1 @@ -1,14 +1,20 @@ function Get-GitHubFileContents { [CmdletBinding()] param ( - [Parameter(ValueFromPipelineByPropertyName = $true)] - $Url + [Parameter(ValueFromPipelineByPropertyName = $true, Mandatory = $true)] + $FullName, + + [Parameter(ValueFromPipelineByPropertyName = $true, Mandatory = $true)] + $Path, + + [Parameter(ValueFromPipelineByPropertyName = $true, Mandatory = $true)] + $Branch ) process { - [uri]$Uri = $Url - $Path = $Uri.PathAndQuery.TrimStart('/') - $File = Invoke-GitHubApiRequest -Path "$Path" -Method GET + $Path = "repos/$($FullName)/contents/$($Path)?ref=$($Branch)" + #Write-Information $Path + $File = Invoke-GitHubApiRequest -Path $Path -Method GET return [PSCustomObject]@{ name = $File.name