Skip to content

Commit

Permalink
improved Get-WikiPage Handling
Browse files Browse the repository at this point in the history
implements AtlassianPS#15
  • Loading branch information
lipkau committed Apr 15, 2017
1 parent ebe9481 commit c77fbcc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 52 deletions.
93 changes: 49 additions & 44 deletions ConfluencePS/Public/Get-WikiChildPage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,61 +27,66 @@
.LINK
https://github.com/brianbunke/ConfluencePS
#>
[CmdletBinding()]
param (
[CmdletBinding(DefaultParameterSetName = "byID")]
[OutputType([ConfluencePS.Page])]
param (
# The URi of the API interface.
# Value can be set persistently with Set-WikiInfo.
[Parameter( Mandatory = $true )]
[URi]$apiURi,

# Confluence's credentials for authentication.
# Value can be set persistently with Set-WikiInfo.
[Parameter( Mandatory = $true )]
[PSCredential]$Credential,

# Filter results by page ID.
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateRange(1,[int]::MaxValue)]
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ParameterSetName = "byID"
)]
[ValidateRange(1, [int]::MaxValue)]
[Alias('ID')]
[int]$ParentID,
[int]$PageID,

# Find child pages by Page Object
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ParameterSetName = "byObject"
)]
[ConfluencePS.Page]$InputObject,

# Get all child pages recursively
[switch]$Recurse,

# Defaults to 25 max results; can be modified here.
# Numbers above 100 may not be honored if -Expand is used.
[ValidateRange(1,[int]::MaxValue)]
[int]$Limit,

# Additionally returns expanded results for each page (body, version, etc.).
# May negatively affect -Limit, client/server performance, and network bandwidth.
[switch]$Expand
[ValidateRange(1, [int]::MaxValue)]
[int]$PageSize
)

BEGIN {
If (!($Credential) -or !($BaseURI)) {
Write-Warning 'Confluence instance info not yet defined in this session. Calling Set-WikiInfo'
Set-WikiInfo
}
$depthLevel = "child"
}

PROCESS {
$URI = "$BaseURI/content/$ParentID/child/page"
Write-Debug "ParameterSetName: $($PsCmdlet.ParameterSetName)"
Write-Debug "PSBoundParameters: $($PSBoundParameters | Out-String)"

# URI prep based on specified parameters
If ($Expand -and $Limit) {
$URI = $URI + "?expand=body.view,version&limit=$Limit"
} ElseIf ($Expand) {
$URI = $URI + '?expand=body.view,version'
} ElseIf ($Limit) {
$URI = $URI + "?limit=$Limit"
if ($PsCmdlet.ParameterSetName -eq "byObject") {
$PageID = $InputObject.ID
}
if ($Recurse) {$depthLevel = "descendant"} # depth = ALL
$URI = "$apiURi/content/{0}/{1}/page" -f $PageID, $depthLevel

Write-Verbose "GET call from $URI"
$response = Invoke-WikiMethod -Uri $URI -Method Get

# Display results
# Hashing everything because I don't like the lower case property names from the REST call
If ($Expand) {
$response | Select -ExpandProperty Results |
Select @{n='ID'; e={$_.id}},
@{n='Title'; e={$_.title}},
@{n='Space'; e={$_._expandable.space -replace '/rest/api/space/',''}},
@{n='Ver'; e={$_.version.number}},
@{n='Body'; e={$_.body.view.value}}
} Else {
$response | Select -ExpandProperty Results |
Select @{n='ID'; e={$_.id}},
@{n='Title'; e={$_.title}},
@{n='Space'; e={$_._expandable.space -replace '/rest/api/space/',''}}
}
} #Process
} #Function
# URI prep based on specified parameters
$GETparameters = @{expand = "space,version,body.storage,ancestors"}
If ($PageSize) { $GETparameters["limit"] = $PageSize }

Write-Verbose "Fetching data from $URI"
Invoke-WikiMethod -Uri $URI -Method Get -Credential $Credential -GetParameters $GETparameters -OutputType ([ConfluencePS.Page])
}
}
35 changes: 34 additions & 1 deletion ConfluencePS/Public/Get-WikiPage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
Search again, this time piping in the page ID(s), to also capture version and body from the expanded results.
Store them in a variable for later use (e.g. Set-WikiPage).
.EXAMPLE
$meetingPages = Get-WikiPage -Label "meeting-notes" -SpaceKey PROJ1
Captures all the meeting note pages in the Proj1 Space.
.LINK
https://github.com/brianbunke/ConfluencePS
#>
Expand All @@ -57,7 +61,7 @@
)]
[ValidateRange(1, [int]::MaxValue)]
[Alias('ID')]
[int]$PageID,
[int[]]$PageID,

