refactor:
This commit is contained in:
parent
0d4751ff68
commit
88aae9e3a6
5 changed files with 578 additions and 328 deletions
410
src/Page.psm1
410
src/Page.psm1
|
|
@ -38,8 +38,8 @@ function New-Page
|
|||
# title of page to be published
|
||||
[Parameter()] [string]$Title,
|
||||
# pages manifest
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[Collections.Hashtable[]]$Manifest,
|
||||
[Parameter(Mandatory)]
|
||||
[PSCustomObject[]]$Manifest,
|
||||
# pages manifest index, mandatory for ancestor lookup
|
||||
[Parameter(Mandatory)] [Collections.Hashtable]$Index,
|
||||
# flag on whether to fail hard, or just continue
|
||||
|
|
@ -66,7 +66,8 @@ function New-Page
|
|||
|
||||
ElseIf (-Not $pageMeta.Ref)
|
||||
{
|
||||
$errMsg = "no reference to local content for page ``$Title``."
|
||||
$errMsg = ("``$($pageMeta.Title)``: no reference to local " +
|
||||
'content for page .')
|
||||
|
||||
If ($Strict) {throw $errMsg}
|
||||
|
||||
|
|
@ -78,7 +79,10 @@ function New-Page
|
|||
ElseIf ($pageMeta.Id)
|
||||
{
|
||||
|
||||
Write-Debug "skipping, page already published: $($pageMeta.Id)"
|
||||
Write-Debug (
|
||||
"New-Page: ``$($pageMeta.Title)``: skipping, already " +
|
||||
"published ($($pageMeta.Id))"
|
||||
)
|
||||
|
||||
$pageMeta
|
||||
|
||||
|
|
@ -87,13 +91,29 @@ function New-Page
|
|||
|
||||
Else
|
||||
{
|
||||
$content = Get-Content -Path $pageMeta.Ref
|
||||
Write-Host "New-Page: ``$($pageMeta.Title)``: creating"
|
||||
|
||||
Try
|
||||
{
|
||||
$content = Get-Content -Path $pageMeta.Ref | Out-String
|
||||
}
|
||||
|
||||
Catch
|
||||
{
|
||||
$errMsg = "``New-Page: $($PageMeta.Title)``: $_"
|
||||
|
||||
If ($Strict) {throw $errMsg}
|
||||
|
||||
Write-Host $errMsg
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
$contentHash = (Get-StringHash $content).Hash
|
||||
|
||||
$transportBody = @{
|
||||
'type' = 'page'
|
||||
'title' = $Title
|
||||
'title' = $pageMeta.Title
|
||||
'space' = @{
|
||||
'key' = $Space
|
||||
}
|
||||
|
|
@ -103,7 +123,7 @@ function New-Page
|
|||
'representation' = 'storage'
|
||||
}
|
||||
}
|
||||
} | ConvertTo-JSON
|
||||
} | ConvertTo-JSON -Depth 5
|
||||
|
||||
Try
|
||||
{
|
||||
|
|
@ -120,10 +140,12 @@ function New-Page
|
|||
|
||||
Catch
|
||||
{
|
||||
$errMsg = "skipping ``$pageMeta.Title``: $_"
|
||||
$errMsg = "skipping ``$($pageMeta.Title)``: $($_)"
|
||||
|
||||
If ($Strict)
|
||||
{
|
||||
$_
|
||||
|
||||
throw $errMsg
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +163,7 @@ function New-Page
|
|||
|
||||
$pageMeta | Add-Member `
|
||||
-NotePropertyName 'Version' `
|
||||
-NotePropertyValue $response.version.number `
|
||||
-NotePropertyValue "1" `
|
||||
-Force
|
||||
|
||||
$pageMeta | Add-Member `
|
||||
|
|
@ -176,107 +198,224 @@ function Update-Page
|
|||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Add a confluence page
|
||||
Update an existing Confluence page
|
||||
|
||||
.DESCRIPTION
|
||||
|
||||
This function is unaware of the publishing status of ancestors and
|
||||
assumes that ancestral hierarchy is maintained through the
|
||||
manifest's item order, therefore an index must be supplied.
|
||||
|
||||
If a page's metadata does not include a reference, it will be
|
||||
treated as a publishing failure and therefore not output the
|
||||
original metadata.
|
||||
|
||||
.OUTPUTS
|
||||
|
||||
When no $Title is provided and the $Manifest array only contains 1
|
||||
page metadata, the ``Count`` attribute is faulty. Why? Don't know.
|
||||
|
||||
.EXAMPLE
|
||||
Update-ConfluencePage
|
||||
Add-ConfluencePage `
|
||||
-Host 'confluence.contoso.com' `
|
||||
-Space 'TIARA' `
|
||||
-Title 'Testitest' `
|
||||
-Manifest @{}
|
||||
-Content @{}
|
||||
#>
|
||||
Param(
|
||||
[Parameter(Mandatory)] [string] $Host,
|
||||
# The name of the Confluence space to publish to
|
||||
[Parameter(Mandatory)] [string] $Space,
|
||||
# confluence instance hostname
|
||||
[Parameter(Mandatory)] [string]$Host,
|
||||
# name of the Confluence space to publish to
|
||||
[Parameter(Mandatory)] [string]$Space,
|
||||
# title of page to be published
|
||||
[Parameter(Mandatory)] [string] $Title,
|
||||
[Parameter()] [string]$Title,
|
||||
# pages manifest
|
||||
[Parameter(Mandatory)] [Array] $Manifest,
|
||||
# pages manifest index
|
||||
[Parameter()] [Collections.Hashtable] $Index
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[PSCustomObject[]]$Manifest,
|
||||
# pages manifest index, mandatory for ancestor lookup
|
||||
[Parameter(Mandatory)] [Collections.Hashtable]$Index,
|
||||
# flag on whether to fail hard, or just continue
|
||||
[Parameter()] [Switch]$Strict,
|
||||
# flag on whether to force update of page, regardless of content
|
||||
[Parameter()] [Switch]$Force
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$pat = Get-PersonalAccessToken $Host
|
||||
}
|
||||
|
||||
Process
|
||||
{
|
||||
$pageMeta = Get-PageMeta `
|
||||
-Host $Host `
|
||||
-Space $Space `
|
||||
-Title $Title `
|
||||
-Manifest $Manifest `
|
||||
-Index $Index
|
||||
|
||||
if (-Not $pageMeta.Ref)
|
||||
If ($Title -And $Manifest[$Index.$Title])
|
||||
{
|
||||
throw "no reference to local content for page '$Title'."
|
||||
$Manifest = @(
|
||||
$Manifest[$Index.$Title]
|
||||
)
|
||||
}
|
||||
|
||||
if (-Not $pageMeta.Id)
|
||||
ForEach($pageMeta in $Manifest)
|
||||
{
|
||||
throw "no id for page '$Title'."
|
||||
}
|
||||
If ($Title -And $pageMeta.Title -ne $Title) {continue}
|
||||
|
||||
$content = Get-Content -Path $pageMeta.Ref
|
||||
ElseIf (-Not $pageMeta.Ref)
|
||||
{
|
||||
$errMsg = "no reference to local content for page ``$Title``."
|
||||
|
||||
$hash = (Get-StringHash $content).Hash
|
||||
If ($Strict) {throw $errMsg}
|
||||
|
||||
if ($hash -eq $pageMeta.Hash)
|
||||
{
|
||||
Write-Host "content unchanged, skipping: '$Title'"
|
||||
Write-Host $errMsg
|
||||
|
||||
# yep, this is funny... This behaves like a return statement, because
|
||||
# a cmdlet, treats the input as an array of inputs. We keep it that
|
||||
# way so that all functions can properly act upon pipes. See
|
||||
# additional information on 'Process' blocks.
|
||||
continue
|
||||
}
|
||||
|
||||
# we're not updating this in place, so that we don't have to reset the
|
||||
# value opon failure
|
||||
$version = $pageMeta.Version + 1
|
||||
|
||||
$transportBody = @{
|
||||
'id' = $PageMeta.Id
|
||||
'type' = 'page'
|
||||
'title' = $Title
|
||||
'space' = @{
|
||||
'key' = $Space
|
||||
continue
|
||||
}
|
||||
'body' = @{
|
||||
'storage' = @{
|
||||
'value' = $content
|
||||
'representation' = 'storage'
|
||||
|
||||
ElseIf (-Not $pageMeta.Id)
|
||||
{
|
||||
$errMsg = "``$($pageMeta.Title)``: unknown page id"
|
||||
|
||||
If ($Strict) {throw $errMsg}
|
||||
|
||||
Write-Host $errMsg
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
ElseIf (-Not $pageMeta.Version)
|
||||
{
|
||||
Write-Host "``$($pageMeta.Title)``: unknown (current) version"
|
||||
|
||||
$pageMeta
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
Else
|
||||
{
|
||||
Try
|
||||
{
|
||||
$content = Get-Content -Path $pageMeta.Ref | Out-String
|
||||
}
|
||||
|
||||
Catch
|
||||
{
|
||||
$errMsg = "``$Title``: $_"
|
||||
|
||||
If ($Strict) {throw $errMsg}
|
||||
|
||||
Write-Host $errMsg
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
$version = [Int]($pageMeta.Version) + 1
|
||||
|
||||
$contentHash = (Get-StringHash $content).Hash
|
||||
|
||||
If (
|
||||
$pageMeta.Hash -And
|
||||
$pageMeta.Hash -eq $contentHash -And
|
||||
-Not $Force
|
||||
)
|
||||
{
|
||||
Write-Debug (
|
||||
"Update-Page: ``$($pageMeta.Title)``: skipping, no " +
|
||||
"content changes"
|
||||
)
|
||||
|
||||
$pageMeta
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
Else
|
||||
{
|
||||
Write-Host "``$($pageMeta.Title)``: updating"
|
||||
}
|
||||
|
||||
# status needs to be set as to restore the page, if it is
|
||||
# trashed
|
||||
$transportBody = @{
|
||||
'id' = $PageMeta.Id
|
||||
'type' = 'page'
|
||||
'title' = $pageMeta.Title
|
||||
'space' = @{
|
||||
'key' = $Space
|
||||
}
|
||||
'body' = @{
|
||||
'storage' = @{
|
||||
'value' = $content
|
||||
'representation' = 'storage'
|
||||
}
|
||||
}
|
||||
'status' = 'current'
|
||||
'version' = @{
|
||||
'number' = $version
|
||||
}
|
||||
} | ConvertTo-JSON -WarningAction 'SilentlyContinue'
|
||||
|
||||
Try
|
||||
{
|
||||
Invoke-WebRequest `
|
||||
-Uri ("https://${Host}/rest/api/content/" +
|
||||
$PageMeta.Id) `
|
||||
-Method 'Put' `
|
||||
-Headers @{
|
||||
'Authorization' = "Bearer $pat"
|
||||
} `
|
||||
-ContentType "application/json" `
|
||||
-Body $transportBody `
|
||||
-OutVariable rawResponse | Out-Null
|
||||
}
|
||||
|
||||
Catch
|
||||
{
|
||||
$errMsg = "skipping ``$($pageMeta.Title)``: $_"
|
||||
|
||||
If ($Strict)
|
||||
{
|
||||
$_
|
||||
|
||||
throw $errMsg
|
||||
}
|
||||
|
||||
Write-Host $errMsg
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
# response isn't needed since no field will be updated by the
|
||||
# Confluence instance itself
|
||||
#$response = ($rawResponse.Content | ConvertFrom-JSON)
|
||||
|
||||
$pageMeta | Add-Member `
|
||||
-NotePropertyName 'Version' `
|
||||
-NotePropertyValue $version `
|
||||
-Force
|
||||
|
||||
$pageMeta | Add-Member `
|
||||
-NotePropertyName 'Hash' `
|
||||
-NotePropertyValue $contentHash `
|
||||
-Force
|
||||
|
||||
If (
|
||||
($Title -And $pageMeta.Title -eq $Title) -Or
|
||||
$Manifest.Count -eq 1
|
||||
)
|
||||
{
|
||||
# TODO: further research mechanism of expanding single item
|
||||
# array pipelines. For now we have to apply the unary
|
||||
# operator, otherwise we get a wrong count on the output
|
||||
,@($pageMeta)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
Else
|
||||
{
|
||||
$pageMeta
|
||||
}
|
||||
}
|
||||
'version' = @{
|
||||
'number' = $version
|
||||
}
|
||||
} | ConvertTo-JSON
|
||||
|
||||
Invoke-WebRequest `
|
||||
-Uri "https://${Host}/rest/api/content/$($PageMeta.Id)" `
|
||||
-Method 'Put' `
|
||||
-Headers @{
|
||||
'Authorization' = "Bearer $(Get-PersonalAccessToken $Host)"
|
||||
} `
|
||||
-ContentType "application/json" `
|
||||
-Body $transportBody `
|
||||
-OutVariable rawResponse | Out-Null
|
||||
}
|
||||
|
||||
End
|
||||
{
|
||||
$response = ($rawResponse.Content | ConvertFrom-JSON)
|
||||
|
||||
Update-PageMeta `
|
||||
-Title $Title `
|
||||
-Id $pageMeta.Id `
|
||||
-Version $response.version.number `
|
||||
-Hash $hash `
|
||||
-Manifest $Manifest `
|
||||
-Index $Index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -284,82 +423,43 @@ function Update-Page
|
|||
function Publish-Page
|
||||
{
|
||||
Param(
|
||||
# title of the page (used for manifest lookup)
|
||||
[Parameter(Mandatory)] [string] $Title,
|
||||
# hostname of Confluence instance
|
||||
[Parameter(Mandatory)] [string] $Host,
|
||||
# name of Confluence space
|
||||
[Parameter(Mandatory)] [string] $Space,
|
||||
# manifest object
|
||||
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $Meta
|
||||
# confluence instance hostname
|
||||
[Parameter(Mandatory)] [string]$Host,
|
||||
# name of the Confluence space to publish to
|
||||
[Parameter(Mandatory)] [string]$Space,
|
||||
# title of page to be published
|
||||
[Parameter()] [string]$Title,
|
||||
# pages manifest
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[PSCustomObject[]]$Manifest,
|
||||
# pages manifest index, mandatory for ancestor lookup
|
||||
[Parameter(Mandatory)] [Collections.Hashtable]$Index,
|
||||
# flag on whether to fail hard, or just continue
|
||||
[Parameter()] [Switch]$Strict,
|
||||
# flag on whether to force update of page, regardless of content
|
||||
[Parameter()] [Switch]$Force
|
||||
)
|
||||
|
||||
Process
|
||||
{
|
||||
ForEach($meta in $Meta)
|
||||
{
|
||||
$meta = Get-PageMeta `
|
||||
-Host $hostname `
|
||||
-Space $spaceName `
|
||||
-Title $Title `
|
||||
-Manifest $Manifest
|
||||
$result = Update-Page `
|
||||
-Host $Host `
|
||||
-Space $Space `
|
||||
-Manifest $Manifest `
|
||||
-Index $Index `
|
||||
-Strict:$Strict `
|
||||
-Force:$Force
|
||||
|
||||
if ($meta.AncestorTitle)
|
||||
{
|
||||
$ancestorPageMeta = Get-PageMeta `
|
||||
-Host $hostname `
|
||||
-Space $spaceName `
|
||||
-Title $pageMeta.AncestorTitle `
|
||||
-Manifest $Manifest
|
||||
$result = New-Page `
|
||||
-Host $Host `
|
||||
-Space $Space `
|
||||
-Manifest $result `
|
||||
-Index $Index `
|
||||
-Strict:$Strict
|
||||
}
|
||||
|
||||
if (-Not ($ancestorPageMeta -Or $ancestorPageMeta.PageId))
|
||||
{
|
||||
Write-Host "ancestor, not published, skipping: $Title"
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if (-Not $pageId)
|
||||
{
|
||||
Write-Host ("create ${_}: $prettyName")
|
||||
|
||||
try {
|
||||
New-Page `
|
||||
-Host $hostname `
|
||||
-Space $spaceName `
|
||||
-Title $Title `
|
||||
-Manifest $Manifest
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
Write-Host "error for '$Title', skipping: $_"
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Write-Host ("update ${_} (${pageId}): $prettyName")
|
||||
|
||||
try
|
||||
{
|
||||
Update-Page `
|
||||
-Host $hostname `
|
||||
-Space $Space `
|
||||
-Title $Title `
|
||||
-Manifest $Manifest
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
Write-Host "error for '$Title', skipping: $_"
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
End
|
||||
{
|
||||
$result
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue