Skip to content

Commit 6411edb

Browse files
List and get workspaces (#101)
1 parent 9820e37 commit 6411edb

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Client struct {
3030
User user
3131
Teams teams
3232
Repositories *Repositories
33+
Workspaces *Workspace
3334
Pagelen uint64
3435
MaxDepth uint64
3536
apiBaseURL string
@@ -138,6 +139,7 @@ func injectClient(a *auth) *Client {
138139
c.Users = &Users{c: c}
139140
c.User = &User{c: c}
140141
c.Teams = &Teams{c: c}
142+
c.Workspaces = &Workspace{c: c, Repositories: c.Repositories}
141143
c.HttpClient = new(http.Client)
142144
return c
143145
}

tests/workspace_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/ktrysmt/go-bitbucket"
8+
)
9+
10+
func getBitbucketClient(t *testing.T) *bitbucket.Client {
11+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
12+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
13+
14+
if user == "" {
15+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
16+
}
17+
if pass == "" {
18+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
19+
}
20+
21+
return bitbucket.NewBasicAuth(user, pass)
22+
}
23+
24+
func getWorkspace(t *testing.T) string {
25+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
26+
if owner == "" {
27+
t.Error("BITBUCKET_TEST_OWNER is empty.")
28+
}
29+
30+
return owner
31+
}
32+
33+
func getRepositoryName(t *testing.T) string {
34+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
35+
if repo == "" {
36+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
37+
}
38+
39+
return repo
40+
}
41+
42+
func TestListWorkspaces(t *testing.T) {
43+
c := getBitbucketClient(t)
44+
45+
res, err := c.Workspaces.List()
46+
if err != nil {
47+
t.Error("The workspaces could not be listed.")
48+
}
49+
50+
if res == nil || res.Size == 0 {
51+
t.Error("Should have at least one workspace")
52+
}
53+
}
54+
55+
func TestGetWorkspace(t *testing.T) {
56+
c := getBitbucketClient(t)
57+
workspaceName := getWorkspace(t)
58+
59+
res, err := c.Workspaces.Get(workspaceName)
60+
if err != nil {
61+
t.Error("Could not get the workspace.")
62+
}
63+
64+
if res == nil || res.Slug != workspaceName {
65+
t.Error("The workspace was not returned")
66+
}
67+
}
68+
69+
func TestGetWorkspaceRepository(t *testing.T) {
70+
c := getBitbucketClient(t)
71+
workspaceName := getWorkspace(t)
72+
repositoryName := getRepositoryName(t)
73+
74+
opt := &bitbucket.RepositoryOptions{
75+
Owner: workspaceName,
76+
RepoSlug: repositoryName,
77+
}
78+
79+
res, err := c.Workspaces.Repositories.Repository.Get(opt)
80+
if err != nil {
81+
t.Error("The repository is not found.")
82+
}
83+
84+
if res.Full_name != workspaceName+"/"+repositoryName {
85+
t.Error("Cannot catch repos full name.")
86+
}
87+
}

workspaces.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package bitbucket
2+
3+
import (
4+
"github.com/mitchellh/mapstructure"
5+
)
6+
7+
type Workspace struct {
8+
c *Client
9+
10+
Repositories *Repositories
11+
12+
UUID string
13+
Type string
14+
Slug string
15+
Is_Private bool
16+
Name string
17+
}
18+
19+
type WorkspaceList struct {
20+
Page int
21+
Pagelen int
22+
MaxDepth int
23+
Size int
24+
Next string
25+
Workspaces []Workspace
26+
}
27+
28+
func (t *Workspace) List() (*WorkspaceList, error) {
29+
urlStr := t.c.requestUrl("/workspaces")
30+
response, err := t.c.execute("GET", urlStr, "")
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return decodeWorkspaceList(response)
36+
}
37+
38+
func (t *Workspace) Get(workspace string) (*Workspace, error) {
39+
urlStr := t.c.requestUrl("/workspaces/%s", workspace)
40+
response, err := t.c.execute("GET", urlStr, "")
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return decodeWorkspace(response)
46+
}
47+
48+
func decodeWorkspace(workspace interface{}) (*Workspace, error) {
49+
var workspaceEntry Workspace
50+
workspaceResponseMap := workspace.(map[string]interface{})
51+
52+
if workspaceResponseMap["type"] != nil && workspaceResponseMap["type"] == "error" {
53+
return nil, DecodeError(workspaceResponseMap)
54+
}
55+
56+
err := mapstructure.Decode(workspace, &workspaceEntry)
57+
return &workspaceEntry, err
58+
}
59+
60+
func decodeWorkspaceList(workspaceResponse interface{}) (*WorkspaceList, error) {
61+
workspaceResponseMap := workspaceResponse.(map[string]interface{})
62+
workspaceMapList := workspaceResponseMap["values"].([]interface{})
63+
64+
var workspaces []Workspace
65+
for _, workspaceMap := range workspaceMapList {
66+
workspaceEntry, err := decodeWorkspace(workspaceMap)
67+
if err != nil {
68+
return nil, err
69+
}
70+
workspaces = append(workspaces, *workspaceEntry)
71+
}
72+
73+
page, ok := workspaceResponseMap["page"].(float64)
74+
if !ok {
75+
page = 0
76+
}
77+
78+
pagelen, ok := workspaceResponseMap["pagelen"].(float64)
79+
if !ok {
80+
pagelen = 0
81+
}
82+
max_depth, ok := workspaceResponseMap["max_depth"].(float64)
83+
if !ok {
84+
max_depth = 0
85+
}
86+
size, ok := workspaceResponseMap["size"].(float64)
87+
if !ok {
88+
size = 0
89+
}
90+
91+
next, ok := workspaceResponseMap["next"].(string)
92+
if !ok {
93+
next = ""
94+
}
95+
96+
workspacesList := WorkspaceList{
97+
Page: int(page),
98+
Pagelen: int(pagelen),
99+
MaxDepth: int(max_depth),
100+
Size: int(size),
101+
Next: next,
102+
Workspaces: workspaces,
103+
}
104+
105+
return &workspacesList, nil
106+
}

0 commit comments

Comments
 (0)