Skip to content

Commit b8bc8c9

Browse files
authored
add workspace support (ktrysmt#132)
1 parent 5941b62 commit b8bc8c9

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

bitbucket.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type pullrequests interface {
2525
Merge(opt PullRequestsOptions) (interface{}, error)
2626
Decline(opt PullRequestsOptions) (interface{}, error)
2727
}
28+
type workspace interface {
29+
GetProject(opt ProjectOptions) (*Project, error)
30+
CreateProject(opt ProjectOptions) (*Project, error)
31+
}
2832

2933
type repository interface {
3034
Get(opt RepositoryOptions) (*Repository, error)

project.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package bitbucket
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/k0kubun/pp"
8+
"github.com/mitchellh/mapstructure"
9+
)
10+
11+
type Project struct {
12+
c *Client
13+
14+
Uuid string
15+
Key string
16+
Name string
17+
Description string
18+
IsPrivate bool
19+
}
20+
21+
type ProjectOptions struct {
22+
Uuid string `json:"uuid"`
23+
Owner string `json:"owner"`
24+
Name string `json:"name"`
25+
Key string `json:"key"`
26+
Description string `json:"description"`
27+
IsPrivate bool `json:"is_private"`
28+
}
29+
30+
func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
31+
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
32+
response, err := t.c.execute("GET", urlStr, "")
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return decodeProject(response)
38+
}
39+
40+
func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
41+
data := t.buildProjectBody(opt)
42+
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
43+
response, err := t.c.execute("POST", urlStr, data)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return decodeProject(response)
49+
}
50+
51+
func (t *Workspace) DeleteProject(opt *ProjectOptions) (interface{}, error) {
52+
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
53+
return t.c.execute("DELETE", urlStr, "")
54+
}
55+
56+
func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
57+
data := t.buildProjectBody(opt)
58+
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
59+
response, err := t.c.execute("PUT", urlStr, data)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return decodeProject(response)
65+
}
66+
67+
func (t *Workspace) buildJsonBody(body map[string]interface{}) string {
68+
data, err := json.Marshal(body)
69+
if err != nil {
70+
pp.Println(err)
71+
os.Exit(9)
72+
}
73+
74+
return string(data)
75+
}
76+
77+
func (t *Workspace) buildProjectBody(opts *ProjectOptions) string {
78+
body := map[string]interface{}{}
79+
80+
if opts.Description != "" {
81+
body["description"] = opts.Description
82+
}
83+
84+
if opts.Name != "" {
85+
body["name"] = opts.Name
86+
}
87+
88+
if opts.Key != "" {
89+
body["key"] = opts.Key
90+
}
91+
92+
body["is_private"] = opts.IsPrivate
93+
94+
return t.buildJsonBody(body)
95+
}
96+
97+
func decodeProject(project interface{}) (*Project, error) {
98+
var projectEntry Project
99+
projectResponseMap := project.(map[string]interface{})
100+
101+
if projectResponseMap["type"] != nil && projectResponseMap["type"] == "error" {
102+
return nil, DecodeError(projectResponseMap)
103+
}
104+
105+
err := mapstructure.Decode(project, &projectEntry)
106+
return &projectEntry, err
107+
}

repository.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import (
1515
"github.com/mitchellh/mapstructure"
1616
)
1717

18-
type Project struct {
19-
Key string
20-
Name string
21-
}
22-
2318
type Repository struct {
2419
c *Client
2520

workspaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Workspace struct {
1515
Slug string
1616
Is_Private bool
1717
Name string
18+
workspace
1819
}
1920

2021
type WorkspaceList struct {

0 commit comments

Comments
 (0)