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

    process {
        foreach ($i in $InputObject) {
            # Write-Debug "[ConvertTo-JiraComment] Processing object: '$i'"

            # Write-Debug "[ConvertTo-JiraComment] Defining standard properties"
            $props = @{
                'ID'         = $i.id;
                'Body'       = $i.body;
                'Visibility' = $i.visibility;
                'RestUrl'    = $i.self;
            }

            # These fields should typically exist on an object returned from Jira,
            # but this provides a bit of extra error checking and safety

            # Write-Debug "[ConvertTo-JiraComment] Checking for author"
            if ($i.author) {
                $props.Author = ConvertTo-JiraUser -InputObject $i.author
            }

            # Write-Debug "[ConvertTo-JiraComment] Checking for update author"
            if ($i.updateAuthor) {
                $props.UpdateAuthor = ConvertTo-JiraUser -InputObject $i.updateAuthor
            }

            # Write-Debug "[ConvertTo-JiraComment] Checking for created date"
            if ($i.created) {
                $props.Created = (Get-Date ($i.created))
            }

            # Write-Debug "[ConvertTo-JiraComment] Checking for updated date"
            if ($i.updated) {
                $props.Updated = (Get-Date ($i.updated))
            }

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

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

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

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

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