Public/Get-JiraComponent.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Get-JiraComponent {
    <#
    .Synopsis
       Returns a Component from Jira
    .DESCRIPTION
       This function returns information regarding a specified component from Jira.
       If -InputObject is given via parameter or pipe all components for
       the given project are returned.
       It is not possible to get all components with this function.
    .EXAMPLE
       Get-JiraComponent -Id 10000 -Credential $cred
       Returns information about the component with ID 10000
    .EXAMPLE
       Get-JiraComponent 20000 -Credential $cred
       Returns information about the component with ID 20000
    .EXAMPLE
       Get-JiraProject Project1 | Get-JiraComponent -Credential $cred
       Returns information about all components within project 'Project1'
    .EXAMPLE
        Get-JiraComponent ABC,DEF
        Return information about all components within projects 'ABC' and 'DEF'
    .INPUTS
       [String[]] Component ID
       [PSCredential] Credentials to use to connect to Jira
    .OUTPUTS
       [JiraPS.Component]
    #>

    [CmdletBinding(DefaultParameterSetName = 'ByID')]
    param(
        # The Project ID or project key of a project to search.
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ParameterSetName = 'ByProject'
        )]
        $Project,

        # The Component ID.
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'ByID'
        )]
        [Alias("Id")]
        [int[]] $ComponentId,

        # 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-JiraComponent] Reading server from config file"
        try {
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[Get-JiraComponent] Encountered an error reading the Jira server."
            throw $err
        }

        $uri = "$server/rest/api/latest"
    }

    process {
        if ($Project) {
            if ($Project.PSObject.TypeNames[0] -eq 'JiraPS.Project') {
                $ComponentId = @($Project.Components | Select-Object -ExpandProperty id)
            }
            else {
                foreach ($p in $Project) {
                    if ($p -is [string]) {
                        Write-Debug "[Get-JiraComponent] Processing project [$p]"
                        $thisUri = "$uri/project/${p}/components"

                        Write-Debug "[Get-JiraComponent] Preparing for blastoff!"

                        $result = Invoke-JiraMethod -Method Get -URI $thisUri -Credential $Credential
                        if ($result) {
                            Write-Debug "[Get-JiraComponent] Converting to object"
                            $obj = ConvertTo-JiraComponent -InputObject $result

                            Write-Debug "[Get-JiraComponent] Outputting result"
                            Write-Output $obj
                        }
                        else {
                            Write-Debug "[Get-JiraComponent] No results were returned from Jira"
                            Write-Debug "[Get-JiraComponent] No results were returned from Jira for component [$i]"
                        }
                    }
                }
            }
        }
        if ($ComponentId) {
            foreach ($i in $ComponentId) {
                Write-Debug "[Get-JiraComponent] Processing component [$i]"
                $thisUri = "$uri/component/${i}"

                Write-Debug "[Get-JiraComponent] Preparing for blastoff!"

                $result = Invoke-JiraMethod -Method Get -URI $thisUri -Credential $Credential
                if ($result) {
                    Write-Debug "[Get-JiraComponent] Converting to object"
                    $obj = ConvertTo-JiraComponent -InputObject $result

                    Write-Debug "[Get-JiraComponent] Outputting result"
                    Write-Output $obj
                }
                else {
                    Write-Debug "[Get-JiraComponent] No results were returned from Jira"
                    Write-Debug "[Get-JiraComponent] No results were returned from Jira for component [$i]"
                }
            }
        }
    }

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