Skip to content

Commit

Permalink
Get permission for user on workspace (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
josemiguelmelo authored Nov 19, 2020
1 parent 99adb9b commit cec8f67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions tests/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
34 changes: 34 additions & 0 deletions workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Workspace struct {
c *Client

Repositories *Repositories
Permissions *Permission

UUID string
Type string
Expand All @@ -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, "")
Expand All @@ -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{})
Expand Down

0 comments on commit cec8f67

Please sign in to comment.