forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploykeys.go
76 lines (60 loc) · 1.69 KB
/
deploykeys.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
package bitbucket
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
)
type DeployKeys struct {
c *Client
}
type DeployKey struct {
Id int `json:"id"`
Label string `json:"label"`
Key string `json:"key"`
Comment string `json:"comment"`
}
func decodeDeployKey(response interface{}) (*DeployKey, error) {
respMap := response.(map[string]interface{})
if respMap["type"] == "error" {
return nil, DecodeError(respMap)
}
var deployKey = new(DeployKey)
err := mapstructure.Decode(respMap, deployKey)
if err != nil {
return nil, err
}
return deployKey, nil
}
func buildDeployKeysBody(opt *DeployKeyOptions) (string, error) {
body := map[string]interface{}{}
body["label"] = opt.Label
body["key"] = opt.Key
data, err := json.Marshal(body)
if err != nil {
return "", err
}
return string(data), nil
}
func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
data, err := buildDeployKeysBody(opt)
if err != nil {
return nil, err
}
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
response, err := dk.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}
return decodeDeployKey(response)
}
func (dk *DeployKeys) Get(opt *DeployKeyOptions) (*DeployKey, error) {
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys/%d", opt.Owner, opt.RepoSlug, opt.Id)
response, err := dk.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeDeployKey(response)
}
func (dk *DeployKeys) Delete(opt *DeployKeyOptions) (interface{}, error) {
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys/%d", opt.Owner, opt.RepoSlug, opt.Id)
return dk.c.execute("DELETE", urlStr, "")
}