-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathclient_server.go
More file actions
286 lines (233 loc) · 8.27 KB
/
Copy pathclient_server.go
File metadata and controls
286 lines (233 loc) · 8.27 KB
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
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-plugin-confluence/server/serializer"
"github.com/mattermost/mattermost-plugin-confluence/server/service"
"github.com/mattermost/mattermost-plugin-confluence/server/util"
"github.com/mattermost/mattermost-plugin-confluence/server/util/types"
)
const (
PathCurrentUser = "/rest/api/user/current"
PathContentData = "/rest/api/content/"
PathSpaceData = "/rest/api/space/"
PathUserData = "/rest/api/user/"
PathAdminData = "/rest/api/audit"
)
const (
Comment = "comment"
Space = "space"
Page = "page"
)
const pageSize = 10
type confluenceServerClient struct {
URL string
HTTPClient *http.Client
}
type ConfluenceServerUser struct {
UserKey string `json:"userKey"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Type string `json:"type"`
}
type AdminData struct {
Number int `json:"number"`
Units string `json:"units"`
}
type SpaceResponse struct {
ID int64 `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
Links Links `json:"_links"`
}
type CommentContainer struct {
ID string `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Links Links `json:"_links"`
}
type Links struct {
Self string `json:"webui"`
}
type View struct {
Value string `json:"value"`
}
type Body struct {
View View `json:"view"`
}
type CreatedBy struct {
Username string `json:"username"`
}
type History struct {
CreatedBy CreatedBy `json:"createdBy"`
}
type CommentResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Space SpaceResponse `json:"space"`
Container CommentContainer `json:"container"`
Body Body `json:"body"`
Links Links `json:"_links"`
History History `json:"history"`
}
type PageResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Space SpaceResponse `json:"space"`
Body Body `json:"body"`
Links Links `json:"_links"`
History History `json:"history"`
}
type ConfluenceServerEvent struct {
Comment *CommentResponse
Page *PageResponse
Space *SpaceResponse
BaseURL string
}
func newServerClient(url string, httpClient *http.Client) Client {
return &confluenceServerClient{
URL: url,
HTTPClient: httpClient,
}
}
func (csc *confluenceServerClient) GetSelf() (*types.ConfluenceUser, error) {
confluenceServerUser := &ConfluenceServerUser{}
if _, _, err := service.CallJSONWithURL(csc.URL, PathCurrentUser, http.MethodGet, nil, confluenceServerUser, csc.HTTPClient); err != nil {
return nil, errors.Wrap(err, "Confluence GetSelf. Error getting the current user")
}
confluenceUser := &types.ConfluenceUser{
AccountID: confluenceServerUser.UserKey,
Name: confluenceServerUser.Username,
DisplayName: confluenceServerUser.DisplayName,
}
return confluenceUser, nil
}
func (csc *confluenceServerClient) GetEventData(webhookPayload *serializer.ConfluenceServerWebhookPayload) (*ConfluenceServerEvent, error) {
var confluenceServerEvent ConfluenceServerEvent
var err error
if strings.Contains(webhookPayload.Event, Comment) {
confluenceServerEvent.Comment, err = csc.GetCommentData(webhookPayload)
if err != nil {
return nil, errors.Errorf("error getting comment data for the event. CommentID %d. Error: %v", webhookPayload.Comment.ID, err)
}
}
if strings.Contains(webhookPayload.Event, Page) {
confluenceServerEvent.Page, err = csc.GetPageData(int(webhookPayload.Page.ID))
if err != nil {
return nil, errors.Errorf("error getting page data for the event. PageID %d. Error: %v", webhookPayload.Page.ID, err)
}
}
if strings.Contains(webhookPayload.Event, Space) {
confluenceServerEvent.Space, err = csc.GetSpaceData(webhookPayload.Space.SpaceKey)
if err != nil {
return nil, errors.Errorf("error getting space data for the event. SpaceKey %s. Error: %v", webhookPayload.Space.SpaceKey, err)
}
}
return &confluenceServerEvent, nil
}
func (csc *confluenceServerClient) GetCommentData(webhookPayload *serializer.ConfluenceServerWebhookPayload) (*CommentResponse, error) {
commentResponse := &CommentResponse{}
if _, _, err := service.CallJSONWithURL(csc.URL, fmt.Sprintf("%s%s?expand=body.view,container,space,history", PathContentData, strconv.FormatInt(webhookPayload.Comment.ID, 10)), http.MethodGet, nil, commentResponse, csc.HTTPClient); err != nil {
return nil, err
}
commentResponse.Body.View.Value = util.GetBodyForExcerpt(commentResponse.Body.View.Value)
return commentResponse, nil
}
func (csc *confluenceServerClient) GetPageData(pageID int) (*PageResponse, error) {
pageResponse := &PageResponse{}
path := fmt.Sprintf("%s%s?status=any&expand=body.view,container,space,history", PathContentData, strconv.Itoa(pageID))
if _, statusCode, err := service.CallJSONWithURL(csc.URL, path, http.MethodGet, nil, pageResponse, csc.HTTPClient); err != nil {
return nil, withAPIStatus(err, path, statusCode)
}
pageResponse.Body.View.Value = util.GetBodyForExcerpt(pageResponse.Body.View.Value)
return pageResponse, nil
}
func (csc *confluenceServerClient) GetSpaceData(spaceKey string) (*SpaceResponse, error) {
spaceResponse := &SpaceResponse{}
path := fmt.Sprintf("%s%s?status=any", PathSpaceData, spaceKey)
if _, statusCode, err := service.CallJSONWithURL(csc.URL, path, http.MethodGet, nil, spaceResponse, csc.HTTPClient); err != nil {
return nil, withAPIStatus(err, path, statusCode)
}
return spaceResponse, nil
}
type apiResponse struct {
Results []struct {
ID int64 `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
} `json:"results"`
Size int `json:"size"`
}
func (csc *confluenceServerClient) GetSpaceKeyFromSpaceID(spaceID int64) (string, error) {
start := 0
for {
path := fmt.Sprintf("%s?start=%d&limit=%d", PathSpaceData, start, pageSize)
response := &apiResponse{}
if _, _, err := service.CallJSONWithURL(csc.URL, path, http.MethodGet, nil, response, csc.HTTPClient); err != nil {
return "", errors.Wrap(err, "Confluence GetSpaceKeyFromSpaceID")
}
for _, space := range response.Results {
if space.ID == spaceID {
return space.Key, nil
}
}
if len(response.Results) < pageSize {
break
}
start += pageSize
}
return "", fmt.Errorf("confluence GetSpaceKeyFromSpaceID: no space key found for the space ID")
}
type ConfluenceUser struct {
DisplayName string `json:"displayName"`
Type string `json:"type"`
UserKey string `json:"userKey"`
Username string `json:"username"`
ProfilePicture struct {
Path string `json:"path"`
Width int `json:"width"`
Height int `json:"height"`
IsDefault bool `json:"isDefault"`
} `json:"profilePicture"`
Expandable map[string]string `json:"_expandable"`
Links struct {
Base string `json:"base"`
Context string `json:"context"`
Self string `json:"self"`
} `json:"_links"`
}
type serverStorageBodyResponse struct {
Body struct {
Storage struct {
Value string `json:"value"`
Representation string `json:"representation"`
} `json:"storage"`
} `json:"body"`
}
func (csc *confluenceServerClient) MentionAccountIDsInPage(pageID string) ([]string, error) {
return csc.mentionAccountIDsFromStorage(pageID)
}
func (csc *confluenceServerClient) MentionAccountIDsInComment(commentID, _ string) ([]string, error) {
return csc.mentionAccountIDsFromStorage(commentID)
}
func (csc *confluenceServerClient) mentionAccountIDsFromStorage(contentID string) ([]string, error) {
path := fmt.Sprintf("%s%s?expand=body.storage", PathContentData, contentID)
resp := &serverStorageBodyResponse{}
if _, _, err := service.CallJSONWithURL(csc.URL, path, http.MethodGet, nil, resp, csc.HTTPClient); err != nil {
return nil, err
}
if resp.Body.Storage.Value == "" {
return nil, nil
}
return service.ExtractMentionAccountIDsFromStorage([]byte(resp.Body.Storage.Value))
}
func (csc *confluenceServerClient) GetUserFromUserKey(userKey string) (*ConfluenceUser, error) {
var user ConfluenceUser
if _, _, err := service.CallJSONWithURL(csc.URL, fmt.Sprintf("%s?key=%s", PathUserData, userKey), http.MethodGet, nil, &user, csc.HTTPClient); err != nil {
return nil, fmt.Errorf("error fetching user data: %w", err)
}
return &user, nil
}