From 4fa55b8602ceddeca8246f8e3c227fe53a857083 Mon Sep 17 00:00:00 2001 From: "Rodweil, Theodor" Date: Mon, 14 Aug 2023 05:10:40 +0200 Subject: [PATCH] fix: attachment and page debug logs --- src/Attachment.psm1 | 296 +++++++++++++++++++++++++++++++++++++++++++- src/Page.psm1 | 18 ++- 2 files changed, 304 insertions(+), 10 deletions(-) diff --git a/src/Attachment.psm1 b/src/Attachment.psm1 index 468f1b4..a3f290d 100755 --- a/src/Attachment.psm1 +++ b/src/Attachment.psm1 @@ -116,7 +116,7 @@ function New-Attachment { Write-Debug ( - "New-Attachment: ``$($attacmentMeta.Name)``: skipping, " + + "New-Attachment: ``$($attachmentMeta.Name)``: skipping, " + "already published ($($attachmentMeta.Id))" ) @@ -205,15 +205,17 @@ function New-Attachment $response = ($rawResponse.Content | ConvertFrom-JSON) + $result = $response.results[0] + $attachmentMeta | Add-Member ` -NotePropertyName 'Id' ` - -NotePropertyValue $response.id ` + -NotePropertyValue $result.id ` -Force $attachmentMeta | Add-Member ` -NotePropertyName 'Version' ` -NotePropertyValue ( - $response.version.number + $result.version.number ) ` -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 { Param( @@ -273,13 +551,23 @@ function Publish-Attachment Process { - $result = New-Attachment ` + $result = Update-Attachment ` -Host $Host ` -Space $Space ` -Manifest $Manifest ` -Index $Index ` -PagesManifest $PagesManifest ` -PagesIndex $PagesIndex ` + -Strict:$Strict ` + -Force:$Force + + $result = New-Attachment ` + -Host $Host ` + -Space $Space ` + -Manifest $manifest ` + -Index $Index ` + -PagesManifest $PagesManifest ` + -PagesIndex $PagesIndex ` -Strict:$Strict } diff --git a/src/Page.psm1 b/src/Page.psm1 index 0df3019..7a0d54e 100755 --- a/src/Page.psm1 +++ b/src/Page.psm1 @@ -78,7 +78,6 @@ function New-Page ElseIf ($pageMeta.Id) { - Write-Debug ( "New-Page: ``$($pageMeta.Title)``: skipping, already " + "published ($($pageMeta.Id))" @@ -298,11 +297,13 @@ function Update-Page ElseIf (-Not $pageMeta.Id) { - $errMsg = "``$($pageMeta.Title)``: unknown page id" + $errMsg = ( + "Update-Page: ``$($pageMeta.Title)``: unknown page id." + ) If ($Strict) {throw $errMsg} - Write-Host $errMsg + Write-Host "$errMsg Skipping." $pageMeta @@ -311,7 +312,10 @@ function Update-Page ElseIf (-Not $pageMeta.Version) { - Write-Host "``$($pageMeta.Title)``: unknown (current) version" + Write-Host ( + "Update-Page: ``$($pageMeta.Title)``: unknown (current) " + + "version. Skipping." + ) $pageMeta @@ -336,7 +340,7 @@ function Update-Page continue } - $version = [Int]($pageMeta.Version) + 1 + $version = [Int]$pageMeta.Version + 1 $contentHash = (Get-StringHash $content).Hash @@ -358,7 +362,9 @@ function Update-Page 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