diff --git a/src/classes/public/Webhooks/GitHubWebhookRedelivery.ps1 b/src/classes/public/Webhooks/GitHubWebhookRedelivery.ps1 new file mode 100644 index 000000000..aa797f93f --- /dev/null +++ b/src/classes/public/Webhooks/GitHubWebhookRedelivery.ps1 @@ -0,0 +1,21 @@ +class GitHubWebhookRedelivery : GitHubWebhook { + # Number of attempts to deliver the webhook. + [int] $Attempts + + # Simple parameterless constructor + GitHubWebhookRedelivery() {} + + # Creates a context object from a hashtable of key-vaule pairs. + GitHubWebhookRedelivery([hashtable]$Properties) { + foreach ($Property in $Properties.Keys) { + $this.$Property = $Properties.$Property + } + } + + # Creates a context object from a PSCustomObject. + GitHubWebhookRedelivery([PSCustomObject]$Object) { + $Object.PSObject.Properties | ForEach-Object { + $this.($_.Name) = $_.Value + } + } +} diff --git a/src/formats/GitHubWebhookRedelivery.format.ps1xml b/src/formats/GitHubWebhookRedelivery.format.ps1xml new file mode 100644 index 000000000..dab55ba4c --- /dev/null +++ b/src/formats/GitHubWebhookRedelivery.format.ps1xml @@ -0,0 +1,120 @@ + + + + + + GitHubWebhookRedeliveryTable + + GitHubWebhookRedelivery + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Attempts + + + ID + + + GUID + + + Event + + + StatusCode + + + DeliveredAt + + + Duration + + + + + + + + + + GitHubWebhookRedeliveryList + + GitHubWebhookRedelivery + + + + + + + + Attempts + + + + ID + + + + GUID + + + + DeliveredAt + + + + Redelivery + + + + Duration + + + + Status + + + + StatusCode + + + + Event + + + + Action + + + + + + + + diff --git a/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 b/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 new file mode 100644 index 000000000..9bb7ce43c --- /dev/null +++ b/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 @@ -0,0 +1,72 @@ +function Get-GitHubAppWebhookDeliveryToRedeliver { + <# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .EXAMPLE + An example + + .NOTES + [Ttle](link) + #> + [CmdletBinding()] + param( + # The timespan to check for redeliveries in hours. + [Parameter()] + [int] $TimeSpan = -2, + + # The number of results per page (max 100). + [Parameter()] + [ValidateRange(0, 100)] + [int] $PerPage, + + # 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 { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType APP + } + + process { + try { + $checkPoint = (Get-Date).AddHours($TimeSpan) + Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage | Where-Object { $_.DeliveredAt -gt $checkPoint } | + Group-Object -Property GUID | Where-Object { $_.Group.Status -notcontains 'OK' } | ForEach-Object { + $refObject = $_.Group | Sort-Object -Property DeliveredAt + [GitHubWebhookRedelivery]@{ + Attempts = $_.Count + GUID = $_.Name + Status = $refObject.Status + StatusCode = $refObject.StatusCode + Event = $refObject.Event + Action = $refObject.Action + Duration = $_.Group.Duration | Measure-Object -Average | Select-Object -ExpandProperty Average + ID = $refObject.ID + DeliveredAt = $refObject.DeliveredAt + Redelivery = $refObject.Redelivery + InstallationID = $refObject.InstallationID + RepositoryID = $refObject.RepositoryID + ThrottledAt = $refObject.ThrottledAt + URL = $refObject.URL + Request = $refObject.Request + Response = $refObject.Response + } + } + } catch { + throw $_ + } + } + + end { + Write-Debug "[$commandName] - End" + } +} diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index e4719a71b..0208f92c3 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -24,18 +24,32 @@ [Get a webhook configuration for an app](https://docs.github.com/rest/apps/webhooks#get-a-webhook-configuration-for-an-app) #> [OutputType([GitHubWebhook[]])] - [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] + [CmdletBinding(DefaultParameterSetName = 'ByList')] param( # The ID of the delivery. [Parameter( Mandatory, - ParameterSetName = 'ByID', - ValueFromPipeline, - ValueFromPipelineByPropertyName + ParameterSetName = 'ByID' )] [Alias('DeliveryID', 'delivery_id')] [string] $ID, + # Only the ones to redeliver. + [Parameter( + Mandatory, + ParameterSetName = 'Redelivery')] + [switch] $NeedingRedelivery, + + # The timespan to check for redeliveries in hours. + [Parameter(ParameterSetName = 'Redelivery')] + [int] $TimeSpan = -2, + + # The number of results per page (max 100). + [Parameter(ParameterSetName = 'ByList')] + [Parameter(ParameterSetName = 'Redelivery')] + [ValidateRange(0, 100)] + [int] $PerPage, + # 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()] @@ -53,10 +67,16 @@ try { switch ($PSCmdlet.ParameterSetName) { 'ByID' { + Write-Debug "ByID: [$ID]" Get-GitHubAppWebhookDeliveryByID -ID $ID -Context $Context } - '__AllParameterSets' { - Get-GitHubAppWebhookDeliveryByList -Context $Context + 'Redelivery' { + Write-Debug "Redelivery: [$NeedingRedelivery]" + Get-GitHubAppWebhookDeliveryToRedeliver -Context $Context -PerPage $PerPage -TimeSpan $TimeSpan + } + 'ByList' { + Write-Debug 'ByList' + Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage } } } catch { diff --git a/src/functions/public/Apps/Webhooks/Redeliver-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Redeliver-GitHubAppWebhookDelivery.ps1 index a837dbe87..7808e2af3 100644 --- a/src/functions/public/Apps/Webhooks/Redeliver-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Redeliver-GitHubAppWebhookDelivery.ps1 @@ -1,4 +1,4 @@ -function Redeliver-GitHubAppWebhookDelivery { +function Invoke-GitHubAppWebhookReDelivery { <# .SYNOPSIS Redeliver a delivery for an app webhook @@ -10,7 +10,7 @@ to access this endpoint. .EXAMPLE - Redeliver-GitHubAppWebhookDelivery -ID 12345 + Invoke-GitHubAppWebhookReDelivery -ID 12345 Redelivers the delivery with the ID `12345`. @@ -18,6 +18,7 @@ [Redeliver a delivery for an app webhook](https://docs.github.com/rest/apps/webhooks#redeliver-a-delivery-for-an-app-webhook) #> [OutputType([void])] + [Alias('Redeliver-GitHubAppWebhookDelivery')] [CmdletBinding(SupportsShouldProcess)] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseApprovedVerbs', '', Scope = 'Function', diff --git a/tools/utilities/New-FunctionTemplate.ps1 b/tools/utilities/New-FunctionTemplate.ps1 index 0c74e8ea3..5716bf6f0 100644 --- a/tools/utilities/New-FunctionTemplate.ps1 +++ b/tools/utilities/New-FunctionTemplate.ps1 @@ -29,8 +29,8 @@ ) begin { - $commandName = $MyInvocation.MyCommand.Name - Write-Debug "[$commandName] - Start" + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" $Context = Resolve-GitHubContext -Context $Context Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT @@ -76,7 +76,7 @@ } end { - Write-Debug "[$commandName] - End" + Write-Debug "[$stackPath] - End" } clean {