Skip to content

Commit 9cde833

Browse files
committed
fix updating attachments when confluence returns a short response object
1 parent f1e861c commit 9cde833

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

Taskfile.yml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1-
version: '2'
1+
version: '3'
22

33
vars:
4+
version: 7.13.0
45
pwd:
56
sh: pwd
67

78
tasks:
9+
volume:
10+
cmds:
11+
- mkdir -p docker/{{.version}}
12+
13+
network:
14+
desc: create docker network
15+
cmds:
16+
- docker network create confluence || true
17+
18+
postgres:
19+
desc: start postgres for confluence
20+
deps: [network, volume]
21+
cmds:
22+
- docker run -it -p 5432:5432
23+
--name confluence-postgres
24+
--network confluence
25+
-v {{.pwd}}/docker/{{.version}}/postgres:/var/lib/postgresql/data
26+
-e POSTGRES_PASSWORD=confluence
27+
-e POSTGRES_DB=confluence
28+
-e POSTGRES_USER=confluence
29+
postgres
30+
831
confluence:
32+
desc: start confluence server
33+
deps: [network, volume]
934
cmds:
10-
- docker run -v {{ .pwd }}/docker:/var/atlassian/application-data/confluence
35+
- docker run -v {{ .pwd }}/docker/{{.version}}/confluence:/var/atlassian/application-data/confluence
1136
--name="confluence"
37+
--network confluence
1238
-p 8090:8090
1339
-p 8091:8091
14-
atlassian/confluence-server
40+
atlassian/confluence-server:{{.version}}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Flags struct {
3737
}
3838

3939
const (
40-
version = "6.6"
40+
version = "6.7"
4141
usage = `mark - a tool for updating Atlassian Confluence pages from markdown.
4242
4343
Docs: https://github.com/kovetskiy/mark

pkg/confluence/api.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package confluence
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
@@ -142,7 +143,6 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
142143
request, err := api.rest.Res(
143144
"space/"+space, &SpaceInfo{},
144145
).Get(payload)
145-
146146
if err != nil {
147147
return nil, err
148148
}
@@ -154,7 +154,11 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
154154
return &request.Response.(*SpaceInfo).Homepage, nil
155155
}
156156

157-
func (api *API) FindPage(space string, title string, pageType string) (*PageInfo, error) {
157+
func (api *API) FindPage(
158+
space string,
159+
title string,
160+
pageType string,
161+
) (*PageInfo, error) {
158162
result := struct {
159163
Results []PageInfo `json:"results"`
160164
}{}
@@ -248,6 +252,10 @@ func (api *API) CreateAttachment(
248252
return info, nil
249253
}
250254

255+
// UpdateAttachment uploads a new version of the same attachment if the
256+
// checksums differs from the previous one.
257+
// It also handles a case where Confluence returns sort of "short" variant of
258+
// the response instead of an extended one.
251259
func (api *API) UpdateAttachment(
252260
pageID string,
253261
attachID string,
@@ -262,13 +270,15 @@ func (api *API) UpdateAttachment(
262270
return AttachmentInfo{}, err
263271
}
264272

265-
var result struct {
273+
var extendedResponse struct {
266274
Links struct {
267275
Context string `json:"context"`
268276
} `json:"_links"`
269277
Results []AttachmentInfo `json:"results"`
270278
}
271279

280+
var result json.RawMessage
281+
272282
resource := api.rest.Res(
273283
"content/"+pageID+"/child/attachment/"+attachID+"/data", &result,
274284
)
@@ -288,24 +298,40 @@ func (api *API) UpdateAttachment(
288298
return info, newErrorStatusNotOK(request)
289299
}
290300

291-
if len(result.Results) == 0 {
292-
return info, errors.New(
293-
"Confluence REST API for creating attachments returned " +
294-
"0 json objects, expected at least 1",
301+
err = json.Unmarshal(result, &extendedResponse)
302+
if err != nil {
303+
return info, karma.Format(
304+
err,
305+
"unable to unmarshal JSON response as full response format: %s",
306+
string(result),
295307
)
296308
}
297309

298-
for i, info := range result.Results {
299-
if info.Links.Context == "" {
300-
info.Links.Context = result.Links.Context
310+
if len(extendedResponse.Results) > 0 {
311+
for i, info := range extendedResponse.Results {
312+
if info.Links.Context == "" {
313+
info.Links.Context = extendedResponse.Links.Context
314+
}
315+
316+
extendedResponse.Results[i] = info
301317
}
302318

303-
result.Results[i] = info
319+
info = extendedResponse.Results[0]
320+
321+
return info, nil
304322
}
305323

306-
info = result.Results[0]
324+
var shortResponse AttachmentInfo
325+
err = json.Unmarshal(result, &shortResponse)
326+
if err != nil {
327+
return info, karma.Format(
328+
err,
329+
"unable to unmarshal JSON response as short response format: %s",
330+
string(result),
331+
)
332+
}
307333

308-
return info, nil
334+
return shortResponse, nil
309335
}
310336

311337
func getAttachmentPayload(name, comment, path string) (*form, error) {

0 commit comments

Comments
 (0)