Public/Get-DuneJobEvent.ps1

<#
.SYNOPSIS
Retrieve job events.

.DESCRIPTION
Gets job event objects. Supports filtering by `Name`, `Id`, `Job` (pipeline input), event `State`, and an option to include deleted events. Returns `DuneJobEvent` objects by default; use `-Raw` for raw API output.

.PARAMETER Name
Filter job events by name (supports wildcards). Position 0 in the default parameter set.

.PARAMETER Id
The GUID of a job event. Use the `Id` parameter set to retrieve a specific event.

.PARAMETER Job
A `DuneJob` object; returns events for the supplied job (pipeline input supported).

.PARAMETER State
Filter job events by `JobEventStates`.

.PARAMETER Raw
If set, returns raw API objects instead of `DuneJobEvent` objects.

.PARAMETER IncludeDeleted
Include deleted job events in results.

.EXAMPLE
PS> Get-DuneJobEvent -Name "provision"
Returns job events matching `provision`.

.EXAMPLE
PS> Get-DuneJob -Name "deploy-app" | Get-DuneJobEvent
Pipeline example using the `Job` parameter set.
#>

function Get-DuneJobEvent {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(Position = 0)]
        [string]$Name,

        [Parameter(ParameterSetName = "Id")]
        [guid]$Id,

        [Parameter(ParameterSetName = "Job", ValueFromPipeline)]
        [DuneJob]$Job,

        [Parameter()]
        [JobEventStates]$State,

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeleted
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $BaseUri = 'jobevents'
        $Method = 'GET'
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { '{0}/{1}' -f $BaseUri, $Id }
            'Job' { '{0}?JobId={1}' -f $BaseUri, $Job.Id }
            Default { $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($PSBoundParameters.ContainsKey('State')) {
            $Uri = $Uri | Add-UriQueryParam "State=$State"
        }
        if ($IncludeDeleted) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1"
        }

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                # ApiCall and Object conversion
                $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
                $ProcessedUrls += $Uri
                $ReturnObjects += $ResultItems | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneJobEvent -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object StartTime
    }
}