Skip to content

Commit

Permalink
add workspace support (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroschupp authored Jun 9, 2021
1 parent 5941b62 commit b8bc8c9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type pullrequests interface {
Merge(opt PullRequestsOptions) (interface{}, error)
Decline(opt PullRequestsOptions) (interface{}, error)
}
type workspace interface {
GetProject(opt ProjectOptions) (*Project, error)
CreateProject(opt ProjectOptions) (*Project, error)
}

type repository interface {
Get(opt RepositoryOptions) (*Repository, error)
Expand Down
107 changes: 107 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package bitbucket

import (
"encoding/json"
"os"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
)

type Project struct {
c *Client

Uuid string
Key string
Name string
Description string
IsPrivate bool
}

type ProjectOptions struct {
Uuid string `json:"uuid"`
Owner string `json:"owner"`
Name string `json:"name"`
Key string `json:"key"`
Description string `json:"description"`
IsPrivate bool `json:"is_private"`
}

func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
response, err := t.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return decodeProject(response)
}

func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
data := t.buildProjectBody(opt)
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
response, err := t.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}

return decodeProject(response)
}

func (t *Workspace) DeleteProject(opt *ProjectOptions) (interface{}, error) {
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
return t.c.execute("DELETE", urlStr, "")
}

func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
data := t.buildProjectBody(opt)
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
response, err := t.c.execute("PUT", urlStr, data)
if err != nil {
return nil, err
}

return decodeProject(response)
}

func (t *Workspace) buildJsonBody(body map[string]interface{}) string {
data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
}

return string(data)
}

func (t *Workspace) buildProjectBody(opts *ProjectOptions) string {
body := map[string]interface{}{}

if opts.Description != "" {
body["description"] = opts.Description
}

if opts.Name != "" {
body["name"] = opts.Name
}

if opts.Key != "" {
body["key"] = opts.Key
}

body["is_private"] = opts.IsPrivate

return t.buildJsonBody(body)
}

func decodeProject(project interface{}) (*Project, error) {
var projectEntry Project
projectResponseMap := project.(map[string]interface{})

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

err := mapstructure.Decode(project, &projectEntry)
return &projectEntry, err
}
5 changes: 0 additions & 5 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import (
"github.com/mitchellh/mapstructure"
)

type Project struct {
Key string
Name string
}

type Repository struct {
c *Client

Expand Down
1 change: 1 addition & 0 deletions workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Workspace struct {
Slug string
Is_Private bool
Name string
workspace
}

type WorkspaceList struct {
Expand Down

0 comments on commit b8bc8c9

Please sign in to comment.