forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspaces.go
160 lines (130 loc) · 3.73 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
package bitbucket
import (
"github.com/mitchellh/mapstructure"
)
type Workspace struct {
c *Client
Repositories *Repositories
Permissions *Permission
UUID string
Type string
Slug string
Is_Private bool
Name string
}
type WorkspaceList struct {
Page int
Pagelen int
MaxDepth int
Size int
Next string
Workspaces []Workspace
}
type Permission struct {
c *Client
Type string
}
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.execute("GET", urlStr, "")
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.execute("GET", urlStr, "")
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.execute("GET", urlStr, "")
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) (interface{}, error) {
urlStr := w.c.requestUrl("/workspaces/%s/members", teamname)
return w.c.execute("GET", urlStr, "")
}
func (w *Workspace) Projects(teamname string) (interface{}, error) {
urlStr := w.c.requestUrl("/workspaces/%s/projects/", teamname)
return w.c.execute("GET", urlStr, "")
}
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
}