forked from Virtomize/confluence-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
420 lines (368 loc) · 11.4 KB
/
content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package goconfluence
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strconv"
"strings"
)
// Results array
type Results struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
Content Content `json:"content"`
Excerpt string `json:"excerpt,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
}
// Content specifies content properties
type Content struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Status string `json:"status,omitempty"`
Title string `json:"title"`
Ancestors []Ancestor `json:"ancestors,omitempty"`
Body Body `json:"body"`
Version *Version `json:"version,omitempty"`
Space Space `json:"space"`
History *History `json:"history,omitempty"`
Links *Links `json:"_links,omitempty"`
}
// Links contains link information
type Links struct {
Base string `json:"base"`
TinyUI string `json:"tinyui"`
}
// Ancestor defines ancestors to create sub pages
type Ancestor struct {
ID string `json:"id"`
}
// Body holds the storage information
type Body struct {
Storage Storage `json:"storage"`
View *Storage `json:"view,omitempty"`
}
// BodyExportView holds the export_view information
type BodyExportView struct {
ExportView *Storage `json:"export_view"`
View *Storage `json:"view,omitempty"`
}
// Storage defines the storage information
type Storage struct {
Value string `json:"value"`
Representation string `json:"representation"`
}
// Version defines the content version number
// the version number is used for updating content
type Version struct {
Number int `json:"number"`
MinorEdit bool `json:"minorEdit"`
Message string `json:"message,omitempty"`
By *User `json:"by,omitempty"`
}
// Space holds the Space information of a Content Page
type Space struct {
ID int `json:"id,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
}
// getContentIDEndpoint creates the correct api endpoint by given id
func (a *API) getContentIDEndpoint(id string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id)
}
// getContentEndpoint creates the correct api endpoint
func (a *API) getContentEndpoint() (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/")
}
// getContentChildEndpoint creates the correct api endpoint by given id and type
func (a *API) getContentChildEndpoint(id string, t string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id + "/child/" + t)
}
// getContentGenericEndpoint creates the correct api endpoint by given id and type
func (a *API) getContentGenericEndpoint(id string, t string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id + "/" + t)
}
// ContentQuery defines the query parameters
// used for content related searching
// Query parameter values https://developer.atlassian.com/cloud/confluence/rest/#api-content-get
type ContentQuery struct {
Expand []string
Limit int // page limit
OrderBy string // fieldpath asc/desc e.g: "history.createdDate desc"
PostingDay string // required for blogpost type Format: yyyy-mm-dd
SpaceKey string
Start int // page start
Status string // current, trashed, draft, any
Title string // required for page
Trigger string // viewed
Type string // page, blogpost
Version int //version number when not lastest
}
// GetContentByID querys content by id
func (a *API) GetContentByID(id string, query ContentQuery) (*Content, error) {
ep, err := a.getContentIDEndpoint(id)
if err != nil {
return nil, err
}
ep.RawQuery = addContentQueryParams(query).Encode()
return a.SendContentRequest(ep, "GET", nil)
}
// ContentSearch results
type ContentSearch struct {
Results []Content `json:"results"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
}
// GetContent querys content using a query parameters
func (a *API) GetContent(query ContentQuery) (*ContentSearch, error) {
ep, err := a.getContentEndpoint()
if err != nil {
return nil, err
}
ep.RawQuery = addContentQueryParams(query).Encode()
req, err := http.NewRequest("GET", ep.String(), nil)
if err != nil {
return nil, err
}
res, err := a.Request(req)
if err != nil {
return nil, err
}
var search ContentSearch
err = json.Unmarshal(res, &search)
if err != nil {
return nil, err
}
return &search, nil
}
// GetChildPages returns a content list of child page objects
func (a *API) GetChildPages(id string) (*Search, error) {
var (
results []Results
searchResult Search
)
ep, err := a.getContentChildEndpoint(id, "page")
if err != nil {
return nil, err
}
query := ContentQuery{
Start: 0,
Limit: 25,
}
searchResult.Start = 0
for {
ep.RawQuery = addContentQueryParams(query).Encode()
s, err := a.SendSearchRequest(ep, "GET")
if err != nil {
return nil, err
}
results = append(results, s.Results...)
if len(s.Results) < query.Limit {
break
}
query.Start += 25
}
searchResult.Limit = len(results)
searchResult.Size = len(results)
searchResult.Results = results
return &searchResult, nil
}
// GetComments returns a list of comments belonging to id
func (a *API) GetComments(id string) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "comment")
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}
// GetAttachments returns a list of attachments belonging to id
func (a *API) GetAttachments(id string) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "attachment")
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}
// History contains object history information
type History struct {
LastUpdated LastUpdated `json:"lastUpdated"`
Latest bool `json:"latest"`
CreatedBy User `json:"createdBy"`
CreatedDate string `json:"createdDate"`
}
// LastUpdated contains information about the last update
type LastUpdated struct {
By User `json:"by"`
When string `json:"when"`
FriendlyWhen string `json:"friendlyWhen"`
Message string `json:"message"`
Number int `json:"number"`
MinorEdit bool `json:"minorEdit"`
SyncRev string `json:"syncRev"`
ConfRev string `json:"confRev"`
}
// GetHistory returns history information
func (a *API) GetHistory(id string) (*History, error) {
ep, err := a.getContentGenericEndpoint(id, "history")
if err != nil {
return nil, err
}
return a.SendHistoryRequest(ep, "GET")
}
// Labels is the label containter type
type Labels struct {
Labels []Label `json:"results"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
}
// Label contains label information
type Label struct {
Prefix string `json:"prefix"`
Name string `json:"name"`
ID string `json:"id,omitempty"`
Label string `json:"label,omitempty"`
}
// GetLabels returns a list of labels attachted to a content object
func (a *API) GetLabels(id string) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label")
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "GET", nil)
}
// AddLabels returns adds labels
func (a *API) AddLabels(id string, labels *[]Label) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label")
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "POST", labels)
}
// DeleteLabel removes a label by name from content identified by id
func (a *API) DeleteLabel(id string, name string) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label/"+name)
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "DELETE", nil)
}
// Watchers is a list of Watcher
type Watchers struct {
Watchers []Watcher `json:"results"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
}
// Watcher contains information about watching users of a page
type Watcher struct {
Type string `json:"type"`
Watcher User `json:"watcher"`
ContentID int `json:"contentId"`
}
// GetWatchers returns a list of watchers
func (a *API) GetWatchers(id string) (*Watchers, error) {
ep, err := a.getContentGenericEndpoint(id, "notification/child-created")
if err != nil {
return nil, err
}
return a.SendWatcherRequest(ep, "GET")
}
// CreateContent creates content
func (a *API) CreateContent(c *Content) (*Content, error) {
ep, err := a.getContentEndpoint()
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "POST", c)
}
// UpdateContent updates content
func (a *API) UpdateContent(c *Content) (*Content, error) {
ep, err := a.getContentIDEndpoint(c.ID)
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "PUT", c)
}
// UploadAttachment uploaded the given reader as an attachment to the
// page with the given id. The existing attachment won't be updated with
// a new version number
func (a *API) UploadAttachment(id string, attachmentName string, attachment io.Reader) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "attachment")
if err != nil {
return nil, err
}
return a.SendContentAttachmentRequest(ep, attachmentName, attachment, map[string]string{})
}
// UpdateAttachment update the attachment with an attachmentID on a page with an id to a new version
func (a *API) UpdateAttachment(id string, attachmentName string, attachmentID string, attachment io.Reader) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "attachment/"+attachmentID+"/data")
if err != nil {
return nil, err
}
return a.SendContentAttachmentRequest(ep, attachmentName, attachment, map[string]string{})
}
// DelContent deletes content by id
func (a *API) DelContent(id string) (*Content, error) {
ep, err := a.getContentIDEndpoint(id)
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "DELETE", nil)
}
// ContentVersionResult contains the version results
type ContentVersionResult struct {
Result []Version `json:"results"`
}
// GetContentVersion gets all versions of this content
func (a *API) GetContentVersion(id string) (*ContentVersionResult, error) {
ep, err := a.getContentGenericEndpoint(id, "version")
if err != nil {
return nil, err
}
return a.SendContentVersionRequest(ep, "GET")
}
// addContentQueryParams adds the defined query parameters
func addContentQueryParams(query ContentQuery) *url.Values {
data := url.Values{}
if len(query.Expand) != 0 {
data.Set("expand", strings.Join(query.Expand, ","))
}
//get specific version
if query.Version != 0 {
data.Set("version", strconv.Itoa(query.Version))
}
if query.Limit != 0 {
data.Set("limit", strconv.Itoa(query.Limit))
}
if query.OrderBy != "" {
data.Set("orderby", query.OrderBy)
}
if query.PostingDay != "" {
data.Set("postingDay", query.PostingDay)
}
if query.SpaceKey != "" {
data.Set("spaceKey", query.SpaceKey)
}
if query.Start != 0 {
data.Set("start", strconv.Itoa(query.Start))
}
if query.Status != "" {
data.Set("status", query.Status)
}
if query.Title != "" {
data.Set("title", query.Title)
}
if query.Trigger != "" {
data.Set("trigger", query.Trigger)
}
if query.Type != "" {
data.Set("type", query.Type)
}
return &data
}