Functions/BuildResult/Get-BambooResult.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<#
.SYNOPSIS
    Gets build results for all or for a single Bamboo Plan.
.DESCRIPTION
    If -PlanKey is specified it filters for only those build results.
    By default it returns the latest build results.
.PARAMETER BuildKey
    Optional - Key for the exact Bamboo build to get the build results
.PARAMETER PlanKey
    Optional - Key for the Bamboo Plans to filter build results
.PARAMETER Build
    Optional - Parameter to specify the build in question, by default it
    always returns the latest build and the valid values are:
        LATEST - to return the latest build info
        ALL - to return all build results
        BUILD-Number - to return an exact build result by its id
.EXAMPLE
    Get-BambooResult

    key state lifeCycleState buildStartedTime buildCompletedDate
    --- ----- -------------- ---------------- ------------------
    PSB-BB-23 Successful Finished 2015-10-07T00:30:09.800+13:00 2015-10-07T00:30:13.344+13:00
    TPRJ-TPLN-11 Successful Finished 2015-10-07T00:30:13.598+13:00 2015-10-07T00:30:15.426+13:00

.EXAMPLE
    Get-BambooResult -BuildKey PSB-BB-22

    key state lifeCycleState buildStartedTime buildCompletedDate
    --- ----- -------------- ---------------- ------------------
    PSB-BB-22 Successful Finished 2015-10-07T00:26:17.977+13:00 2015-10-07T00:26:20.480+13:00

.EXAMPLE
    Get-BambooResult -PlanKey PSB-BB -All

    key state lifeCycleState buildStartedTime buildCompletedDate
    --- ----- -------------- ---------------- ------------------
    PSB-BB-23 Successful Finished 2015-10-07T00:30:09.800+13:00 2015-10-07T00:30:13.344+13:00
    PSB-BB-22 Successful Finished 2015-10-07T00:26:17.977+13:00 2015-10-07T00:26:20.480+13:00
    PSB-BB-21 Successful Finished 2015-10-07T00:24:39.928+13:00 2015-10-07T00:24:41.778+13:00
#>

function Get-BambooResult {
    [CmdletBinding(DefaultParameterSetName="ListAll")]
    param(
        [Parameter(ParameterSetName="ByBuildKey")]
        [ValidatePattern('\w+-\w+-\d+')]
        [string]$BuildKey,

        [Parameter(ParameterSetName="ByPlanKey")]
        [Parameter(ParameterSetName="ListExact")]
        [Parameter(ParameterSetName="ListLatest")]
        [Parameter(ParameterSetName="ListAll")]
        [ValidatePattern('\w+-\w+')]
        [string]$PlanKey,

        [Parameter(ParameterSetName="ListExact")]
        [ValidatePattern('\d+|latest|all')]
        [string]$Build='latest',

        [Parameter(ParameterSetName="ListLatest")]
        [switch]$Latest,

        [Parameter(ParameterSetName="ListAll")]
        [switch]$All
    )

    $resource = 'result'

    # List results by PlanKey
    if ($PlanKey) {
        if ($All) { $Build = 'all' }
        if ($Latest) { $Build = 'latest' }

        $resource = "result/$PlanKey"
        if ($Build -and $Build -ne 'all') {
            $resource="$($resource)-$Build"
        }
    }

    # Get exact BuildKey
    if ($BuildKey) {
        $resource = "result/$BuildKey"
    }

    Invoke-BambooRestMethod -Resource $resource  -Expand 'results.result' |
    Expand-BambooResource -ResourceName 'result' |
    Add_ObjectType -TypeName 'PsBamboo.Build'
}