Public/Get-JiraField.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
function Get-JiraField {
    <#
    .Synopsis
       This function returns information about JIRA fields
    .DESCRIPTION
       This function provides information about JIRA fields, or about one field in particular. This is a good way to identify a field's ID by its name, or vice versa.
 
       Typically, this information is only needed when identifying what fields are necessary to create or edit issues. See Get-JiraIssueCreateMetadata for more details.
    .EXAMPLE
       Get-JiraField
       This example returns information about all JIRA fields visible to the current user (or using anonymous access if a JiraPS session has not been defined).
    .EXAMPLE
       Get-JiraField IssueKey
       This example returns information about the IssueKey field.
    .INPUTS
       This function does not accept pipeline input.
    .OUTPUTS
       This function outputs the JiraPS.Field object(s) that represent the JIRA field(s).
    .NOTES
       This function requires either the -Credential parameter to be passed or a persistent JIRA session. See New-JiraSession for more details. If neither are supplied, this function will run with anonymous access to JIRA.
    #>

    [CmdletBinding()]
    param(
        # The Field name or ID to search.
        [Parameter(
            Position = 0,
            Mandatory = $false,
            ValueFromRemainingArguments = $true
        )]
        [String[]] $Field,

        # Credentials to use to connect to JIRA.
        # If not specified, this function will use anonymous access.
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential] $Credential
    )

    begin {
        Write-Debug "[Get-JiraField] Reading server from config file"
        try {
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[Get-JiraField] Encountered an error reading the Jira server."
            throw $err
        }

        $uri = "$server/rest/api/latest/field"
        Write-Debug "[Get-JiraField] Obtaining all fields from Jira"
        $allFields = ConvertTo-JiraField -InputObject (Invoke-JiraMethod -Method Get -URI $uri -Credential $Credential)
    }

    process {
        if ($Field) {
            foreach ($f in $Field) {
                Write-Debug "[Get-JiraField] Processing field [$f]"
                Write-Debug "[Get-JiraField] Searching for field (name=[$f])"
                $thisField = $allFields | Where-Object -FilterScript {$_.Name -eq $f}
                if ($thisField) {
                    Write-Debug "[Get-JiraField] Found results; outputting"
                    Write-Output $thisField
                }
                else {
                    Write-Debug "[Get-JiraField] No results were found for issue type by name. Searching for issue type (id=[$i])"
                    $thisField = $allFields | Where-Object -FilterScript {$_.Id -eq $f}
                    if ($thisField) {
                        Write-Debug "[Get-JiraField] Found results; outputting"
                        Write-Output $thisField
                    }
                    else {
                        Write-Debug "[Get-JiraField] No results were found for issue type by ID. This issue type appears to be unknown."
                        Write-Verbose "Unable to identify Jira field [$f]"
                    }
                }
            }
        }
        else {
            Write-Debug "[Get-JiraField] No Field was supplied. Outputting all fields."
            Write-Output $allFields
        }
    }

    end {
        Write-Debug "[Get-JiraField] Complete"
    }
}