Functions/Public/Get-GocdJobHistory.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Get-GocdJobHistory {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Pipeline
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Stage
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Job
        ,
        [Parameter()]
        [switch]
        $Raw
    )

    $Response = Invoke-GocdApi -Path "/api/jobs/$Pipeline/$Stage/$Job/history"
    $Jobs = $Response | Select-Object -ExpandProperty Content | ConvertFrom-Json
    $Jobs = $Jobs | Select-Object -ExpandProperty jobs

    if ($Raw) {
        $Jobs
        return
    }

    foreach ($Item in $Jobs) {
        [pscustomobject]@{
            JobId           = $Item.id
            Pipeline        = $Item.pipeline_name
            PipelineCounter = $Item.pipeline_counter
            Stage           = $Item.stage_name
            StageCounter    = $Item.stage_counter
            Job             = $Item.name
            Scheduled       = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($Item.scheduled_date / 1000))
            State           = $Item.state
            Result          = $Item.result
        }
    }
}