From e2e322660154b5140f108f65e7bff11ee9e96d6d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 20 Dec 2024 16:14:23 +0100 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=9A=80=20[Feature]:=20Add=20Get-GitHu?= =?UTF-8?q?bAppWebhookDeliveryToRedeliver=20function=20and=20update=20deli?= =?UTF-8?q?very=20retrieval=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...et-GitHubAppWebhookDeliveryToRedeliver.ps1 | 59 +++++++++++++++++++ .../Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 9 +++ tools/utilities/New-FunctionTemplate.ps1 | 6 +- 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 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..de704d578 --- /dev/null +++ b/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 @@ -0,0 +1,59 @@ +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 { + $allDeliveries = Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage + $checkPoint = (Get-Date).AddHours($TimeSpan) + $allDeliveries | Where-Object { $_.DeliveredAt -gt $checkPoint } | Group-Object -Property guid | + Where-Object { $_.Group.status -notcontains 'OK' } | ForEach-Object { + [pscustomobject]@{ + guid = $_.name + redeliver = $_.Group.status -notcontains 'OK' + id = $_.Group[0].id + } + } | Where-Object { $_.redeliver } + } 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..9fbb10582 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -36,6 +36,12 @@ [Alias('DeliveryID', 'delivery_id')] [string] $ID, + # Only the ones to redeliver. + [Parameter( + Mandatory, + ParameterSetName = 'ToRedeliver')] + [switch] $ToRedeliver, + # 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()] @@ -55,6 +61,9 @@ 'ByID' { Get-GitHubAppWebhookDeliveryByID -ID $ID -Context $Context } + 'ToRedeliver' { + Get-GitHubAppWebhookDeliveryToRedeliver -Context $Context + } '__AllParameterSets' { Get-GitHubAppWebhookDeliveryByList -Context $Context } 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 { From 25d445bd34a3f4c5cb49d7a1244f69e52b3d7b29 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 20 Dec 2024 16:17:07 +0100 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20paramete?= =?UTF-8?q?r=20sets=20in=20Get-GitHubAppWebhookDelivery=20function=20for?= =?UTF-8?q?=20improved=20delivery=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index 9fbb10582..ff8f29740 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -24,7 +24,7 @@ [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( @@ -42,6 +42,15 @@ ParameterSetName = 'ToRedeliver')] [switch] $ToRedeliver, + # The timespan to check for redeliveries in hours. + [Parameter(ParameterSetName = 'ToRedeliver')] + [int] $TimeSpan = -2, + + # The number of results per page (max 100). + [Parameter(ParameterSetName = 'ByList')] + [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()] @@ -59,13 +68,16 @@ try { switch ($PSCmdlet.ParameterSetName) { 'ByID' { + Write-Debug "ByID: [$ID]" Get-GitHubAppWebhookDeliveryByID -ID $ID -Context $Context } 'ToRedeliver' { - Get-GitHubAppWebhookDeliveryToRedeliver -Context $Context + Write-Debug "ToRedeliver: [$ToRedeliver]" + Get-GitHubAppWebhookDeliveryToRedeliver -Context $Context -PerPage $PerPage -TimeSpan $TimeSpan } - '__AllParameterSets' { - Get-GitHubAppWebhookDeliveryByList -Context $Context + 'ByList' { + Write-Debug 'ByList' + Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage } } } catch { From 9804cd8bb5dcdb526336b38daad76f6ce0c42b07 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 20 Dec 2024 17:05:35 +0100 Subject: [PATCH 3/5] Add GitHubWebhookRedelivery class and format for table and list views --- .../Webhooks/GitHubWebhookRedelivery.ps1 | 21 +++ .../GitHubWebhookRedelivery.format.ps1xml | 120 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 src/classes/public/Webhooks/GitHubWebhookRedelivery.ps1 create mode 100644 src/formats/GitHubWebhookRedelivery.format.ps1xml 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 + + + + + + + + From cd91adacacffa3de636e1762c2ab25be7d5b8fae Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 20 Dec 2024 17:05:50 +0100 Subject: [PATCH 4/5] Refactor Get-GitHubAppWebhookDeliveryToRedeliver function to enhance delivery status retrieval and structure output --- ...et-GitHubAppWebhookDeliveryToRedeliver.ps1 | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 b/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 index de704d578..9bb7ce43c 100644 --- a/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 +++ b/src/functions/private/Apps/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1 @@ -38,16 +38,29 @@ process { try { - $allDeliveries = Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage $checkPoint = (Get-Date).AddHours($TimeSpan) - $allDeliveries | Where-Object { $_.DeliveredAt -gt $checkPoint } | Group-Object -Property guid | - Where-Object { $_.Group.status -notcontains 'OK' } | ForEach-Object { - [pscustomobject]@{ - guid = $_.name - redeliver = $_.Group.status -notcontains 'OK' - id = $_.Group[0].id + 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 } - } | Where-Object { $_.redeliver } + } } catch { throw $_ } From 31bb354f830e0eb8977f78da915ba667cfe68867 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 20 Dec 2024 17:21:40 +0100 Subject: [PATCH 5/5] Refactor parameter sets in Get-GitHubAppWebhookDelivery function for clarity and rename Redeliver-GitHubAppWebhookDelivery to Invoke-GitHubAppWebhookReDelivery --- .../Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 15 +++++++-------- .../Redeliver-GitHubAppWebhookDelivery.ps1 | 5 +++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index ff8f29740..0208f92c3 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -29,9 +29,7 @@ # The ID of the delivery. [Parameter( Mandatory, - ParameterSetName = 'ByID', - ValueFromPipeline, - ValueFromPipelineByPropertyName + ParameterSetName = 'ByID' )] [Alias('DeliveryID', 'delivery_id')] [string] $ID, @@ -39,15 +37,16 @@ # Only the ones to redeliver. [Parameter( Mandatory, - ParameterSetName = 'ToRedeliver')] - [switch] $ToRedeliver, + ParameterSetName = 'Redelivery')] + [switch] $NeedingRedelivery, # The timespan to check for redeliveries in hours. - [Parameter(ParameterSetName = 'ToRedeliver')] + [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, @@ -71,8 +70,8 @@ Write-Debug "ByID: [$ID]" Get-GitHubAppWebhookDeliveryByID -ID $ID -Context $Context } - 'ToRedeliver' { - Write-Debug "ToRedeliver: [$ToRedeliver]" + 'Redelivery' { + Write-Debug "Redelivery: [$NeedingRedelivery]" Get-GitHubAppWebhookDeliveryToRedeliver -Context $Context -PerPage $PerPage -TimeSpan $TimeSpan } 'ByList' { 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',