@@ -2,6 +2,7 @@ package confluence
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
7
8
"io"
@@ -142,7 +143,6 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
142
143
request , err := api .rest .Res (
143
144
"space/" + space , & SpaceInfo {},
144
145
).Get (payload )
145
-
146
146
if err != nil {
147
147
return nil , err
148
148
}
@@ -154,7 +154,11 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
154
154
return & request .Response .(* SpaceInfo ).Homepage , nil
155
155
}
156
156
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 ) {
158
162
result := struct {
159
163
Results []PageInfo `json:"results"`
160
164
}{}
@@ -248,6 +252,10 @@ func (api *API) CreateAttachment(
248
252
return info , nil
249
253
}
250
254
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.
251
259
func (api * API ) UpdateAttachment (
252
260
pageID string ,
253
261
attachID string ,
@@ -262,13 +270,15 @@ func (api *API) UpdateAttachment(
262
270
return AttachmentInfo {}, err
263
271
}
264
272
265
- var result struct {
273
+ var extendedResponse struct {
266
274
Links struct {
267
275
Context string `json:"context"`
268
276
} `json:"_links"`
269
277
Results []AttachmentInfo `json:"results"`
270
278
}
271
279
280
+ var result json.RawMessage
281
+
272
282
resource := api .rest .Res (
273
283
"content/" + pageID + "/child/attachment/" + attachID + "/data" , & result ,
274
284
)
@@ -288,24 +298,40 @@ func (api *API) UpdateAttachment(
288
298
return info , newErrorStatusNotOK (request )
289
299
}
290
300
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 ),
295
307
)
296
308
}
297
309
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
301
317
}
302
318
303
- result .Results [i ] = info
319
+ info = extendedResponse .Results [0 ]
320
+
321
+ return info , nil
304
322
}
305
323
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
+ }
307
333
308
- return info , nil
334
+ return shortResponse , nil
309
335
}
310
336
311
337
func getAttachmentPayload (name , comment , path string ) (* form , error ) {
0 commit comments