forked from wiquan/go-confluence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
191 lines (170 loc) · 6.12 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
package confluence
import (
"encoding/json"
"strings"
"github.com/google/go-querystring/query"
log "github.com/sirupsen/logrus"
)
func (client *Client) labelEndpoint(contentID string) string {
return "/rest/api/content/" + contentID + "/label"
}
// GetContent Returns all content in a Confluence instance.
// https://developer.atlassian.com/cloud/confluence/rest/#api-content-get
func (client *Client) GetContent(qp *GetContentQueryParameters) ([]Content, error) {
qp.ExpandString = strings.Join(qp.Expand, ",")
v, _ := query.Values(qp)
queryParams := v.Encode()
body, err := client.request("GET", "/rest/api/content", queryParams, "")
if err != nil {
return nil, err
}
var contentResponse ContentResponse
err = json.Unmarshal(body, &contentResponse)
if err != nil {
log.Error("Unable to unmarshal ContentResponse. Received: '", string(body), "'")
}
return contentResponse.Results, err
}
// GetContentQueryParameters query parameters for GetContent
type GetContentQueryParameters struct {
QueryParameters
Expand []string `url:"-"`
ExpandString string `url:"expand,omitempty"`
Limit int `url:"limit,omitempty"`
Orderby string `url:"orderby,omitempty"`
PostingDay string `url:"postingDay,omitempty"`
Spacekey string `url:"spaceKey,omitempty"`
Start int `url:"start,omitempty"`
Title string `url:"title,omitempty"`
Trigger string `url:"trigger,omitempty"`
Type string `url:"type,omitempty"`
}
// CreateContent creates a new piece of content or publishes an existing draft.
// https://developer.atlassian.com/cloud/confluence/rest/#api-content-post
func (client *Client) CreateContent(bp *CreateContentBodyParameters, qp *QueryParameters) (Content, error) {
var res Content
var queryParams string
if qp != nil {
v, _ := query.Values(qp)
queryParams = v.Encode()
}
byteString, err := json.Marshal(bp)
if err != nil {
log.Error("Unable to marshal body. Received: '", err, "'")
}
body, err := client.request("POST", "/rest/api/content", queryParams, string(byteString))
if err != nil {
return res, err
}
err = json.Unmarshal(body, &res)
if err != nil {
log.Error(body)
log.Error(err)
log.Error("Unable to unmarshal CreateContentResponse. Received: '", string(body), "'")
}
return res, err
}
// UpdateContent 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.
// https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-put
func (client *Client) UpdateContent(content *Content, qp *QueryParameters) (Content, error) {
var queryParams string
if qp != nil {
v, _ := query.Values(qp)
queryParams = v.Encode()
}
byteString, err := json.Marshal(content)
if err != nil {
log.Error("Unable to marshal body. Received: '", err, "'")
}
body, err := client.request("PUT", "/rest/api/content/"+content.ID, queryParams, string(byteString))
if err != nil {
return *content, err
}
err = json.Unmarshal(body, &content)
if err != nil {
log.Error(body)
log.Error(err)
log.Error("Unable to unmarshal UpdateContent response. Received: '", string(body), "'")
}
return *content, err
}
// LabelPrefix ...
type LabelPrefix string
const (
// GlobalPrefix ...
GlobalPrefix LabelPrefix = "global"
// LocalPrefix ...
LocalPrefix LabelPrefix = "local"
)
// AddLabels ...
func (client *Client) AddLabels(contentID string, labels []string, prefix LabelPrefix) error {
type Label struct {
Prefix string `json:"prefix"`
Name string `json:"name"`
}
var labelsContent []Label
for _, l := range labels {
labelsContent = append(labelsContent, Label{string(prefix), l})
}
jsonbody, err := json.Marshal(labelsContent)
if err != nil {
return err
}
labelEndpoint := client.labelEndpoint(contentID)
_, err = client.request("POST", labelEndpoint, "", string(jsonbody))
if err != nil {
return err
}
return nil
}
// CreateContentBodyParameters query parameters for CreateContent
type CreateContentBodyParameters struct {
Content
}
// DeleteContent oves 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.
// https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-delete
func (client *Client) DeleteContent(content Content) error {
_, err := client.request("DELETE", "/rest/api/content/"+content.ID, "", "")
return err
}
// ContentResponse represents the data returned from the Confluence API
type ContentResponse struct {
Results []Content `json:"results"`
}
// Content represents the data returned from the Confluence API
type Content struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Ancestors []struct {
ID string `json:"id,omitempty"`
} `json:"ancestors,omitempty"`
Space struct {
Key string `json:"key,omitempty"`
} `json:"space,omitempty"`
Version struct {
Number int `json:"number,omitempty"`
Message string `json:"message,omitempty"`
} `json:"version,omitempty"`
Body struct {
Storage struct {
Value string `json:"value,omitempty"`
Representation string `json:"representation,omitempty"`
EmbeddedContent []interface{} `json:"embeddedContent,omitempty"`
Expandable struct {
Content string `json:"content,omitempty"`
} `json:"_expandable,omitempty"`
} `json:"storage,omitempty"`
} `json:"body,omitempty"`
Links struct {
Self string `json:"self,omitempty"`
Tinyui string `json:"tinyui,omitempty"`
Editui string `json:"editui,omitempty"`
Webui string `json:"webui,omitempty"`
} `json:"_links,omitempty"`
}