Public/Remove-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
function Remove-JiraVersion {
    <#
    .Synopsis
       This function removes an existing version.
    .DESCRIPTION
       This function removes an existing version in JIRA.
    .EXAMPLE
       Get-JiraVersion -Name '1.0.0.0' -Project $Project | Remove-JiraVersion
       This example removes the Version given.
    .EXAMPLE
       Remove-JiraVersion -Version '66596'
       This example removes the Version given.
     .INPUTS
        [JiraPS.Version]
    .OUTPUTS
       This Function outputs no results
    .LINK
        New-JiraVersion
    .LINK
        Get-JiraVersion
    .LINK
        Set-JiraVersion
    .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(
        ConfirmImpact = 'High',
        SupportsShouldProcess = $true
    )]
    param(
        # Version Object or ID to delete.
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [Object[]] $Version,

        # Credentials to use to connect to Jira
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential] $Credential,

        # Suppress user confirmation.
        [Switch] $Force
    )

    begin {
        Write-Debug -Message '[Remove-JiraVersion] Reading information from config file'
        $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop

        if ($Force) {
            Write-Debug "[Remove-JiraVersion] -Force was passed. Backing up current ConfirmPreference [$ConfirmPreference] and setting to None"
            $oldConfirmPreference = $ConfirmPreference
            $ConfirmPreference = 'None'
        }
    }

    process {
        foreach ($_version in $Version) {
            Write-Debug "[Remove-JiraVersion] Obtaining reference to Version [$_version]"
            if ($_version.PSObject.TypeNames[0] -eq "JiraPS.Version") {
                $versionObject = Get-JiraVersion -Id $_version.Id -Credential $Credential
            }
            elseif ($_version -is [Int]) {
                $versionObject = Get-JiraVersion -Id $_version -Credential $Credential
            }
            else {
                $message = "Invalid Version provided."
                $exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
                Throw $exception
            }

            if ($versionObject) {
                $restUrl = "$server/rest/api/latest/version/$($versionObject.Id)"

                if ($PSCmdlet.ShouldProcess($versionObject.Name, "Removing Version on JIRA")) {
                    Write-Debug -Message '[Remove-JiraVersion] Preparing for blastoff!'
                    Invoke-JiraMethod -Method Delete -URI $restUrl -Credential $Credential
                }
            }
            else {
                throw "no versionoBjects"
            }
        }
    }

    end {
        if ($Force) {
            Write-Debug "[Remove-JiraVersion] Restoring ConfirmPreference to [$oldConfirmPreference]"
            $ConfirmPreference = $oldConfirmPreference
        }

        Write-Debug "[Remove-JiraVersion] Complete"
    }
}