Modules/Get-VstsRecentBuilds.psm1

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
. "$PSScriptRoot\Test-Environment.ps1"
<#
.SYNOPSIS
Gets the most recent builds for a build definition
 
.DESCRIPTION
Long description
 
.PARAMETER recent
How many builds to return at most. Defaults to 4
 
.PARAMETER sourceBranch
The branch that the build was invoked against. Defaults to the branch the call is made from, assuming this is invoked from a git repository.
This is mandatory if not in a git repository
 
.EXAMPLE
Get-VstsRecentBuilds -recent 10 -sourceBranch develop -build VstsBuildDefinitionName
 
.NOTES
Outputs an object with properties:
 * Project - the project name
 * By - the display name of who invoked the build
 * Definition - the build definition name
 * BuildNumber - the build number property of the build
 * SourceBranch - the branch the build was invoked against
 * StartTime
 * FinishTime
 * Result - indicates if the build succeeded or not
#>

function Get-VstsRecentBuilds () { 
    [CmdletBinding(PositionalBinding = $false)]
    param(
        [Parameter(Mandatory = $false)]
        [int]$recent = 4,
        [Parameter(Mandatory = $false)]
        [string]$sourceBranch = $null
    )
    DynamicParam {
        Test-Environment
        # Set the dynamic parameters' name
        $ParameterName = 'build'
        
        # Create the dictionary
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        
        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $false
        $ParameterAttribute.Position = 0
        $AttributeCollection.Add($ParameterAttribute)

        # Add the attributes to the attributes collection

        # Generate and set the ValidateSet
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($MyInvocation.MyCommand.Module.PrivateData.buildDefinitions.Keys)

        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute)

        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
        
    }
    begin {
        $build = $PSBoundParameters[$ParameterName]
        if ($build -ne $null -and $MyInvocation.MyCommand.Module.PrivateData.buildDefinitions.ContainsKey($build)) {
            $build = $MyInvocation.MyCommand.Module.PrivateData.buildDefinitions[$build]
        }
    }
    process {
        $response = Get-RestVsts -U "_apis/build/builds?definitions=$($build)&`$top=$($recent)" -V 2.0 -Verbose:$VerbosePreference
        foreach ($v in $response.value) {
            [pscustomobject] @{"Project" = $v.project.name; "By" = $v.requestedBy.displayName; Definition = $v.definition.name; BuildNumber = $v.buildNumber; SourceBranch = $v.sourceBranch; StartTime = $v.startTime; FinishTime = $v.finishTime; Result = $v.result} 
        }
    }
}