forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
110 lines (87 loc) · 2.47 KB
/
project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package bitbucket
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
)
type Project struct {
c *Client
Uuid string
Key string
Name string
Description string
Is_private 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, err := t.buildProjectBody(opt)
if err != nil {
return nil, err
}
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, err := t.buildProjectBody(opt)
if err != nil {
return nil, err
}
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, error) {
data, err := json.Marshal(body)
if err != nil {
return "", err
}
return string(data), nil
}
func (t *Workspace) buildProjectBody(opts *ProjectOptions) (string, error) {
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
}