|
1 | 1 | package bitbucket |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + |
4 | 6 | "github.com/mitchellh/mapstructure" |
5 | 7 | ) |
6 | 8 |
|
@@ -33,6 +35,14 @@ type Permission struct { |
33 | 35 | Type string |
34 | 36 | } |
35 | 37 |
|
| 38 | +type ProjectsRes struct { |
| 39 | + Page int32 |
| 40 | + Pagelen int32 |
| 41 | + MaxDepth int32 |
| 42 | + Size int32 |
| 43 | + Items []Project |
| 44 | +} |
| 45 | + |
36 | 46 | func (t *Permission) GetUserPermissions(organization, member string) (*Permission, error) { |
37 | 47 | urlStr := t.c.requestUrl("/workspaces/%s/permissions?q=user.nickname=\"%s\"", organization, member) |
38 | 48 | response, err := t.c.execute("GET", urlStr, "") |
@@ -78,9 +88,14 @@ func (w *Workspace) Members(teamname string) (interface{}, error) { |
78 | 88 | return w.c.execute("GET", urlStr, "") |
79 | 89 | } |
80 | 90 |
|
81 | | -func (w *Workspace) Projects(teamname string) (interface{}, error) { |
| 91 | +func (w *Workspace) Projects(teamname string) (*ProjectsRes, error) { |
82 | 92 | 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) |
84 | 99 | } |
85 | 100 |
|
86 | 101 | func decodePermission(permission interface{}) *Permission { |
@@ -159,3 +174,46 @@ func decodeWorkspaceList(workspaceResponse interface{}) (*WorkspaceList, error) |
159 | 174 |
|
160 | 175 | return &workspacesList, nil |
161 | 176 | } |
| 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