Skip to content

Commit 4807eeb

Browse files
authored
Decode projects resource (#149)
After retrieving a list of workspace projects, we now decode the result and return that to the caller. The implementation is pattern after `Repositories.ListForTeam`.
1 parent db639bc commit 4807eeb

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

tests/workspace_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,17 @@ func TestGetWorkspacePermissionForUser(t *testing.T) {
104104
t.Error("The workspace was not returned")
105105
}
106106
}
107+
108+
func TestGetWorkspaceProjects(t *testing.T) {
109+
c := getBitbucketClient(t)
110+
workspaceName := getWorkspace(t)
111+
112+
res, err := c.Workspaces.Projects(workspaceName)
113+
if err != nil {
114+
t.Error("could not get workspace projects")
115+
}
116+
117+
if res == nil || len(res.Items) == 0 {
118+
t.Error("no workspace projects were returned")
119+
}
120+
}

workspaces.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bitbucket
22

33
import (
4+
"errors"
5+
46
"github.com/mitchellh/mapstructure"
57
)
68

@@ -33,6 +35,14 @@ type Permission struct {
3335
Type string
3436
}
3537

38+
type ProjectsRes struct {
39+
Page int32
40+
Pagelen int32
41+
MaxDepth int32
42+
Size int32
43+
Items []Project
44+
}
45+
3646
func (t *Permission) GetUserPermissions(organization, member string) (*Permission, error) {
3747
urlStr := t.c.requestUrl("/workspaces/%s/permissions?q=user.nickname=\"%s\"", organization, member)
3848
response, err := t.c.execute("GET", urlStr, "")
@@ -78,9 +88,14 @@ func (w *Workspace) Members(teamname string) (interface{}, error) {
7888
return w.c.execute("GET", urlStr, "")
7989
}
8090

81-
func (w *Workspace) Projects(teamname string) (interface{}, error) {
91+
func (w *Workspace) Projects(teamname string) (*ProjectsRes, error) {
8292
urlStr := w.c.requestUrl("/workspaces/%s/projects/", teamname)
83-
return w.c.execute("GET", urlStr, "")
93+
response, err := w.c.execute("GET", urlStr, "")
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
return decodeProjects(response)
8499
}
85100

86101
func decodePermission(permission interface{}) *Permission {
@@ -159,3 +174,46 @@ func decodeWorkspaceList(workspaceResponse interface{}) (*WorkspaceList, error)
159174

160175
return &workspacesList, nil
161176
}
177+
178+
func decodeProjects(projectResponse interface{}) (*ProjectsRes, error) {
179+
projectsResponseMap, ok := projectResponse.(map[string]interface{})
180+
if !ok {
181+
return nil, errors.New("Not a valid format")
182+
}
183+
184+
var projects []Project
185+
projectArray := projectsResponseMap["values"].([]interface{})
186+
for _, projectEntry := range projectArray {
187+
var project Project
188+
if err := mapstructure.Decode(projectEntry, &project); err == nil {
189+
projects = append(projects, project)
190+
}
191+
}
192+
193+
page, ok := projectsResponseMap["page"].(float64)
194+
if !ok {
195+
page = 0
196+
}
197+
198+
pagelen, ok := projectsResponseMap["pagelen"].(float64)
199+
if !ok {
200+
pagelen = 0
201+
}
202+
max_depth, ok := projectsResponseMap["max_width"].(float64)
203+
if !ok {
204+
max_depth = 0
205+
}
206+
size, ok := projectsResponseMap["size"].(float64)
207+
if !ok {
208+
size = 0
209+
}
210+
211+
res := ProjectsRes{
212+
Page: int32(page),
213+
Pagelen: int32(pagelen),
214+
MaxDepth: int32(max_depth),
215+
Size: int32(size),
216+
Items: projects,
217+
}
218+
return &res, nil
219+
}

0 commit comments

Comments
 (0)