Public/Format-Jira.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
function Format-Jira {
    <#
    .Synopsis
       Converts an object into a table formatted according to JIRA's markdown syntax
    .DESCRIPTION
       This function converts a PowerShell object into a table using JIRA's markdown syntax. This can then be added to a JIRA issue description or comment.
 
       Like the native Format-* cmdlets, this is a destructive operation, so as always, remember to "filter left, format right!"
    .EXAMPLE
       Get-Process | Format-Jira | Add-JiraIssueComment -Issue TEST-001
       This example illustrates converting the output from Get-Process into a JIRA table, which is then added as a comment to issue TEST-001.
    .EXAMPLE
       Get-Process chrome | Format-Jira Name,Id,VM
       This example obtains all Google Chrome processes, then creates a JIRA table with only the Name,ID, and VM properties of each object.
    .INPUTS
       [System.Object[]] - accepts any Object via pipeline
    .OUTPUTS
       [System.String]
    .NOTES
       This is a destructive operation, since it permanently reduces InputObjects to Strings. Remember to "filter left, format right."
    #>

    [CmdletBinding()]
    [OutputType([System.String])]
    param(
        # List of properties to display. If omitted, only the default properties will be shown.
        #
        # To display all properties, use -Property *.
        [Parameter(
            Position = 0,
            Mandatory = $false
        )]
        [Object[]] $Property,

        # Object to format.
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromRemainingArguments = $true
        )]
        [ValidateNotNull()]
        [PSObject[]] $InputObject
    )

    begin {
        $headers = New-Object -TypeName System.Collections.ArrayList
        $thisLine = New-Object -TypeName System.Text.StringBuilder
        $allText = New-Object -TypeName System.Text.StringBuilder

        $headerDefined = $false

        $n = [System.Environment]::NewLine

        if ($Property) {
            if ($Property -eq '*') {
                Write-Debug "[Format-Jira] -Property * was passed. Adding all properties."
            }
            else {

                foreach ($p in $Property) {
                    Write-Debug "[Format-Jira] Adding header [$p]"
                    [void] $headers.Add($p.ToString())
                }

                $headerString = "||$(($headers.ToArray()) -join '||')||"
                Write-Debug "[Format-Jira] Full header: [$headerString]"
                [void] $allText.Append($headerString)
                $headerDefined = $true
            }
        }
        else {
            Write-Debug "[Format-Jira] Property parameter was not specified. Checking first InputObject for property names."
        }
    }

    process {
        foreach ($i in $InputObject) {
            if (-not ($headerDefined)) {
                # This should only be called if Property was not supplied and this is the first object in the InputObject array.
                if ($Property -and $Property -eq '*') {
                    Write-Debug "[Format-Jira] Adding all properties from object [$i]"
                    $allProperties = Get-Member -InputObject $i -MemberType '*Property'
                    foreach ($a in $allProperties) {
                        Write-Debug "[Format-Jira] Adding header [$($a.Name)]"
                        [void] $headers.Add($a.Name)
                    }
                }
                else {

                    # TODO: find a way to format output objects based on PowerShell's own Format-Table
                    # Identify default table properties if possible and use them to create a Jira table

                    if ($i.PSStandardMembers.DefaultDisplayPropertySet) {
                        Write-Debug "[Format-Jira] Identifying default properties for object [$i]"
                        $propertyNames = $i.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
                        foreach ($p in $propertyNames) {
                            Write-Debug "[Format-Jira] Adding header [$p]"
                            [void] $headers.Add($p)
                        }
                    }
                    else {
                        Write-Debug "[Format-Jira] No default format data exists for object [$i] (type=[$($i.GetType())]). All properties will be used."
                        $allProperties = Get-Member -InputObject $i -MemberType '*Property'
                        foreach ($a in $allProperties) {
                            Write-Debug "[Format-Jira] Adding header [$($a.Name)]"
                            [void] $headers.Add($a.Name)
                        }
                    }
                }

                $headerString = "||$(($headers.ToArray()) -join '||')||"
                Write-Debug "[Format-Jira] Full header: [$headerString]"
                [void] $allText.Append($headerString)
                $headerDefined = $true
            }

            Write-Debug "[Format-Jira] Processing object [$i]"
            [void] $thisLine.Clear()
            [void] $thisLine.Append("$n|")

            foreach ($h in $headers) {
                $value = $InputObject.$h
                if ($value) {
                    Write-Debug "[Format-Jira] Adding property (name=[$h], value=[$value])"
                    [void] $thisLine.Append("$value|")
                }
                else {
                    Write-Debug "[Format-Jira] Property [$h] does not exist on this object."
                    [void] $thisLine.Append(' |')
                }
            }

            $thisLineString = $thisLine.ToString()
            Write-Debug "[Format-Jira] Completed line: [$thisLineString]"
            [void] $allText.Append($thisLineString)
        }
    }

    end {
        $allTextString = $allText.ToString()
        Write-Output $allTextString
        Write-Debug "[Format-Jira] Complete"
    }
}