-
Notifications
You must be signed in to change notification settings - Fork 147
/
workspaces.go
271 lines (226 loc) · 5.96 KB
/
workspaces.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
package bitbucket
import (
"errors"
"github.com/mitchellh/mapstructure"
)
type Workspace struct {
c *Client
Repositories *Repositories
Permissions *Permission
UUID string
Type string
Slug string
Is_Private bool
Name string
workspace
}
type WorkspaceList struct {
Page int
Pagelen int
MaxDepth int
Size int
Next string
Workspaces []Workspace
}
type Permission struct {
c *Client
Type string
}
type ProjectsRes struct {
Page int32
Pagelen int32
MaxDepth int32
Size int32
Items []Project
}
type WorkspaceMembers struct {
Page int
Pagelen int
Size int
Members []User
}
func (t *Permission) GetUserPermissions(organization, member string) (*Permission, error) {
urlStr := t.c.requestUrl("/workspaces/%s/permissions?q=user.nickname=\"%s\"", organization, member)
response, err := t.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodePermission(response), err
}
func (t *Permission) GetUserPermissionsByUuid(organization, member string) (*Permission, error) {
urlStr := t.c.requestUrl("/workspaces/%s/permissions?q=user.uuid=\"%s\"", organization, member)
response, err := t.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodePermission(response), err
}
func (t *Workspace) List() (*WorkspaceList, error) {
urlStr := t.c.requestUrl("/workspaces")
response, err := t.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodeWorkspaceList(response)
}
func (t *Workspace) Get(workspace string) (*Workspace, error) {
urlStr := t.c.requestUrl("/workspaces/%s", workspace)
response, err := t.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeWorkspace(response)
}
func (w *Workspace) Members(teamname string) (*WorkspaceMembers, error) {
urlStr := w.c.requestUrl("/workspaces/%s/members", teamname)
response, err := w.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodeMembers(response)
}
func (w *Workspace) Projects(teamname string) (*ProjectsRes, error) {
urlStr := w.c.requestUrl("/workspaces/%s/projects/", teamname)
response, err := w.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodeProjects(response)
}
func decodePermission(permission interface{}) *Permission {
permissionResponseMap := permission.(map[string]interface{})
if permissionResponseMap["size"].(float64) == 0 {
return nil
}
permissionValues := permissionResponseMap["values"].([]interface{})
if len(permissionValues) == 0 {
return nil
}
permissionValue := permissionValues[0].(map[string]interface{})
return &Permission{
Type: permissionValue["permission"].(string),
}
}
func decodeWorkspace(workspace interface{}) (*Workspace, error) {
var workspaceEntry Workspace
workspaceResponseMap := workspace.(map[string]interface{})
if workspaceResponseMap["type"] != nil && workspaceResponseMap["type"] == "error" {
return nil, DecodeError(workspaceResponseMap)
}
err := mapstructure.Decode(workspace, &workspaceEntry)
return &workspaceEntry, err
}
func decodeWorkspaceList(workspaceResponse interface{}) (*WorkspaceList, error) {
workspaceResponseMap := workspaceResponse.(map[string]interface{})
workspaceMapList := workspaceResponseMap["values"].([]interface{})
var workspaces []Workspace
for _, workspaceMap := range workspaceMapList {
workspaceEntry, err := decodeWorkspace(workspaceMap)
if err != nil {
return nil, err
}
workspaces = append(workspaces, *workspaceEntry)
}
page, ok := workspaceResponseMap["page"].(float64)
if !ok {
page = 0
}
pagelen, ok := workspaceResponseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
max_depth, ok := workspaceResponseMap["max_depth"].(float64)
if !ok {
max_depth = 0
}
size, ok := workspaceResponseMap["size"].(float64)
if !ok {
size = 0
}
next, ok := workspaceResponseMap["next"].(string)
if !ok {
next = ""
}
workspacesList := WorkspaceList{
Page: int(page),
Pagelen: int(pagelen),
MaxDepth: int(max_depth),
Size: int(size),
Next: next,
Workspaces: workspaces,
}
return &workspacesList, nil
}
func decodeProjects(projectResponse interface{}) (*ProjectsRes, error) {
projectsResponseMap, ok := projectResponse.(map[string]interface{})
if !ok {
return nil, errors.New("Not a valid format")
}
var projects []Project
projectArray := projectsResponseMap["values"].([]interface{})
for _, projectEntry := range projectArray {
var project Project
if err := mapstructure.Decode(projectEntry, &project); err == nil {
projects = append(projects, project)
}
}
page, ok := projectsResponseMap["page"].(float64)
if !ok {
page = 0
}
pagelen, ok := projectsResponseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
max_depth, ok := projectsResponseMap["max_width"].(float64)
if !ok {
max_depth = 0
}
size, ok := projectsResponseMap["size"].(float64)
if !ok {
size = 0
}
res := ProjectsRes{
Page: int32(page),
Pagelen: int32(pagelen),
MaxDepth: int32(max_depth),
Size: int32(size),
Items: projects,
}
return &res, nil
}
func decodeMembers(membersResponse interface{}) (*WorkspaceMembers, error) {
responseMap, ok := membersResponse.(map[string]interface{})
if !ok {
return nil, errors.New("not a valid format")
}
var members []User
userArray := responseMap["values"].([]interface{})
for _, userEntry := range userArray {
userEntryMap := userEntry.(map[string]interface{})
member, err := decodeUser(userEntryMap["user"])
if err != nil {
return nil, err
}
members = append(members, *member)
}
page, ok := responseMap["page"].(int)
if !ok {
page = 0
}
pagelen, ok := responseMap["pagelen"].(int)
if !ok {
pagelen = 0
}
size, ok := responseMap["size"].(int)
if !ok {
size = 0
}
workspaceMembers := WorkspaceMembers{
Page: page,
Pagelen: pagelen,
Size: size,
Members: members,
}
return &workspaceMembers, nil
}