Public/Get-JiraVersion.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
function Get-JiraVersion {
    <#
    .Synopsis
       This function returns information about a JIRA Project's Version
    .DESCRIPTION
       This function provides information about JIRA Version
    .EXAMPLE
       Get-JiraVersion -Project $ProjectKey
       This example returns information about all JIRA Version visible to the current user for the project.
    .EXAMPLE
       Get-JiraVersion -Project $ProjectKey -Name '1.0.0.0'
       This example returns the information of a specific Version.
    .EXAMPLE
       Get-JiraVersion -ID '66596'
       This example returns information about all JIRA Version visible to the current user (or using anonymous access if a JiraPS session has not been defined) for the project.
    .INPUTS
        [JiraPS.Project]
    .OUTPUTS
       [JiraPS.Version]
    .LINK
        New-JiraVersion
    .LINK
        Remove-JiraVersion
    .LINK
        Set-JiraVersion
    .LINK
        Get-JiraProject
    .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(DefaultParameterSetName = 'byId')]
    param(
        # Project key of a project to search
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'byProject',
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('Key')]
        [String[]] $Project,

        # Jira Version Name
        [Parameter(
            Mandatory = $false,
            ParameterSetName = 'byProject'
        )]
        [Alias('Versions')]
        [String[]] $Name,

        # The Version ID
        [Parameter(
            Mandatory = $true,
            ParameterSetName = 'byId',
            ValueFromPipelineByPropertyName = $true
        )]
        [Int[]] $Id,

        # Credentials to use to connect to Jira
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential] $Credential
    )
    begin {
        Write-Debug "[Get-JiraVersion] Reading server from config file"
        $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop

        Write-Debug "[Get-JiraVersion] Completed Begin block."
    }
    process {
        Switch ($PSCmdlet.ParameterSetName) {
            'byProject' {
                foreach ($_project in $Project) {
                    Write-Debug "[Get-JiraVersion] Gathering project data for [$_project]."
                    $projectData = Get-JiraProject -Project $_project -Credential $Credential
                    $restUrl = "$server/rest/api/latest/project/$($projectData.key)/versions"

                    Write-Debug -Message '[Get-JiraVersion] Preparing for blastoff!'
                    $result = Invoke-JiraMethod -Method Get -URI $restUrl -Credential $Credential

                    If ($Name) {
                        $result = $result | Where-Object {$_.Name -in $Name}
                    }
                    $result | ConvertTo-JiraVersion -Credential $Credential
                }
            }
            'byId' {
                foreach ($_id in $ID) {
                    $restUrl = "$server/rest/api/latest/version/$_id"

                    Write-Debug -Message '[Get-JiraVersion] Preparing for blastoff!'
                    Invoke-JiraMethod -Method Get -URI $restUrl -Credential $Credential | ConvertTo-JiraVersion -Credential $Credential
                }
            }
        }
    }
    end {
        Write-Debug "[Get-JiraVersion] Complete"
    }
}