Skip to content

Commit

Permalink
Refactor GitHub API functions to standardize path and method usage; a…
Browse files Browse the repository at this point in the history
…dd New-FunctionTemplate for function creation
  • Loading branch information
MariusStorhaug committed Dec 15, 2024
1 parent dc897a5 commit 0987040
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 118 deletions.
60 changes: 6 additions & 54 deletions tools/utilities/GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
115 changes: 51 additions & 64 deletions tools/utilities/New-Function.ps1
Original file line number Diff line number Diff line change
@@ -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'
}
}
85 changes: 85 additions & 0 deletions tools/utilities/New-FunctionTemplate.ps1
Original file line number Diff line number Diff line change
@@ -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'
}
}

0 comments on commit 0987040

Please sign in to comment.