From 098704079e7481bdf79a71681faad1a257d97f25 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 15 Dec 2024 10:57:42 +0100 Subject: [PATCH] Refactor GitHub API functions to standardize path and method usage; add New-FunctionTemplate for function creation --- tools/utilities/GitHubAPI.ps1 | 60 ++---------- tools/utilities/New-Function.ps1 | 115 ++++++++++------------- tools/utilities/New-FunctionTemplate.ps1 | 85 +++++++++++++++++ 3 files changed, 142 insertions(+), 118 deletions(-) create mode 100644 tools/utilities/New-FunctionTemplate.ps1 diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index e7a06f98..05baf5dd 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/apps/{app_slug}' -$method = 'get' +$path = '/repos/{owner}/{repo}/actions/runs' +$method = 'GET' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName @@ -41,57 +41,9 @@ $response.paths.$path.$method.responses.'200'.content.'application/json'.schema $response.paths.$path.$method.responses.'200'.content.'application/json'.schema.items # -> OutputType $response.components.schemas.'issue-comment' | ConvertTo-Json +$response.components.responses # HTTP status descriptions -function New-Function { - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory)] - [string] $Path, - - [Parameter(Mandatory)] - [string] $Method - ) - - $APIDocURI = 'https://raw.githubusercontent.com/github/rest-api-description/main' - $Bundled = '/descriptions/api.github.com/api.github.com.json' - # $Dereferenced = 'descriptions/api.github.com/dereferenced/api.github.com.deref.json' - $APIDocURI = $APIDocURI + $Bundled - $response = Invoke-RestMethod -Uri $APIDocURI -Method Get - - $response.paths.$Path.$Method - - $FunctionName = "$Method-GitHub" + (($response.paths.$path.$method.operationId) -Replace '/', '-') - - $folderName = $response.paths.$path.$method.'x-github'.category - $subFolderName = $response.paths.$path.$method.'x-github'.subcategory - - $template = @" - function $FunctionName { - <# - .SYNOPSIS - $($response.paths.$path.$method.summary) - - .DESCRIPTION - $($response.paths.$path.$method.description) - - .EXAMPLE - An example - - .NOTES - [$($response.paths.$path.$method.summary)]($($response.paths.$path.$method.externalDocs.url)) - #> - [OutputType([pscustomobject])] - [CmdletBinding()] - param( - # The context to run the command in. - [Parameter()] - [string] `$Context = (Get-GitHubConfig -Name 'DefaultContext') - ) - } -"@ - if ($PSCmdlet.ShouldProcess('Function', 'Create')) { - New-Item -Path "src/functions/$folderName/$subFolderName" -Name "$FunctionName.ps1" -ItemType File -Value $template - } - -} +$path = '/repos/{owner}/{repo}/actions/runs' +$method = 'get' +$response.components.schemas.'issue-comment' diff --git a/tools/utilities/New-Function.ps1 b/tools/utilities/New-Function.ps1 index 062e987e..e7a79fde 100644 --- a/tools/utilities/New-Function.ps1 +++ b/tools/utilities/New-Function.ps1 @@ -1,85 +1,72 @@ -function New-Function { + +function New-Function { <# - .SYNOPSIS - Short description + .SYNOPSIS + Short description - .DESCRIPTION - Long description + .DESCRIPTION + Long description - .EXAMPLE - An example + .PARAMETER Path + Parameter description - .NOTES - General notes + .PARAMETER Method + Parameter description + + .EXAMPLE + An example + + .NOTES + General notes #> [CmdletBinding(SupportsShouldProcess)] param( - # The name of the organization. - [Parameter()] - [string]$Owner, - - # The name of the organization. - [Parameter()] - [string]$Repo, + [Parameter(Mandatory)] + [string] $Path, - # 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. - [Parameter()] - [object] $Context = (Get-GitHubContext) + [Parameter(Mandatory)] + [string] $Method ) - begin { - $commandName = $MyInvocation.MyCommand.Name - Write-Debug "[$commandName] - Start" + $APIDocURI = 'https://raw.githubusercontent.com/github/rest-api-description/main' + $Bundled = '/descriptions/api.github.com/api.github.com.json' + # $Dereferenced = 'descriptions/api.github.com/dereferenced/api.github.com.deref.json' + $APIDocURI = $APIDocURI + $Bundled + $response = Invoke-RestMethod -Uri $APIDocURI -Method Get - $Context = Resolve-GitHubContext -Context $Context + $response.paths.$Path.$Method - if ([string]::IsNullOrEmpty($Enterprise)) { - $Enterprise = $Context.Enterprise - } - Write-Debug "Enterprise : [$($Context.Enterprise)]" + $FunctionName = "$Method-GitHub" + (($response.paths.$path.$method.operationId) -Replace '/', '-') - if ([string]::IsNullOrEmpty($Owner)) { - $Owner = $Context.Owner - } - Write-Debug "Owner : [$($Context.Owner)]" + $folderName = $response.paths.$path.$method.'x-github'.category + $subFolderName = $response.paths.$path.$method.'x-github'.subcategory - if ([string]::IsNullOrEmpty($Repo)) { - $Repo = $Context.Repo - } - Write-Debug "Repo : [$($Context.Repo)]" - } + $template = @" + function $FunctionName { + <# + .SYNOPSIS + $($response.paths.$path.$method.summary) - process { - try { - $body = @{ - per_page = $PerPage - } + .DESCRIPTION + $($response.paths.$path.$method.description) - $inputObject = @{ - Context = $Context - APIEndpoint = "/orgs/$OrganizationName/blocks" - Method = 'GET' - Body = $body - } + .EXAMPLE + An example - if ($PSCmdlet.ShouldProcess('Target', 'Operation')) { - Invoke-GitHubAPI @inputObject | ForEach-Object { - Write-Output $_.Response - } - } - } catch { - Write-Debug "Error: $_" - } finally { - Write-Debug 'Finally' - } + .NOTES + [$($response.paths.$path.$method.summary)]($($response.paths.$path.$method.externalDocs.url)) + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param( + # The context to run the command in. + [Parameter()] + [string] `$Context = (Get-GitHubConfig -Name 'DefaultContext') + ) } - - end { - Write-Debug "[$commandName] - End" +"@ + if ($PSCmdlet.ShouldProcess('Function', 'Create')) { + New-Item -Path "src/functions/$folderName/$subFolderName" -Name "$FunctionName.ps1" -ItemType File -Value $template } - clean { - Write-Debug 'Clean' - } } diff --git a/tools/utilities/New-FunctionTemplate.ps1 b/tools/utilities/New-FunctionTemplate.ps1 new file mode 100644 index 00000000..4d025022 --- /dev/null +++ b/tools/utilities/New-FunctionTemplate.ps1 @@ -0,0 +1,85 @@ +function New-FunctionTemplate { + <# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .EXAMPLE + An example + + .NOTES + General notes + #> + [CmdletBinding(SupportsShouldProcess)] + param( + # The name of the organization. + [Parameter()] + [string]$Owner, + + # The name of the organization. + [Parameter()] + [string]$Repo, + + # 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. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Debug "[$commandName] - Start" + + $Context = Resolve-GitHubContext -Context $Context + + if ([string]::IsNullOrEmpty($Enterprise)) { + $Enterprise = $Context.Enterprise + } + Write-Debug "Enterprise : [$($Context.Enterprise)]" + + if ([string]::IsNullOrEmpty($Owner)) { + $Owner = $Context.Owner + } + Write-Debug "Owner : [$($Context.Owner)]" + + if ([string]::IsNullOrEmpty($Repo)) { + $Repo = $Context.Repo + } + Write-Debug "Repo : [$($Context.Repo)]" + } + + process { + try { + $body = @{ + per_page = $PerPage + } + + $inputObject = @{ + Context = $Context + APIEndpoint = "/orgs/$OrganizationName/blocks" + Method = 'GET' + Body = $body + } + + if ($PSCmdlet.ShouldProcess('Target', 'Operation')) { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } + } catch { + Write-Debug "Error: $_" + } finally { + Write-Debug 'Finally' + } + } + + end { + Write-Debug "[$commandName] - End" + } + + clean { + Write-Debug 'Clean' + } +}