Public/Set-JiraIssue.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
function Set-JiraIssue {
    <#
    .Synopsis
       Modifies an existing issue in JIRA
    .DESCRIPTION
       This function modifies an existing isue in JIRA. This can include changing
       the issue's summary or description, or assigning the issue.
    .EXAMPLE
       Set-JiraIssue -Issue TEST-01 -Summary 'Modified issue summary' -Description 'This issue has been modified by PowerShell'
       This example changes the summary and description of the JIRA issue TEST-01.
    .EXAMPLE
       $issue = Get-JiraIssue TEST-01
       $issue | Set-JiraIssue -Description "$($issue.Description)`n`nEdit: Also foo."
       This example appends text to the end of an existing issue description by using
       Get-JiraIssue to obtain a reference to the current issue and description.
    .EXAMPLE
       Set-JiraIssue -Issue TEST-01 -Assignee 'Unassigned'
       This example removes the assignee from JIRA issue TEST-01.
    .EXAMPLE
       Set-JiraIssue -Issue TEST-01 -Assignee 'joe' -AddComment 'Dear [~joe], please review.'
       This example assigns the JIRA Issue TEST-01 to 'joe' and adds a comment at one.
    .INPUTS
       [JiraPS.Issue[]] The JIRA issue that should be modified
    .OUTPUTS
       If the -PassThru parameter is provided, this function will provide a reference
       to the JIRA issue modified. Otherwise, this function does not provide output.
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true
    )]
    param(
        # Issue key or JiraPS.Issue object returned from Get-JiraIssue
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('Key')]
        [Object[]] $Issue,

        # New summary of the issue.
        [Parameter(Mandatory = $false)]
        [String] $Summary,

        # New description of the issue.
        [Parameter(Mandatory = $false)]
        [String] $Description,

        # Set the FixVersion of the issue, this will overwrite any present FixVersions
        [Parameter(Mandatory = $false)]
        [Alias('FixVersions')]
        [String[]] $FixVersion,

        # New assignee of the issue. Enter 'Unassigned' to unassign the issue.
        [Parameter(Mandatory = $false)]
        [Object] $Assignee,

        # Labels to be set on the issue. These wil overwrite any existing
        # labels on the issue. For more granular control over issue labels,
        # use Set-JiraIssueLabel.
        [String[]] $Label,

        # Any additional fields that should be updated.
        [System.Collections.Hashtable] $Fields,

        # Add a comment ad once with your changes
        [Parameter(Mandatory = $false)]
        [String] $AddComment,

        # Path of the file where the configuration is stored.
        [ValidateScript( {Test-Path $_})]
        [String] $ConfigFile,

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

        # Whether output should be provided after invoking this function.
        [Switch] $PassThru
    )

    begin {
        Write-Debug "[Set-JiraIssue] Checking to see if we have any operations to perform"
        $fieldNames = $Fields.Keys
        if (-not ($Summary -or $Description -or $Assignee -or $Label -or $FixVersion -or $fieldNames)) {
            Write-Verbose "Nothing to do."
            return
        }

        if ($Assignee) {
            Write-Debug "[Set-JiraIssue] Testing Assignee type"
            if ($Assignee -eq 'Unassigned') {
                Write-Debug "[Set-JiraIssue] 'Unassigned' String passed. Issue will be assigned to no one."
                $assigneeString = ""
                $validAssignee = $true
            }
            else {
                Write-Debug "[Set-JiraIssue] Attempting to obtain Jira user [$Assignee]"
                $assigneeObj = Get-JiraUser -InputObject $Assignee -Credential $Credential
                if ($assigneeObj) {
                    Write-Debug "[Set-JiraIssue] User found (name=[$($assigneeObj.Name)],RestUrl=[$($assigneeObj.RestUrl)])"
                    $assigneeString = $assigneeObj.Name
                    $validAssignee = $true
                }
                else {
                    Write-Debug "[Set-JiraIssue] Unable to obtain Assignee. Exception will be thrown."
                    throw "Unable to validate Jira user [$Assignee]. Use Get-JiraUser for more details."
                }
            }
        }

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

    process {
        foreach ($i in $Issue) {
            $actOnIssueUri = $false
            $actOnAssigneeUri = $false

            Write-Debug "[Set-JiraIssue] Obtaining reference to issue"
            $issueObj = Get-JiraIssue -InputObject $i -Credential $Credential

            if ($issueObj) {
                $issueProps = @{
                    'update' = @{}
                }

                if ($Summary) {
                    # Update properties need to be passed to JIRA as arrays
                    $issueProps.update.summary = @()
                    $issueProps.update.summary += @{
                        'set' = $Summary;
                    }
                    $actOnIssueUri = $true
                }

                if ($Description) {
                    $issueProps.update.description = @()
                    $issueProps.update.description += @{
                        'set' = $Description;
                    }
                    $actOnIssueUri = $true
                }

                If ($FixVersion) {
                    $fixVersionSet = @()
                    Foreach ($f in $FixVersion) {
                        $fixVersionSet += @{
                            'name' = $f
                        }
                    }
                    $issueProps.update.fixVersions = @()
                    $issueProps.update.fixVersions += @{
                        'set' = $fixVersionSet;
                    }
                    $actOnIssueUri = $true
                }

                if ($AddComment) {
                    $issueProps.update.comment = @()
                    $issueProps.update.comment += @{
                        'add' = @{
                            'body' = $AddComment
                        }
                    }
                    $actOnIssueUri = $true
                }

                if ($Fields) {
                    Write-Debug "[Set-JiraIssue] Validating field names"
                    foreach ($k in $Fields.Keys) {
                        $name = $k
                        $value = $Fields.$k
                        Write-Debug "[Set-JiraIssue] Attempting to identify field (name=[$name], value=[$value])"

                        $f = Get-JiraField -Field $name -Credential $Credential
                        if ($f) {
                            # For some reason, this was coming through as a hashtable instead of a String,
                            # which was causing ConvertTo-Json to crash later.
                            # Not sure why, but this forces $id to be a String and not a hashtable.
                            $id = "$($f.ID)"
                            Write-Debug "[Set-JiraIssue] Field [$name] was identified as ID [$id]"
                            $issueProps.update.$id = @()
                            $issueProps.update.$id += @{
                                'set' = $value;
                            }
                            $actOnIssueUri = $true
                        }
                        else {
                            Write-Debug "[Set-JiraIssue] Field [$name] could not be identified in Jira"
                            throw "Unable to identify field [$name] from -Fields hashtable. Use Get-JiraField for more information."
                        }
                    }
                }

                if ($validAssignee) {
                    $assigneeProps = @{
                        'name' = $assigneeString;
                    }

                    $actOnAssigneeUri = $true
                }

                if ($actOnIssueUri) {
                    Write-Debug "[Set-JiraIssue] IssueProps: [$issueProps]"

                    Write-Debug "[Set-JiraIssue] Converting results to JSON"
                    $json = ConvertTo-Json -InputObject $issueProps -Depth 5
                    $issueObjURL = $issueObj.RestUrl

                    Write-Debug "[Set-JiraIssue] Checking for -WhatIf and Confirm"
                    if ($PSCmdlet.ShouldProcess($Issue, "Updating Issue [$IssueObj] from JIRA")) {
                        Write-Debug "[Set-JiraIssue] Preparing for blastoff!"
                        Invoke-JiraMethod -Method Put -URI $issueObjURL -Body $json -Credential $Credential
                    }
                }

                if ($actOnAssigneeUri) {
                    # Jira handles assignee differently; you can't change it from the default "edit issues" screen unless
                    # you customize the "Edit Issue" screen.

                    $assigneeUrl = "{0}/assignee" -f $issueObj.RestUrl
                    $json = ConvertTo-Json -InputObject $assigneeProps

                    Write-Debug "[Set-JiraIssue] Checking for -WhatIf and Confirm"
                    if ($PSCmdlet.ShouldProcess($Issue, "Updating Issue [Assignee] from JIRA")) {
                        Write-Debug "[Set-JiraIssue] Preparing for blastoff!"
                        Invoke-JiraMethod -Method Put -URI $assigneeUrl -Body $json -Credential $Credential
                    }
                }

                if ($Label) {
                    Write-Debug "[Set-JiraIssue] Invoking Set-JiraIssueLabel to set issue labels"
                    Set-JiraIssueLabel -Issue $issueObj -Set $Label -Credential $Credential
                }

                if ($PassThru) {
                    Write-Debug "[Set-JiraIssue] PassThru was specified. Obtaining updated reference to issue"
                    Get-JiraIssue -Key $issueObj.Key -Credential $Credential
                }
            }
            else {
                Write-Debug "[Set-JiraIssue] Unable to identify issue [$i]. Writing error message."
                Write-Error "Unable to identify issue [$i]"
            }
        }
    }

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