diff --git a/client.go b/client.go index 9dd9175..f36b4a8 100644 --- a/client.go +++ b/client.go @@ -139,7 +139,7 @@ func injectClient(a *auth) *Client { c.Users = &Users{c: c} c.User = &User{c: c} c.Teams = &Teams{c: c} - c.Workspaces = &Workspace{c: c, Repositories: c.Repositories} + c.Workspaces = &Workspace{c: c, Repositories: c.Repositories, Permissions: &Permission{c: c}} c.HttpClient = new(http.Client) return c } diff --git a/tests/workspace_test.go b/tests/workspace_test.go index d0fd09f..d6ef4a8 100644 --- a/tests/workspace_test.go +++ b/tests/workspace_test.go @@ -85,3 +85,17 @@ func TestGetWorkspaceRepository(t *testing.T) { t.Error("Cannot catch repos full name.") } } + +func TestGetWorkspacePermissionForUser(t *testing.T) { + c := getBitbucketClient(t) + workspaceName := getWorkspace(t) + + res, err := c.Workspaces.Permissions.GetUserPermissions(workspaceName, "josemiguelmelo") + if err != nil { + t.Error("Could not get the workspace.") + } + + if res == nil || res.Type == "" { + t.Error("The workspace was not returned") + } +} diff --git a/workspaces.go b/workspaces.go index 2f433e4..fb99e76 100644 --- a/workspaces.go +++ b/workspaces.go @@ -8,6 +8,7 @@ type Workspace struct { c *Client Repositories *Repositories + Permissions *Permission UUID string Type string @@ -25,6 +26,22 @@ type WorkspaceList struct { 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 *Workspace) List() (*WorkspaceList, error) { urlStr := t.c.requestUrl("/workspaces") response, err := t.c.execute("GET", urlStr, "") @@ -45,6 +62,23 @@ func (t *Workspace) Get(workspace string) (*Workspace, error) { return decodeWorkspace(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{})