Functions/Public/Find-GocdConfiguration.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
function Find-GocdConfiguration {
    [CmdletBinding()]
    param(
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [datetime]
        $Start
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [datetime]
        $End = (Get-Date)
    )

    if (-Not $Start) {
        $Start = $End.AddDays(-1)
    }

    $BasePath = '/api/config/revisions'

    $ContinueProcessing = $true
    $Offset = 0
    while ($ContinueProcessing) {
        $Response = Invoke-GocdApi -Path "$BasePath/$Offset"
        $Revisions = $Response | Select-Object -ExpandProperty Content | ConvertFrom-Json
        $Offset += $Revisions.Length

        foreach ($Revision in $Revisions) {
            $NewRevision = [pscustomobject]@{
                Time       = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($Revision.time / 1000))
                User       = $Revision.username
                MD5        = $Revision.MD5
                CommitHash = $Revision.commitSHA
            }

            if ($NewRevision.Time -ge $Start -and $NewRevision.Time -le $End) {
                $NewRevision
            }

            if ($NewRevision.Time -lt $Start) {
                $ContinueProcessing = $false
            }
        }
    }
}