-
Notifications
You must be signed in to change notification settings - Fork 34
/
content.go
304 lines (231 loc) · 8.27 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
package confluence
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
"net/url"
"strconv"
"strings"
)
type ContentService struct {
client *Client
Attachment *ContentAttachmentService
ChildrenDescendant *ContentChildrenDescendantService
Comment *ContentCommentService
Permission *ContentPermissionService
Label *ContentLabelService
Property *ContentPropertyService
}
// Gets returns all content in a Confluence instance.
func (c *ContentService) Gets(ctx context.Context, options *model.GetContentOptionsScheme, startAt, maxResults int) (
result *model.ContentPageScheme, response *ResponseScheme, err error) {
query := url.Values{}
query.Add("start", strconv.Itoa(startAt))
query.Add("limit", strconv.Itoa(maxResults))
if options != nil {
if options.ContextType != "" {
query.Add("type", options.ContextType)
}
if options.SpaceKey != "" {
query.Add("spaceKey", options.SpaceKey)
}
if options.Title != "" {
query.Add("title", options.Title)
}
if options.Trigger != "" {
query.Add("trigger", options.Trigger)
}
if options.OrderBy != "" {
query.Add("orderby", options.OrderBy)
}
if !options.PostingDay.IsZero() {
query.Add("postingDay", options.PostingDay.Format("2006-01-02"))
}
if len(options.Status) != 0 {
query.Add("status", strings.Join(options.Status, ","))
}
if len(options.Expand) != 0 {
query.Add("expand", strings.Join(options.Expand, ","))
}
}
var endpoint = fmt.Sprintf("/wiki/rest/api/content?%v", query.Encode())
request, err := c.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}
// Create creates a new piece of content or publishes an existing draft
// To publish a draft, add the id and status properties to the body of the request.
// Set the id to the ID of the draft and set the status to 'current'.
// When the request is sent, a new piece of content will be created and the metadata from the draft will be transferred into it.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#create-content
func (c *ContentService) Create(ctx context.Context, payload *model.ContentScheme) (result *model.ContentScheme,
response *ResponseScheme, err error) {
payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}
var endpoint = "/wiki/rest/api/content"
request, err := c.client.newRequest(ctx, http.MethodPost, endpoint, payloadAsReader)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}
// Search returns the list of content that matches a Confluence Query Language (CQL) query
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#search-contents-by-cql
func (c *ContentService) Search(ctx context.Context, cql, cqlContext string, expand []string, cursor string, maxResults int) (
result *model.ContentPageScheme, response *ResponseScheme, err error) {
if cql == "" {
return nil, nil, model.ErrNoCQLError
}
query := url.Values{}
query.Add("limit", strconv.Itoa(maxResults))
query.Add("cql", cql)
if cursor != "" {
query.Add("cursor", cursor)
}
if cqlContext != "" {
query.Add("cqlcontext", cqlContext)
}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}
var endpoint = fmt.Sprintf("/wiki/rest/api/search?%v", query.Encode())
request, err := c.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}
// Get returns a single piece of content, like a page or a blog post.
// By default, the following objects are expanded: space, history, version.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#get-content
func (c *ContentService) Get(ctx context.Context, contentID string, expand []string, version int) (result *model.ContentScheme,
response *ResponseScheme, err error) {
if len(contentID) == 0 {
return nil, nil, model.ErrNoContentIDError
}
query := url.Values{}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}
if version != 0 {
query.Add("version", strconv.Itoa(version))
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("/wiki/rest/api/content/%v", contentID))
if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
request, err := c.client.newRequest(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}
// Update updates a piece of content.
// Use this method to update the title or body of a piece of content, change the status, change the parent page, and more.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#update-content
func (c *ContentService) Update(ctx context.Context, contentID string, payload *model.ContentScheme) (result *model.ContentScheme,
response *ResponseScheme, err error) {
if len(contentID) == 0 {
return nil, nil, model.ErrNoContentIDError
}
payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}
var endpoint = fmt.Sprintf("/wiki/rest/api/content/%v", contentID)
request, err := c.client.newRequest(ctx, http.MethodPut, endpoint, payloadAsReader)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}
// Delete moves a piece of content to the space's trash or purges it from the trash,
// depending on the content's type and status:
// If the content's type is page or blogpost and its status is current, it will be trashed.
// If the content's type is page or blogpost and its status is trashed, the content will be purged from the trash and deleted permanently.
// === Note, you must also set the status query parameter to trashed in your request. ===
// If the content's type is comment or attachment, it will be deleted permanently without being trashed.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#delete-content
func (c *ContentService) Delete(ctx context.Context, contentID, status string) (response *ResponseScheme, err error) {
if len(contentID) == 0 {
return nil, model.ErrNoContentIDError
}
query := url.Values{}
if status != "" {
query.Add("status", status)
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("/wiki/rest/api/content/%v", contentID))
if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
request, err := c.client.newRequest(ctx, http.MethodDelete, endpoint.String(), nil)
if err != nil {
return nil, err
}
response, err = c.client.Call(request, nil)
if err != nil {
return response, err
}
return
}
// History returns the most recent update for a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#get-content-history
func (c *ContentService) History(ctx context.Context, contentID string, expand []string) (result *model.ContentHistoryScheme,
response *ResponseScheme, err error) {
if len(contentID) == 0 {
return nil, nil, model.ErrNoContentIDError
}
query := url.Values{}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("/wiki/rest/api/content/%v/history", contentID))
if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
request, err := c.client.newRequest(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, nil, err
}
request.Header.Set("Accept", "application/json")
response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}
return
}