Skip to content

Commit

Permalink
List and get workspaces (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
josemiguelmelo authored Nov 10, 2020
1 parent 9820e37 commit 6411edb
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Client struct {
User user
Teams teams
Repositories *Repositories
Workspaces *Workspace
Pagelen uint64
MaxDepth uint64
apiBaseURL string
Expand Down Expand Up @@ -138,6 +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.HttpClient = new(http.Client)
return c
}
Expand Down
87 changes: 87 additions & 0 deletions tests/workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tests

import (
"os"
"testing"

"github.com/ktrysmt/go-bitbucket"
)

func getBitbucketClient(t *testing.T) *bitbucket.Client {
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")

if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}

return bitbucket.NewBasicAuth(user, pass)
}

func getWorkspace(t *testing.T) string {
owner := os.Getenv("BITBUCKET_TEST_OWNER")
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}

return owner
}

func getRepositoryName(t *testing.T) string {
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}

return repo
}

func TestListWorkspaces(t *testing.T) {
c := getBitbucketClient(t)

res, err := c.Workspaces.List()
if err != nil {
t.Error("The workspaces could not be listed.")
}

if res == nil || res.Size == 0 {
t.Error("Should have at least one workspace")
}
}

func TestGetWorkspace(t *testing.T) {
c := getBitbucketClient(t)
workspaceName := getWorkspace(t)

res, err := c.Workspaces.Get(workspaceName)
if err != nil {
t.Error("Could not get the workspace.")
}

if res == nil || res.Slug != workspaceName {
t.Error("The workspace was not returned")
}
}

func TestGetWorkspaceRepository(t *testing.T) {
c := getBitbucketClient(t)
workspaceName := getWorkspace(t)
repositoryName := getRepositoryName(t)

opt := &bitbucket.RepositoryOptions{
Owner: workspaceName,
RepoSlug: repositoryName,
}

res, err := c.Workspaces.Repositories.Repository.Get(opt)
if err != nil {
t.Error("The repository is not found.")
}

if res.Full_name != workspaceName+"/"+repositoryName {
t.Error("Cannot catch repos full name.")
}
}
106 changes: 106 additions & 0 deletions workspaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package bitbucket

import (
"github.com/mitchellh/mapstructure"
)

type Workspace struct {
c *Client

Repositories *Repositories

UUID string
Type string
Slug string
Is_Private bool
Name string
}

type WorkspaceList struct {
Page int
Pagelen int
MaxDepth int
Size int
Next string
Workspaces []Workspace
}

func (t *Workspace) List() (*WorkspaceList, error) {
urlStr := t.c.requestUrl("/workspaces")
response, err := t.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return decodeWorkspaceList(response)
}

func (t *Workspace) Get(workspace string) (*Workspace, error) {
urlStr := t.c.requestUrl("/workspaces/%s", workspace)
response, err := t.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return decodeWorkspace(response)
}

func decodeWorkspace(workspace interface{}) (*Workspace, error) {
var workspaceEntry Workspace
workspaceResponseMap := workspace.(map[string]interface{})

if workspaceResponseMap["type"] != nil && workspaceResponseMap["type"] == "error" {
return nil, DecodeError(workspaceResponseMap)
}

err := mapstructure.Decode(workspace, &workspaceEntry)
return &workspaceEntry, err
}

func decodeWorkspaceList(workspaceResponse interface{}) (*WorkspaceList, error) {
workspaceResponseMap := workspaceResponse.(map[string]interface{})
workspaceMapList := workspaceResponseMap["values"].([]interface{})

var workspaces []Workspace
for _, workspaceMap := range workspaceMapList {
workspaceEntry, err := decodeWorkspace(workspaceMap)
if err != nil {
return nil, err
}
workspaces = append(workspaces, *workspaceEntry)
}

page, ok := workspaceResponseMap["page"].(float64)
if !ok {
page = 0
}

pagelen, ok := workspaceResponseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
max_depth, ok := workspaceResponseMap["max_depth"].(float64)
if !ok {
max_depth = 0
}
size, ok := workspaceResponseMap["size"].(float64)
if !ok {
size = 0
}

next, ok := workspaceResponseMap["next"].(string)
if !ok {
next = ""
}

workspacesList := WorkspaceList{
Page: int(page),
Pagelen: int(pagelen),
MaxDepth: int(max_depth),
Size: int(size),
Next: next,
Workspaces: workspaces,
}

return &workspacesList, nil
}

0 comments on commit 6411edb

Please sign in to comment.