fix updating attachments when confluence returns a short response object

This commit is contained in:
Egor Kovetskiy 2021-12-09 11:46:57 +06:00
parent f1e861c2fa
commit 9cde833a18
3 changed files with 70 additions and 18 deletions

View File

@ -1,14 +1,40 @@
version: '2' version: '3'
vars: vars:
version: 7.13.0
pwd: pwd:
sh: pwd sh: pwd
tasks: tasks:
confluence: volume:
cmds: cmds:
- docker run -v {{ .pwd }}/docker:/var/atlassian/application-data/confluence - mkdir -p docker/{{.version}}
network:
desc: create docker network
cmds:
- docker network create confluence || true
postgres:
desc: start postgres for confluence
deps: [network, volume]
cmds:
- docker run -it -p 5432:5432
--name confluence-postgres
--network confluence
-v {{.pwd}}/docker/{{.version}}/postgres:/var/lib/postgresql/data
-e POSTGRES_PASSWORD=confluence
-e POSTGRES_DB=confluence
-e POSTGRES_USER=confluence
postgres
confluence:
desc: start confluence server
deps: [network, volume]
cmds:
- docker run -v {{ .pwd }}/docker/{{.version}}/confluence:/var/atlassian/application-data/confluence
--name="confluence" --name="confluence"
--network confluence
-p 8090:8090 -p 8090:8090
-p 8091:8091 -p 8091:8091
atlassian/confluence-server atlassian/confluence-server:{{.version}}

View File

@ -37,7 +37,7 @@ type Flags struct {
} }
const ( const (
version = "6.6" version = "6.7"
usage = `mark - a tool for updating Atlassian Confluence pages from markdown. usage = `mark - a tool for updating Atlassian Confluence pages from markdown.
Docs: https://github.com/kovetskiy/mark Docs: https://github.com/kovetskiy/mark

View File

@ -2,6 +2,7 @@ package confluence
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -142,7 +143,6 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
request, err := api.rest.Res( request, err := api.rest.Res(
"space/"+space, &SpaceInfo{}, "space/"+space, &SpaceInfo{},
).Get(payload) ).Get(payload)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -154,7 +154,11 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
return &request.Response.(*SpaceInfo).Homepage, nil return &request.Response.(*SpaceInfo).Homepage, nil
} }
func (api *API) FindPage(space string, title string, pageType string) (*PageInfo, error) { func (api *API) FindPage(
space string,
title string,
pageType string,
) (*PageInfo, error) {
result := struct { result := struct {
Results []PageInfo `json:"results"` Results []PageInfo `json:"results"`
}{} }{}
@ -248,6 +252,10 @@ func (api *API) CreateAttachment(
return info, nil return info, nil
} }
// UpdateAttachment uploads a new version of the same attachment if the
// checksums differs from the previous one.
// It also handles a case where Confluence returns sort of "short" variant of
// the response instead of an extended one.
func (api *API) UpdateAttachment( func (api *API) UpdateAttachment(
pageID string, pageID string,
attachID string, attachID string,
@ -262,13 +270,15 @@ func (api *API) UpdateAttachment(
return AttachmentInfo{}, err return AttachmentInfo{}, err
} }
var result struct { var extendedResponse struct {
Links struct { Links struct {
Context string `json:"context"` Context string `json:"context"`
} `json:"_links"` } `json:"_links"`
Results []AttachmentInfo `json:"results"` Results []AttachmentInfo `json:"results"`
} }
var result json.RawMessage
resource := api.rest.Res( resource := api.rest.Res(
"content/"+pageID+"/child/attachment/"+attachID+"/data", &result, "content/"+pageID+"/child/attachment/"+attachID+"/data", &result,
) )
@ -288,24 +298,40 @@ func (api *API) UpdateAttachment(
return info, newErrorStatusNotOK(request) return info, newErrorStatusNotOK(request)
} }
if len(result.Results) == 0 { err = json.Unmarshal(result, &extendedResponse)
return info, errors.New( if err != nil {
"Confluence REST API for creating attachments returned " + return info, karma.Format(
"0 json objects, expected at least 1", err,
"unable to unmarshal JSON response as full response format: %s",
string(result),
) )
} }
for i, info := range result.Results { if len(extendedResponse.Results) > 0 {
if info.Links.Context == "" { for i, info := range extendedResponse.Results {
info.Links.Context = result.Links.Context if info.Links.Context == "" {
info.Links.Context = extendedResponse.Links.Context
}
extendedResponse.Results[i] = info
} }
result.Results[i] = info info = extendedResponse.Results[0]
return info, nil
} }
info = result.Results[0] var shortResponse AttachmentInfo
err = json.Unmarshal(result, &shortResponse)
if err != nil {
return info, karma.Format(
err,
"unable to unmarshal JSON response as short response format: %s",
string(result),
)
}
return info, nil return shortResponse, nil
} }
func getAttachmentPayload(name, comment, path string) (*form, error) { func getAttachmentPayload(name, comment, path string) (*form, error) {