Public/Set-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function Set-JiraVersion {
    <#
    .Synopsis
        Modifies an existing Version in JIRA
    .DESCRIPTION
        This function modifies the Version for an existing Project in JIRA.
    .EXAMPLE
        Get-JiraVersion -Project $Project -Name "Old-Name" | Set-JiraVersion -Name 'New-Name'
        This example assigns the modifies the existing version with a new name 'New-Name'.
    .EXAMPLE
        Get-JiraVersion -ID 162401 | Set-JiraVersion -Description 'Descriptive String'
        This example assigns the modifies the existing version with a new name 'New-Name'.
     .INPUTS
        [JiraPS.Version]
     .OUTPUTS
        [JiraPS.Version]
     .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(SupportsShouldProcess = $true)]
    param(
        # Version to be changed
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [Object[]] $Version,

        # New Name of the Version.
        [String] $Name,

        # New Description of the Version.
        [String] $Description,

        # New value for Archived.
        [Bool] $Archived,

        # New value for Released.
        [Bool] $Released,

        # New Date of the release.
        [DateTime] $ReleaseDate,

        # New Date of the user release.
        [DateTime] $StartDate,

        # The new Project where this version should be in.
        # This can be the ID of the Project, or the Project Object
        [Object] $Project,

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

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

        Write-Debug "[Set-JiraVersion] Completed Begin block."
    }

    process {
        foreach ($_version in $Version) {
            try {
                # Validate InputObject type
                if ($_version.PSObject.TypeNames[0] -ne "JiraPS.Version") {
                    Write-Error "Wrong object type provided for Version. Only JiraPS.Version is accepted"
                }

                $id = [Int]($_version.Id)
                $restUrl = "$server/rest/api/latest/version/$id"

                $props = @{}
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("Name")) {
                    $props["name"] = $Name
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("Description")) {
                    $props["description"] = $Description
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("Archived")) {
                    $props["archived"] = $Archived
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("Released")) {
                    $props["released"] = $Released
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("Project")) {
                    if ($Project.PSObject.TypeNames[0] -eq "JiraPS.Project") {
                        if ($Project.Id) {
                            $props["projectId"] = $Project.Id
                        }
                        elseif ($Project.Key) {
                            $props["project"] = $Project.Key
                        }
                    }
                    else {
                        $props["projectId"] = (Get-JiraProject $Project -Credential $Credential).Id
                    }
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("ReleaseDate")) {
                    $props["releaseDate"] = $ReleaseDate.ToString('yyyy-MM-dd')
                }
                if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("StartDate")) {
                    $props["startDate"] = $StartDate.ToString('yyyy-MM-dd')
                }

                Write-Debug -Message '[Set-JiraVersion] Converting to JSON'
                $json = ConvertTo-Json -InputObject $props

                if ($PSCmdlet.ShouldProcess($Name, "Updating Version on JIRA")) {
                    Write-Debug -Message '[Set-JiraVersion] Preparing for blastoff!'
                    Invoke-JiraMethod -Method Put -URI $restUrl -Body $json -Credential $Credential | ConvertTo-JiraVersion -Credential $Credential
                }
            }
            catch {
                Write-Error "Id of the Version was not available or could not be converted to Integer. Value was $($_version.Id)"
            }
        }
    }

    end {
        Write-Debug "[Set-JiraVersion] Complete"
    }
}