Functions/Public/Get-GocdJobFeed.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function Get-GocdJobFeed {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [int[]]
        $Id
        ,
        [Parameter()]
        [switch]
        $Xml
        ,
        [Parameter()]
        [switch]
        $Raw
    )

    process {
        foreach ($JobId in $Id) {
            $Response = Invoke-GocdApi -Path "/api/jobs/$JobId.xml"

            if ($Xml) {
                $Response.Content
                return
            }

            [xml]$Content = $Response.Content

            if ($Raw) {
                $Content
                return
            }
            
            $Job = $Content.job
            $Pipeline = $Job.pipeline
            $Stage = $Job.stage
            $Properties = $Job.properties.property

            [pscustomobject]@{
                JobId           = $Properties | Where-Object { $_.name -ieq 'cruise_job_id' } | Select-Object -ExpandProperty '#cdata-section'
                PipelineName    = $Pipeline.name
                PipelineCounter = $Pipeline.counter
                StageName       = $Stage.name
                StageCounter    = $Stage.counter
                JobName         = $Job.name
                JobString       = "$($Pipeline.name)/$($Pipeline.counter)/$($Stage.name)/$($Stage.counter)/$($Job.name)"
                Scheduled       = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_01_scheduled'  } | Select-Object -ExpandProperty '#cdata-section'
                Assigned        = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_02_assigned'   } | Select-Object -ExpandProperty '#cdata-section'
                Preparing       = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_03_preparing'  } | Select-Object -ExpandProperty '#cdata-section'
                Building        = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_04_building'   } | Select-Object -ExpandProperty '#cdata-section'
                Completing      = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_05_completing' } | Select-Object -ExpandProperty '#cdata-section'
                Completed       = $Properties | Where-Object { $_.name -ieq 'cruise_timestamp_06_completed'  } | Select-Object -ExpandProperty '#cdata-section'
                Duration        = $Properties | Where-Object { $_.name -ieq 'cruise_job_duration'            } | Select-Object -ExpandProperty '#cdata-section'
                Result          = $Job.result
                AgentName       = $Properties | Where-Object { $_.name -ieq 'cruise_agent' } | Select-Object -ExpandProperty '#cdata-section'
                AgentUuid       = $Job.agent.uuid
                Resources       = $Job.resources.resource
                PSTypeName      = 'GocdJobFeed'
            }
        }
    }
}