XML-RPC/Labels.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#Requires -Version 3.0
<# Labels #>
function Get-ConfluenceLabels {
    <#
    .SYNOPSIS
        Remove Page from Confluence
 
    .DESCRIPTION
        Remove a Page from Confluence
 
    .NOTES
        AUTHOR : Oliver Lipkau <oliver@lipkau.net>
        VERSION: 0.0.1 - OL - Initial Code
                 1.0.0 - OL - Replaced hashtables with Objects
 
    .INPUTS
        string
        int
        Confluence.Page
        Confluence.PageSummary
 
    .OUTPUTS
 
 
    .EXAMPLE
        Remove-ConfluencePage -apiURi "http://example.com" -token "000000" -pageId 12345678
        -----------
        Description
        Remove a specific Page by it's ID
 
 
    .EXAMPLE
        $param = @{apiURi = "http://example.com"; token = "000000"}
        $Page = Get-ConfluencePage @param -spacekey "ABC" -pagetitle "My new Title"
        Remove-ConfluencePage @param -page $page
        -----------
        Description
        Fetch a Page and remove it
 
    .EXAMPLE
        $param = @{apiURi = "http://example.com"; token = "000000"}
        Get-ConfluencePage @param -spacekey "ABC" | Remove-ConfluencePage @param
        -----------
        Description
        Fetch all Pages in a Space and remove them
 
    .LINK
        Atlassians's Docs:
            Vector getLabelsById(String token, long objectId) - Returns all labels for the given ContentEntityObject ID
            Vector getMostPopularLabels(String token, int maxCount) - Returns the most popular labels for the Confluence instance, with a specified maximum number.
            Vector getMostPopularLabelsInSpace(String token, String spaceKey, int maxCount) - Returns the most popular labels for the given spaceKey, with a specified maximum number of results.
            Vector getRecentlyUsedLabels(String token, int maxResults) - Returns the recently used labels for the Confluence instance, with a specified maximum number of results.
            Vector getRecentlyUsedLabelsInSpace(String token, String spaceKey, int maxResults) - Returns the recently used labels for the given spaceKey, with a specified maximum number of results.
            TODO: Vector getLabelsByDetail(String token, String labelName, String namespace, String spaceKey, String owner) - Retrieves the labels matching the given labelName, namespace, spaceKey or owner.
 
    #>

    [CmdletBinding(
        DefaultParameterSetName='getRecentlyUsedLabels'
    )]
    [OutputType(
        [Confluence.Label],
        [Confluence.Label[]]
    )]
    param(
        # The URi of the API interface.
        [Parameter(
            Position=0,
            Mandatory=$true
        )]
        [string]$apiURi,

        # Confluence's Authentication Token.
        [Parameter(
            Position=1,
            Mandatory=$true
        )]
        [string]$Token,

        # Id of the object from which to retrieve the Labels
        [Parameter(
            Position=2,
            Mandatory=$true,
            ValueFromPipelineByPropertyName=$true,
            ParameterSetName="getLabelsById"
        )]
        [Alias('id')]
        [long]$objectId,

        # Spapce from which to retrieve the Labels
        [Parameter(
            Position=2,
            Mandatory=$true,
            ParameterSetName="getMostPopularLabels"
        )]
        [Parameter(
            Position=2,
            ParameterSetName="getRecentlyUsedLabels"
        )]
        [Parameter(
            ParameterSetName="getLabelsByDetail"
        )]
        [Alias("Space")]
        [string]$SpaceKey,

        # Limit the numer of resutls
        [Parameter(
            ParameterSetName="getMostPopularLabels"
        )]
        [Parameter(
            ParameterSetName="getRecentlyUsedLabels"
        )]
        [int16]$maxCount = 100,

        # Whether to get Most Popular or Most Recent Labels
        [Parameter(
            ParameterSetName="getMostPopularLabels"
        )]
        [Parameter(
            ParameterSetName="getRecentlyUsedLabels"
        )]
        [ValidateSet(
            "recent",
            "popular"
        )]
        [string]$Condition = "recent",

        # Name of a specific Label
        [Parameter(
            ParameterSetName="getLabelsByDetail"
        )]
        [string]$LabelName = "",

        #
        [Parameter(
            ParameterSetName="getLabelsByDetail"
        )]
        [string]$NameSpace = "",

        #
        [Parameter(
            ParameterSetName="getLabelsByDetail"
        )]
        [string]$Owner =""
    )

    Begin
        { Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started" }

    Process {
        switch ($PsCmdlet.ParameterSetName) {
            "getLabelsById" {
                $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getLabelsById" -Params ($token,$objectId))
                if ($response)
                {
                    foreach ($Label in $response)
                    {
                        [Confluence.Label]$Label
                    }
                }
                break
            }
            {"getRecentlyUsedLabels","getMostPopularLabels" -contains $_} {
                if ($Condition -eq "recent")
                {
                    if ($spaceKey)
                    {
                        $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getRecentlyUsedLabelsInSpace" -Params ($token,$SpaceKey,$maxCount))
                        if ($response)
                        {
                            foreach ($Label in $response)
                            {
                                [Confluence.Label]$Label
                            }
                        }
                        break
                    } else {
                        $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getRecentlyUsedLabels" -Params ($token,$maxCount))
                        if ($response)
                        {
                            foreach ($Label in $response)
                            {
                                [Confluence.Label]$Label
                            }
                        }
                        break
                    }
                } else {
                    if ($spaceKey)
                    {
                        $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getMostPopularLabelsInSpace" -Params ($token,$SpaceKey,$maxCount))
                        if ($response)
                        {
                            foreach ($Label in $response)
                            {
                                [Confluence.Label]$Label
                            }
                        }
                        break
                    } else {
                        $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getMostPopularLabels" -Params ($token,$maxCount))
                        if ($response)
                        {
                            foreach ($Label in $response)
                            {
                                [Confluence.Label]$Label
                            }
                        }
                        break
                    }
                }
            }
            "getLabelsByDetail" {
                $response = ConvertFrom-Xml (Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getLabelsByDetail" -Params ($token,$labelName,$namespace,$spaceKey, $owner))
                if ($response)
                {
                    foreach ($Label in $response)
                    {
                        [Confluence.Label]$Label
                    }
                }
                break
            }
        }
    }

    End
        { Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended" }
}
#getSpacesWithLabel is in <Space/>
<#function Get-ConfluenceRelatedLabel {
    #Vector getRelatedLabels(String token, String labelName, int maxResults) - Returns the labels related to the given label name, with a specified maximum number of results.
    #Vector getRelatedLabelsInSpace(String token, String labelName, String spaceKey, int maxResults) - Returns the labels related to the given label name for the given spaceKey, with a specified maximum number of results.
}#>

<#function Get-ConfluenceLabelContent {
    #Vector getLabelContentById(String token, long labelId) - Returns the content for a given label ID
    #Vector getLabelContentByName(String token, String labelName) - Returns the content for a given label name.
    #Vector getLabelContentByObject(String token, Label labelObject) - Returns the content for a given Label object.
}#>

#getSpacesContainingContentWithLabel is in <Space/>
function Add-ConfluenceLabel {
    <#
        .SYNOPSIS
            Adds Label to Confluence Content
 
        .DESCRIPTION
            Adds Label to Confluence Content
 
        .NOTES
            AUTHOR : Oliver Lipkau <oliver@lipkau.net>
            VERSION: 1.0.0 - OL - Initial Code
 
        .INPUTS
            string
            Confluence.Label
 
        .OUTPUTS
            Boolean
 
        .EXAMPLE
 
 
        .LINK
            Atlassians's Docs:
                boolean addLabelByName(String token, String labelName, long objectId) - Adds label(s) to the object with the given ContentEntityObject ID. For multiple labels, labelName should be in the form of a space-separated or comma-separated string.
                boolean addLabelById(String token, long labelId, long objectId) - Adds a label with the given ID to the object with the given ContentEntityObject ID.
                boolean addLabelByObject(String token, Label labelObject, long objectId) - Adds the given label object to the object with the given ContentEntityObject ID.
                boolean addLabelByNameToSpace(String token, String labelName, String spaceKey) - Adds a label to description of a space with the given space key. Prefix labelName with "team:" in order to make it a space category.
    #>

    [CmdletBinding(
        ConfirmImpact = 'Low',
        SupportsShouldProcess = $true,
        DefaultParameterSetName = 'byString'
    )]
    [OutputType(
        [bool]
    )]
    param(
        # The URi of the API interface.
        # Value can be set persistently with Set-ConfluenceEndpoint.
        [Parameter(
            Mandatory=$true
        )]
        [string]$apiURi,

        # Confluence's Authentication Token.
        # Value can be set persistently with Set-ConfluenceEndpoint.
        [Parameter(
            Mandatory=$true
        )]
        [string]$Token,

        # Confluence Page.
        # Can be sent though the pipe, in which case an object of type Confluence.Page is expected.
        [Parameter(
            Position=0,
            Mandatory=$true,
            ValueFromPipeline=$true,
            ParameterSetName = 'byString',
            ValueFromPipelinebyPropertyName = $true
        )]
        [Parameter(
            Position=0,
            Mandatory=$true,
            ValueFromPipeline=$true,
            ParameterSetName = 'byLabelObject',
            ValueFromPipelinebyPropertyName = $true
        )]
        [Parameter(
            Position=0,
            Mandatory=$true,
            ValueFromPipeline=$true,
            ParameterSetName = 'byLabelId',
            ValueFromPipelinebyPropertyName = $true
        )]
        [Alias("id")]
        $Object,

        # Key of the Space to be labeled
        [Parameter(
            Position=0,
            Mandatory=$true,
            ParameterSetName = 'onSpace'
        )]
        [string]$SpaceKey,

        # Label as text string
        [Parameter(
            Position=1,
            Mandatory=$true,
            ParameterSetName = 'onSpace'
        )]
        [Parameter(
            Position=1,
            Mandatory=$true,
            ParameterSetName = 'byString'
        )]
        [string[]]$LabelName,


        [Parameter(
            Position=1,
            Mandatory=$true,
            ParameterSetName = 'byLabelId'
        )]
        [long]$LabelId,

        [Parameter(
            Position=1,
            Mandatory=$true,
            ParameterSetName = 'byLabelObject'
        )]
        [Confluence.Label]$Label
    )

    Begin
    {
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"
    }

    Process
    {
        # Ensure ValueByPipeProperty is of a valid Type
        Write-Debug "$($MyInvocation.MyCommand.Name):: Value from Pipe $(if ($_ -ne $null) { "is of Type $($_.getType())" } else { "was not provided" })"
        if ($_ `
            -and $PsCmdlet.ParameterSetName -in @('byString', 'byLabelObject', 'byLabelId') `
            -and (!(
                $_ -is [Confluence.PageSummary] `
                -or $_ -is [Confluence.Page] `
                -or $_ -is [Confluence.BlogEntrySummary] `
                -or $_ -is [Confluence.BlogEntry] `
                -or $_ -is [Confluence.Attachment] `
                -or $_ -is [Confluence.ContentSummary] `
            ))
        )
        {
            Throw "Invalid object provided"
        }

        if ($PsCmdlet.ParameterSetName -in @('byString', 'byLabelObject', 'byLabelId') -and (!($Object.id)))
        {
            throw "Invalid object provided"
        }

        switch ($PsCmdlet.ParameterSetName)
        {
            "byString"
            {
                foreach ($labelAsString in $LabelName)
                {
                    Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding Label $LabelName to $($Object.id)"
                    if ($PSCmdlet.ShouldProcess("Object $($Object.id)","Add Label"))
                    {
                        Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addLabelByName" -Params ($token, $labelAsString, $Object.id) -OutputType "bool"
                    }
                }
            }
            "byLabelObject"
            {
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding Label $($Label.name) to $($Object.id)"
                if ($PSCmdlet.ShouldProcess("Object $($Object.id)","Add Label"))
                {
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addLabelByObject" -Params ($token, $Label, $Object.id) -OutputType "bool"
                }
            }
            "byLabelId"
            {
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding Label $LabelId to $($Object.id)"
                if ($PSCmdlet.ShouldProcess("Object $($Object.id)","Add Label"))
                {
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addLabelById" -Params ($token, $LabelId, $Object.id) -OutputType "bool"
                }
            }
            "onSpace"
            {
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding Label $LabelName to Space $SpaceKey"
                if ($PSCmdlet.ShouldProcess("Space $SpaceKey","Add Label"))
                {
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addLabelByNameToSpace" -Params ($token, $LabelName, $SpaceKey) -OutputType "bool"
                }
            }
        }
    }

    End
        { Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended" }
}
<#function Remove-ConfluenceLabel {
    #boolean removeLabelByName(String token, String labelName, long objectId) - Removes the given label from the object with the given ContentEntityObject ID.
    #boolean removeLabelById(String token, long labelId, long objectId) - Removes the label with the given ID from the object with the given ContentEntityObject ID.
    #boolean removeLabelByObject(String token, Label labelObject, long objectId) - Removes the given label object from the object with the given ContentEntityObject ID.
    #boolean removeLabelByNameFromSpace(String token, String labelName, String spaceKey) - Removes the given label from the given spaceKey.
}#>

<# /Labels #>