Internal/ConvertTo-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
function ConvertTo-JiraIssue {
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true
        )]
        [PSObject[]] $InputObject,

        [Switch] $IncludeDebug,

        [Switch] $ReturnError
    )

    begin {
        $userFields = @('Assignee', 'Creator', 'Reporter')
        $dateFields = @('Created', 'LastViewed', 'Updated')

        $transitions = New-Object -TypeName System.Collections.ArrayList
        $comments = New-Object -TypeName System.Collections.ArrayList
    }

    process {
        foreach ($i in $InputObject) {
            [void] $transitions.Clear()
            [void] $comments.Clear()

            # Write-Debug "[ConvertTo-JiraIssue] Processing object: '$i'"

            if ($i.errorMessages) {
                # Write-Debug "[ConvertTo-JiraIssue] Detected an errorMessages property. This is an error result."

                if ($ReturnError) {
                    # Write-Debug "[ConvertTo-JiraIssue] Outputting details about error message"
                    $props = @{
                        'ErrorMessages' = $i.errorMessages;
                    }

                    $result = New-Object -TypeName PSObject -Property $props
                    $result.PSObject.TypeNames.Insert(0, 'JiraPS.Error')

                    Write-Output $result
                }
            }
            else {
                $server = ($InputObject.self -split 'rest')[0]
                $http = "${server}browse/$($i.key)"

                # Write-Debug "[ConvertTo-JiraIssue] Defining standard properties"
                $props = @{
                    'Key'         = $i.key;
                    'ID'          = $i.id;
                    'RestUrl'     = $i.self;
                    'HttpUrl'     = $http;
                    'Summary'     = $i.fields.summary;
                    'Description' = $i.fields.description;
                    'Status'      = $i.fields.status.name;
                }
                if ($i.fields.issuelinks) { $props['IssueLinks'] = (ConvertTo-JiraIssueLink $i.fields.issuelinks)}

                if ($i.fields.project) {
                    # Write-Debug "[ConvertTo-JiraIssue] Obtaining reference to project"
                    $props.Project = ConvertTo-JiraProject -InputObject $i.fields.project
                }

                foreach ($field in $userFields) {
                    # Write-Debug "[ConvertTo-JiraIssue] Checking for user field [$field]"
                    if ($i.fields.$field) {
                        # Write-Debug "[ConvertTo-JiraIssue] Adding user field $field"
                        $props.$field = ConvertTo-JiraUser -InputObject $i.fields.$field
                    }
                    elseif ($field -eq 'Assignee') {
                        # Write-Debug "[ConvertTo-JiraIssue] Adding 'Unassigned' assignee field"
                        $props.Assignee = 'Unassigned'
                    }
                    else {
                        # Write-Debug "[ConvertTo-JiraIssue] Object does not appear to contain property [$field]"
                    }
                }

                foreach ($field in $dateFields) {
                    if ($i.fields.$field) {
                        # Write-Debug "[ConvertTo-JiraIssue] Adding date field $field"
                        $props.$field = Get-Date -Date ($i.fields.$field)
                    }
                }

                if ($IncludeDebug) {
                    # Write-Debug "[ConvertTo-JiraIssue] Defining debug properties"
                    $props.Fields = $i.fields
                    $props.Expand = $i.expand
                }

                # Write-Debug "[ConvertTo-JiraIssue] Adding transitions"
                [void] $transitions.Clear()
                foreach ($t in $i.transitions) {
                    [void] $transitions.Add((ConvertTo-JiraTransition -InputObject $t))
                }
                $props.Transition = ($transitions.ToArray())

                # Write-Debug "[ConvertTo-JiraIssue] Adding comments"
                [void] $comments.Clear()
                if ($i.fields.comment) {
                    if ($i.fields.comment.comments) {
                        foreach ($c in $i.fields.comment.comments) {
                            [void] $comments.Add((ConvertTo-JiraComment -InputObject $c))
                        }
                        $props.Comment = ($comments.ToArray())
                    }
                }

                # Write-Debug "[ConvertTo-JiraIssue] Checking for any additional fields"
                $extraFields = $i.fields.PSObject.Properties | Where-Object -FilterScript { $_.Name -notin $props.Keys }
                foreach ($f in $extraFields) {
                    $name = $f.Name
                    # Write-Debug "[ConvertTo-JiraIssue] Adding property [$name] with value [$($f.Value)]"
                    $props[$name] = $f.Value
                }

                # Write-Debug "[ConvertTo-JiraIssue] Creating PSObject out of properties"
                $result = New-Object -TypeName PSObject -Property $props

                # Write-Debug "[ConvertTo-JiraIssue] Inserting type name information"
                $result.PSObject.TypeNames.Insert(0, 'JiraPS.Issue')

                # Write-Debug "[ConvertTo-JiraProject] Inserting custom toString() method"
                $result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
                    Write-Output "[$($this.Key)] $($this.Summary)"
                }

                # Write-Debug "[ConvertTo-JiraIssue] Outputting object"
                Write-Output $result
            }
        }
    }

    end {
        # Write-Debug "[ConvertTo-JiraIssue] Complete"
    }
}