diff --git a/Taskfile.yml b/Taskfile.yml index 61a3f5c..2b50051 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,14 +1,40 @@ -version: '2' +version: '3' vars: + version: 7.13.0 pwd: sh: pwd tasks: - confluence: + volume: 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" + --network confluence -p 8090:8090 -p 8091:8091 - atlassian/confluence-server + atlassian/confluence-server:{{.version}} diff --git a/main.go b/main.go index a1e39b9..fbd67b8 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ type Flags struct { } const ( - version = "6.6" + version = "6.7" usage = `mark - a tool for updating Atlassian Confluence pages from markdown. Docs: https://github.com/kovetskiy/mark diff --git a/pkg/confluence/api.go b/pkg/confluence/api.go index 8386d70..3dd7a1c 100644 --- a/pkg/confluence/api.go +++ b/pkg/confluence/api.go @@ -2,6 +2,7 @@ package confluence import ( "bytes" + "encoding/json" "errors" "fmt" "io" @@ -142,7 +143,6 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) { request, err := api.rest.Res( "space/"+space, &SpaceInfo{}, ).Get(payload) - if err != nil { return nil, err } @@ -154,7 +154,11 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) { 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 { Results []PageInfo `json:"results"` }{} @@ -248,6 +252,10 @@ func (api *API) CreateAttachment( 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( pageID string, attachID string, @@ -262,13 +270,15 @@ func (api *API) UpdateAttachment( return AttachmentInfo{}, err } - var result struct { + var extendedResponse struct { Links struct { Context string `json:"context"` } `json:"_links"` Results []AttachmentInfo `json:"results"` } + var result json.RawMessage + resource := api.rest.Res( "content/"+pageID+"/child/attachment/"+attachID+"/data", &result, ) @@ -288,24 +298,40 @@ func (api *API) UpdateAttachment( return info, newErrorStatusNotOK(request) } - if len(result.Results) == 0 { - return info, errors.New( - "Confluence REST API for creating attachments returned " + - "0 json objects, expected at least 1", + err = json.Unmarshal(result, &extendedResponse) + if err != nil { + return info, karma.Format( + err, + "unable to unmarshal JSON response as full response format: %s", + string(result), ) } - for i, info := range result.Results { - if info.Links.Context == "" { - info.Links.Context = result.Links.Context + if len(extendedResponse.Results) > 0 { + for i, info := range extendedResponse.Results { + 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) {