Private/Get-JenkinsUserInfo.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
function Get-JenkinsUserInfo {
    [CmdletBinding()]
    param (
        [ValidateNotNullOrEmpty()]
        [String] $UsernameToLookup = "me",
        [String] $Username = $script:apiUsername,
        [SecureString] $Password = $script:apiPassword
    )

    if (!$script:allUserInfo) {
        $script:allUserInfo = [hashtable]::new()
    }

    if ($UsernameToLookup -eq "me" ) {
        $UsernameToLookup = $Username
    }
    if ($null -ne $script:allUserInfo[$UsernameToLookup]) {
        return $script:allUserInfo[$UsernameToLookup]
    } else {
        $response = Invoke-JenkinsRequest -Resource "/user/$UsernameToLookup/api/json" -Username $Username -Password $Password
        $script:allUserInfo[$UsernameToLookup] = $response.Content | ConvertFrom-Json
        return $script:allUserInfo[$UsernameToLookup]
    }
}