Skip to content

Commit cec8f67

Browse files
Get permission for user on workspace (#105)
1 parent 99adb9b commit cec8f67

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func injectClient(a *auth) *Client {
139139
c.Users = &Users{c: c}
140140
c.User = &User{c: c}
141141
c.Teams = &Teams{c: c}
142-
c.Workspaces = &Workspace{c: c, Repositories: c.Repositories}
142+
c.Workspaces = &Workspace{c: c, Repositories: c.Repositories, Permissions: &Permission{c: c}}
143143
c.HttpClient = new(http.Client)
144144
return c
145145
}

tests/workspace_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,17 @@ func TestGetWorkspaceRepository(t *testing.T) {
8585
t.Error("Cannot catch repos full name.")
8686
}
8787
}
88+
89+
func TestGetWorkspacePermissionForUser(t *testing.T) {
90+
c := getBitbucketClient(t)
91+
workspaceName := getWorkspace(t)
92+
93+
res, err := c.Workspaces.Permissions.GetUserPermissions(workspaceName, "josemiguelmelo")
94+
if err != nil {
95+
t.Error("Could not get the workspace.")
96+
}
97+
98+
if res == nil || res.Type == "" {
99+
t.Error("The workspace was not returned")
100+
}
101+
}

workspaces.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Workspace struct {
88
c *Client
99

1010
Repositories *Repositories
11+
Permissions *Permission
1112

1213
UUID string
1314
Type string
@@ -25,6 +26,22 @@ type WorkspaceList struct {
2526
Workspaces []Workspace
2627
}
2728

29+
type Permission struct {
30+
c *Client
31+
32+
Type string
33+
}
34+
35+
func (t *Permission) GetUserPermissions(organization, member string) (*Permission, error) {
36+
urlStr := t.c.requestUrl("/workspaces/%s/permissions?q=user.nickname=\"%s\"", organization, member)
37+
response, err := t.c.execute("GET", urlStr, "")
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return decodePermission(response), err
43+
}
44+
2845
func (t *Workspace) List() (*WorkspaceList, error) {
2946
urlStr := t.c.requestUrl("/workspaces")
3047
response, err := t.c.execute("GET", urlStr, "")
@@ -45,6 +62,23 @@ func (t *Workspace) Get(workspace string) (*Workspace, error) {
4562
return decodeWorkspace(response)
4663
}
4764

65+
func decodePermission(permission interface{}) *Permission {
66+
permissionResponseMap := permission.(map[string]interface{})
67+
if permissionResponseMap["size"].(float64) == 0 {
68+
return nil
69+
}
70+
71+
permissionValues := permissionResponseMap["values"].([]interface{})
72+
if len(permissionValues) == 0 {
73+
return nil
74+
}
75+
76+
permissionValue := permissionValues[0].(map[string]interface{})
77+
return &Permission{
78+
Type: permissionValue["permission"].(string),
79+
}
80+
}
81+
4882
func decodeWorkspace(workspace interface{}) (*Workspace, error) {
4983
var workspaceEntry Workspace
5084
workspaceResponseMap := workspace.(map[string]interface{})

0 commit comments

Comments
 (0)