forked from plouc/go-gitlab-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublic_keys.go
80 lines (71 loc) · 2.06 KB
/
public_keys.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
package gogitlab
import (
"encoding/json"
"net/url"
)
const (
// ID
user_keys = "/user/keys" // Get current user keys
user_key = "/user/keys/:id" // Get user key by id
list_keys = "/users/:uid/keys" // Get keys for the user id
custom_user_keys = "/user/:id/keys" // Create key for user with :id
)
type PublicKey struct {
Id int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Key string `json:"key,omitempty"`
CreatedAtRaw string `json:"created_at,omitempty"`
}
func (g *Gitlab) UserKeys() ([]*PublicKey, error) {
url := g.ResourceUrl(user_keys, nil)
var keys []*PublicKey
contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &keys)
}
return keys, err
}
func (g *Gitlab) ListKeys(id string) ([]*PublicKey, error) {
url := g.ResourceUrl(list_keys, map[string]string{":uid": id})
var keys []*PublicKey
contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &keys)
}
return keys, err
}
func (g *Gitlab) UserKey(id string) (*PublicKey, error) {
url := g.ResourceUrl(user_key, map[string]string{":id": id})
var key *PublicKey
contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &key)
}
return key, err
}
func (g *Gitlab) AddKey(title, key string) error {
path := g.ResourceUrl(user_keys, nil)
var err error
v := url.Values{}
v.Set("title", title)
v.Set("key", key)
body := v.Encode()
_, err = g.buildAndExecRequest("POST", path, []byte(body))
return err
}
func (g *Gitlab) AddUserKey(id, title, key string) error {
path := g.ResourceUrl(user_keys, map[string]string{":id": id})
var err error
v := url.Values{}
v.Set("title", title)
v.Set("key", key)
body := v.Encode()
_, err = g.buildAndExecRequest("POST", path, []byte(body))
return err
}
func (g *Gitlab) DeleteKey(id string) error {
url := g.ResourceUrl(user_key, map[string]string{":id": id})
var err error
_, err = g.buildAndExecRequest("DELETE", url, nil)
return err
}