-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9820e37
commit 6411edb
Showing
3 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |