Functions/DeployEnvironmentResult/Get-BambooDeployEnvironmentResult.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
63
64
65
66
67
<#
.SYNOPSIS
    Gets all results for the deploy environment
.DESCRIPTION
    Gets all results for the deploy environment
.PARAMETER DeployEnvironmentId
    Mandatory - Key for the Bamboo Deploy Project to be described
.EXAMPLE
    Get-BambooDeployEnvironmentResult -DeployEnvironmentId '65732621'

deployProjectId environmentId buildPlanResultKey deploymentState lifeCycleState startedDate (UTC) finishedDate (UTC)
--------------- ------------- ------------------ --------------- -------------- ----------------- ------------------
65601539 65732621 ABAI-BBAI-9 SUCCESS FINISHED 20171128T0253396970000Z 20171128T0300117170000Z
65601539 65732621 ABAI-BBAI-9 SUCCESS FINISHED 20171128T0201389630000Z 20171128T0236497530000Z
65601539 65732621 ABAI-BBAI-8 SUCCESS FINISHED 20171128T0112255570000Z 20171128T0112575570000Z
65601539 65732621 ABAI-BBAI-8 FAILED FINISHED 20171128T0109390130000Z 20171128T0109478430000Z
65601539 65732621 ABAI-BBAI-8 FAILED FINISHED 20171128T0100426800000Z 20171128T0102268330000Z

#>

function Get-BambooDeployEnvironmentResult {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [ValidateNotNullOrEmpty()]
        [string]$DeployEnvironmentId
    )

    $ContentType = 'application/json'
    Write-Warning "Get-BambooDeployEnvironmentResult: The Bamboo API for 'Deploy Environments' only supports JSON response format. Using content-type: $ContentType"

    $resource = "deploy/environment/$DeployEnvironmentId/results"

    #Pagination loop variables
    $moreItem = $null
    $lastIndex = $null
    $isSameLoop = $null
    $uriParams=@{
        'start-index' = 0
    }

    Do {
        $result = $null
        $result = Invoke-BambooRestMethod -Resource $resource -ContentType $ContentType -UriParams $uriParams 
        
        $result | Expand-BambooResource -ResourceName 'results' -ContentType $ContentType |
        Add_ObjectType -TypeName 'PsBamboo.DeployEnvironmentResult'   
        
        #Adjust Pagination
        $page = $null
        $isSameLoop = $True
        
        if ($result -and ($result | Get-Member 'results')) {
            $page = $result | Select-Object size, 'start-index', 'max-result'

            $uriParams.'start-index' = ([int]$page.'start-index') + ([int]$page.'max-result')
            $moreItem = ([int]$page.size) -gt ([int]$uriParams.'start-index')
            Write-Verbose "More pages: $moreItem for $page"

            $isSameLoop = $lastIndex -eq $page.'start-index'
            Write-Verbose "Start-index loop check: '$lastIndex == $($page.'start-index')' => $isSameLoop"

            $lastIndex = $page.'start-index'
        }

    } while ($moreItem -and !$isSameLoop)
}