fix: attachment and page debug logs

This commit is contained in:
Rodweil, Theodor 2023-08-14 05:10:40 +02:00
parent b2ddd0fd38
commit 4fa55b8602
No known key found for this signature in database
GPG key ID: F8BC1B0EB1F9CCF5
2 changed files with 304 additions and 10 deletions

View file

@ -116,7 +116,7 @@ function New-Attachment
{ {
Write-Debug ( Write-Debug (
"New-Attachment: ``$($attacmentMeta.Name)``: skipping, " + "New-Attachment: ``$($attachmentMeta.Name)``: skipping, " +
"already published ($($attachmentMeta.Id))" "already published ($($attachmentMeta.Id))"
) )
@ -205,15 +205,17 @@ function New-Attachment
$response = ($rawResponse.Content | ConvertFrom-JSON) $response = ($rawResponse.Content | ConvertFrom-JSON)
$result = $response.results[0]
$attachmentMeta | Add-Member ` $attachmentMeta | Add-Member `
-NotePropertyName 'Id' ` -NotePropertyName 'Id' `
-NotePropertyValue $response.id ` -NotePropertyValue $result.id `
-Force -Force
$attachmentMeta | Add-Member ` $attachmentMeta | Add-Member `
-NotePropertyName 'Version' ` -NotePropertyName 'Version' `
-NotePropertyValue ( -NotePropertyValue (
$response.version.number $result.version.number
) ` ) `
-Force -Force
@ -246,6 +248,282 @@ function New-Attachment
} }
} }
function Update-Attachment
{
<#
.SYNOPSIS
Add a new attachment
.DESCRIPTION
.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
Add-ConfluencePage `
-Host 'confluence.contoso.com' `
-Space 'TIARA' `
-Title 'Testitest' `
-Content @{}
#>
Param(
# 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]$Name,
# attachments manifest
[Parameter(Mandatory, ValueFromPipeline)]
[PSCustomObject[]]$Manifest,
# attachments manifest index, mandatory for ancestor lookup
[Parameter(Mandatory)] [Collections.Hashtable]$Index,
# pages manifest
[Parameter(Mandatory)]
[PSCustomObject[]]$PagesManifest,
# pages manifest index, mandatory for container page lookup
[Parameter(Mandatory)] [Collections.Hashtable]$PagesIndex,
# flag on whether to fail hard, or just continue
[Parameter()] [Switch]$Strict,
# flag on whether to force update of attachment, regardless of content
# changes
[Parameter()] [Switch]$Force
)
Begin
{
$pat = Get-PersonalAccessToken $Host
}
Process
{
If ($Name -And $Manifest[$Index.$Name])
{
$Manifest = @(
$Manifest[$Index.$Name]
)
}
ForEach($attachmentMeta in $Manifest)
{
If ($Name -And $attachmentMeta.Name -ne $Name) {continue}
$containerPageMeta = $PagesManifest[
$PagesIndex."$($attachmentMeta.ContainerPageTitle)"
]
If (-Not $containerPageMeta)
{
throw (
"Get-AttachmentMeta: ``$($attachmentMeta.Name)``: " +
"unable to lookup metadata for container page " +
"title ``$($attachmentMeta.ContainerPageTitle)``." +
"This is fatal."
)
}
ElseIf (-Not $containerPageMeta.Id)
{
$errMsg = (
"Get-AttachmentMeta: ``$($attachmentMeta.Name)``: " +
"container page titled" +
"``$($attachmentMeta.ContainerPageTitle)`` " +
"has no id, which means that the page has " +
"(presumably) not yet been published."
)
If ($Strict) {throw $errMsg}
Write-Host "$errMsg Continuing nonetheless..."
$attachmentMeta
continue
}
ElseIf (-Not $attachmentMeta.Ref)
{
$errMsg = (
"``$($attachmentMeta.Name)``: no reference to local " +
'content for attachment .'
)
If ($Strict) {throw $errMsg}
Write-Host $errMsg
# not outputting the metadata, since it's invalid anyway
continue
}
ElseIf (-Not $attachmentMeta.Id)
{
$errMsg = (
"Update-Attachment: ``$($attachmentMeta.Name)``: unknown " +
"attachment id."
)
If ($Strict) {throw $errMsg}
Write-Host "$errMsg Skipping."
$attachmentMeta
continue
}
ElseIf (-Not $attachmentMeta.Version)
{
Write-Host = (
"New-Attachment: ``$($attachmentMeta.Name)``: skipping, " +
"unknown (current) version"
)
}
Else
{
Try
{
$rawContent = [IO.File]::ReadAllBytes($attachmentMeta.Ref)
$content = [Text.Encoding]::GetEncoding(
'ISO-8859-1'
).GetString($rawContent)
}
Catch
{
$errMsg = "``New-Attachment: $($attachmentMeta.Name)``: $_"
If ($Strict) {throw $errMsg}
Write-Host $errMsg
continue
}
$version = [Int]$attachmentMeta.Version + 1
$contentHash = (Get-StringHash $content).Hash
If (
$attachmentMeta.Hash -And
$attachmentMeta.Hash -eq $contentHash -And
-Not $Force
)
{
Write-Debug (
"Update-Attachment: ``$($attachmentMeta.Name)``: " +
"skipping, no content changes"
)
$attachmentMeta
continue
}
Else
{
Write-Host (
"Update-Attachment: ``$($attachmentMeta.Name)``: " +
"updating"
)
}
$boundary = [Guid]::NewGuid().ToString()
$LF = "`r`n";
$transportBody = (
"--$boundary",
(
"Content-Disposition: form-data; name=`"file`"; " +
"filename=`"$($attachmentMeta.Name)`""
),
"Content-Type: $($attachmentMeta.MimeType)$LF",
$content,
"--$boundary--$LF"
) -join $LF
$uri = (
"https://${Host}/rest/api/content/" +
"$($containerPageMeta.Id)/child/attachment/" +
"$($attachmentMeta.Id)/data"
)
Try
{
Invoke-WebRequest `
-Uri $uri `
-Method 'Post' `
-Headers @{
'Authorization' = "Bearer $pat"
'X-Atlassian-Token' = 'nocheck'
} `
-ContentType (
"multipart/form-data; boundary=`"$boundary`""
) `
-Body $transportBody | Out-Null
}
Catch
{
$errMsg = "skipping ``$($attachmentMeta.Name)``: $($_)"
If ($Strict)
{
$_
throw $errMsg
}
Write-Host $errMsg
continue
}
# response isn't needed since no field will be updated by the
# Confluence instance itself
$attachmentMeta | Add-Member `
-NotePropertyName 'Version' `
-NotePropertyValue $version `
-Force
$attachmentMeta | Add-Member `
-NotePropertyName 'Hash' `
-NotePropertyValue $contentHash `
-Force
If (
($Title -And $attachmentMeta.Title -eq $Name) -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
,@($attachmentMeta)
break
}
Else
{
$attachmentMeta
}
}
}
}
}
function Publish-Attachment function Publish-Attachment
{ {
Param( Param(
@ -273,13 +551,23 @@ function Publish-Attachment
Process Process
{ {
$result = New-Attachment ` $result = Update-Attachment `
-Host $Host ` -Host $Host `
-Space $Space ` -Space $Space `
-Manifest $Manifest ` -Manifest $Manifest `
-Index $Index ` -Index $Index `
-PagesManifest $PagesManifest ` -PagesManifest $PagesManifest `
-PagesIndex $PagesIndex ` -PagesIndex $PagesIndex `
-Strict:$Strict `
-Force:$Force
$result = New-Attachment `
-Host $Host `
-Space $Space `
-Manifest $manifest `
-Index $Index `
-PagesManifest $PagesManifest `
-PagesIndex $PagesIndex `
-Strict:$Strict -Strict:$Strict
} }

View file

@ -78,7 +78,6 @@ function New-Page
ElseIf ($pageMeta.Id) ElseIf ($pageMeta.Id)
{ {
Write-Debug ( Write-Debug (
"New-Page: ``$($pageMeta.Title)``: skipping, already " + "New-Page: ``$($pageMeta.Title)``: skipping, already " +
"published ($($pageMeta.Id))" "published ($($pageMeta.Id))"
@ -298,11 +297,13 @@ function Update-Page
ElseIf (-Not $pageMeta.Id) ElseIf (-Not $pageMeta.Id)
{ {
$errMsg = "``$($pageMeta.Title)``: unknown page id" $errMsg = (
"Update-Page: ``$($pageMeta.Title)``: unknown page id."
)
If ($Strict) {throw $errMsg} If ($Strict) {throw $errMsg}
Write-Host $errMsg Write-Host "$errMsg Skipping."
$pageMeta $pageMeta
@ -311,7 +312,10 @@ function Update-Page
ElseIf (-Not $pageMeta.Version) ElseIf (-Not $pageMeta.Version)
{ {
Write-Host "``$($pageMeta.Title)``: unknown (current) version" Write-Host (
"Update-Page: ``$($pageMeta.Title)``: unknown (current) " +
"version. Skipping."
)
$pageMeta $pageMeta
@ -336,7 +340,7 @@ function Update-Page
continue continue
} }
$version = [Int]($pageMeta.Version) + 1 $version = [Int]$pageMeta.Version + 1
$contentHash = (Get-StringHash $content).Hash $contentHash = (Get-StringHash $content).Hash
@ -358,7 +362,9 @@ function Update-Page
Else Else
{ {
Write-Host "``$($pageMeta.Title)``: updating" Write-Host (
"Update-Page: ``$($pageMeta.Title)``: updating"
)
} }
# status needs to be set as to restore the page, if it is # status needs to be set as to restore the page, if it is