# Filter results by name.
[Parameter(
Expand All @@ -75,6 +79,9 @@
[Parameter(
ParameterSetName = "byTitle"
)]
[Parameter(
ParameterSetName = "byLabel"
)]
[Alias('Key')]
[string]$SpaceKey,

Expand All @@ -88,8 +95,19 @@
ValueFromPipeline = $true,
ParameterSetName = "byTitle"
)]
[Parameter(
ValueFromPipeline = $true,
ParameterSetName = "byLabel"
)]
[ConfluencePS.Space]$Space,

# Label(s) to use as search criteria to find pages
[Parameter(
Mandatory = $true,
ParameterSetName = "byLabel"
)]
[string[]]$Label,

# Maximimum number of results to fetch per call.
# This setting can be tuned to get better performance according to the load on the server.
# Warning: too high of a PageSize can cause a timeout on the request.
Expand Down Expand Up @@ -130,6 +148,21 @@
if ($SpaceKey) { $GETparameters["spaceKey"] = $SpaceKey }
If ($PageSize) { $GETparameters["limit"] = $PageSize }

Write-Verbose "Fetching data from $URI"
Invoke-WikiMethod -Uri $URI -Method Get -Credential $Credential -GetParameters $GETparameters -OutputType ([ConfluencePS.Page])
break
}
"byLabel" {
$URI = "$contentRoot/search"

$CQLparameters = @("type=page", "label=$Label")
if ($SpaceKey) {$CQLparameters += "space=$SpaceKey"}

$cqlQuery = ConvertTo-URLEncoded ($CQLparameters -join (" AND "))

$GETparameters["cql"] = $cqlQuery
If ($PageSize) { $GETparameters["limit"] = $PageSize }

Write-Verbose "Fetching data from $URI"
Invoke-WikiMethod -Uri $URI -Method Get -Credential $Credential -GetParameters $GETparameters -OutputType ([ConfluencePS.Page])
break
Expand Down
27 changes: 20 additions & 7 deletions Tests/ConfluencePS.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,16 @@ InModuleScope ConfluencePS {
$Title2 = "Pester New Page Orphan"
$Title3 = "Pester Test Space Home"
$Content = "<p>Hi Pester!</p>"
(Get-WikiSpace -SpaceKey $SpaceKey).Homepage | New-WikiLabel -Label "important" -ErrorAction Stop

# ACT
$GetTitle1 = Get-WikiPage -Title $Title1 -PageSize 200 -ErrorAction Stop
$GetTitle2 = Get-WikiPage -Title $Title2 -SpaceKey $SpaceKey -PageSize 200 -ErrorAction Stop
$GetID1 = Get-WikiPage -PageID $GetTitle1.ID -ErrorAction Stop
$GetID2 = Get-WikiPage -PageID $GetTitle2.ID -ErrorAction Stop
$GetKeys = Get-WikiPage -SpaceKey $SpaceKey | Sort ID -ErrorAction Stop
$GetSpacePage = Get-WikiPage -Space (Get-WikiSpace -SpaceKey $SpaceKey) -ErrorAction Stop
$GetTitle1 = Get-WikiPage -Title $Title1 -PageSize 200 -ErrorAction SilentlyContinue
$GetTitle2 = Get-WikiPage -Title $Title2 -SpaceKey $SpaceKey -PageSize 200 -ErrorAction SilentlyContinue
$GetID1 = Get-WikiPage -PageID $GetTitle1.ID -ErrorAction SilentlyContinue
$GetID2 = Get-WikiPage -PageID $GetTitle2.ID -ErrorAction SilentlyContinue
$GetKeys = Get-WikiPage -SpaceKey $SpaceKey | Sort ID -ErrorAction SilentlyContinue
$GetByLabel = Get-WikiPage -Label "important" -ErrorAction SilentlyContinue
$GetSpacePage = Get-WikiPage -Space (Get-WikiSpace -SpaceKey $SpaceKey) -ErrorAction SilentlyContinue

# ASSERT
It 'returns the correct amount of results' {
Expand All @@ -317,6 +319,7 @@ InModuleScope ConfluencePS {
$GetID1.Count | Should Be 1
$GetID2.Count | Should Be 1
$GetKeys.Count | Should Be 5
$GetByLabel.Count | Should Be 1
$GetSpacePage.Count | Should Be 5
}
It 'returns an object with specific properties' {
Expand All @@ -325,18 +328,21 @@ InModuleScope ConfluencePS {
$GetID1 | Should BeOfType [ConfluencePS.Page]
$GetID2 | Should BeOfType [ConfluencePS.Page]
$GetKeys | Should BeOfType [ConfluencePS.Page]
$GetByLabel | Should BeOfType [ConfluencePS.Page]
($GetTitle1 | Get-Member -MemberType Property).Count | Should Be 9
($GetTitle2 | Get-Member -MemberType Property).Count | Should Be 9
($GetID1 | Get-Member -MemberType Property).Count | Should Be 9
($GetID2 | Get-Member -MemberType Property).Count | Should Be 9
($GetKeys | Get-Member -MemberType Property).Count | Should Be 9
($GetByLabel | Get-Member -MemberType Property).Count | Should Be 9
}
It 'id is integer' {
$GetTitle1.ID | Should BeOfType [Int]
$GetTitle2.ID | Should BeOfType [Int]
$GetID1.ID | Should BeOfType [Int]
$GetID2.ID | Should BeOfType [Int]
$GetKeys.ID | Should BeOfType [Int]
$GetByLabel.ID | Should BeOfType [Int]
}
It 'id matches the specified value' {
$GetID1.ID | Should Be $GetTitle1.ID
Expand All @@ -351,18 +357,21 @@ InModuleScope ConfluencePS {
$GetID2.Title | Should BeExactly $Title2
$GetKeys.Title -contains $Title3 | Should Be $true
$GetKeys.Title -contains $GetID1.Title | Should Be $true
$GetByLabel.Title -like "PESTER * Home" | Should Be $true
}
It 'space matches the specified value' {
$GetTitle1.Space.Key | Should BeExactly $SpaceKey
$GetTitle2.Space.Key | Should BeExactly $SpaceKey
$GetID1.Space.Key | Should BeExactly $SpaceKey
$GetID2.Space.Key | Should BeExactly $SpaceKey
$GetKeys.Space.Key -contains $SpaceKey | Should Be $true
$GetByLabel.Space.Key | Should BeExactly $SpaceKey
}
It 'version matches the specified value' {
$GetTitle2.Version.Number | Should Be 1
$GetID2.Version.Number | Should Be 1
$GetKeys.Version.Number -contains 1 | Should Be $true
$GetByLabel.Version.Number | Should Be 1
}
It 'body matches the specified value' {
$GetTitle1.Body | Should BeExactly $Content
Expand All @@ -380,6 +389,8 @@ InModuleScope ConfluencePS {
$GetID2.URL | Should Not BeNullOrEmpty
$GetKeys.URL | Should BeOfType [String]
$GetKeys.URL | Should Not BeNullOrEmpty
$GetByLabel.URL | Should BeOfType [String]
$GetByLabel.URL | Should Not BeNullOrEmpty
}
It 'shorturl is string' {
$GetTitle1.ShortURL | Should BeOfType [String]
Expand All @@ -392,6 +403,8 @@ InModuleScope ConfluencePS {
$GetID2.ShortURL | Should Not BeNullOrEmpty
$GetKeys.ShortURL | Should BeOfType [String]
$GetKeys.ShortURL | Should Not BeNullOrEmpty
$GetByLabel.ShortURL | Should BeOfType [String]
$GetByLabel.ShortURL | Should Not BeNullOrEmpty
}
}

Expand All @@ -410,7 +423,7 @@ InModuleScope ConfluencePS {
# ASSERT
It 'returns the correct amount of results' {
($NewLabel1).Count | Should Be 3
($NewLabel2).Count | Should Be 8
($NewLabel2).Count | Should Be 9
}
It 'returns an object with specific properties' {
$NewLabel1 | Should BeOfType [ConfluencePS.Label]
Expand Down

0 comments on commit c77fbcc

Please sign in to comment.