forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_keys.go
109 lines (88 loc) · 2.64 KB
/
repo_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
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
package github
import (
"fmt"
)
type RepoKeyResource struct {
client *Client
}
// Gets a list of the keys associated with a repo.
func (r *RepoKeyResource) List(owner, repo string) ([]*Key, error) {
keys := []*Key{}
path := fmt.Sprintf("/repos/%s/%s/keys", owner, repo)
if err := r.client.do("GET", path, nil, &keys); err != nil {
return nil, err
}
return keys, nil
}
// Gets the key associated with the specified id.
func (r *RepoKeyResource) Find(owner, repo string, id int) (*Key, error) {
key := Key{}
path := fmt.Sprintf("/repos/%s/%s/keys/%v", owner, repo, id)
if err := r.client.do("GET", path, nil, &key); err != nil {
return nil, err
}
return &key, nil
}
// Gets the key associated with specified title.
func (r *RepoKeyResource) FindName(owner, repo, title string) (*Key, error) {
keys, err := r.List(owner, repo)
if err != nil {
return nil, err
}
for _, key := range keys {
if key.Title == title {
return key, nil
}
}
return nil, ErrNotFound
}
// Creates a key on the specified repo. You must supply a valid key
// that is unique across the Github service.
func (r *RepoKeyResource) Create(owner, repo, key, title string) (*Key, error) {
in := Key{Title: title, Key: key}
out := Key{}
path := fmt.Sprintf("/repos/%s/%s/keys", owner, repo)
if err := r.client.do("POST", path, &in, &out); err != nil {
return nil, err
}
return &out, nil
}
// Updates a key on the specified repo.
func (r *RepoKeyResource) Update(owner, repo, key, title string, id int) (*Key, error) {
out := Key{}
in := Key{
Title: title,
Key: key,
}
path := fmt.Sprintf("/repos/%s/%s/keys/%v", owner, repo, id)
if err := r.client.do("PATCH", path, &in, &out); err != nil {
return nil, err
}
return &out, nil
}
// Creates a key for the specified repo, assuming it does not already
// exist in the system
func (r *RepoKeyResource) CreateUpdate(owner, repo, key, title string) (*Key, error) {
if found, err := r.FindName(owner, repo, title); err == nil {
// if the public keys are different we should update
if found.Key != key {
return r.Update(owner, repo, key, title, found.Id)
}
// otherwise we should just return the key, since there
// is nothing to update
return found, nil
}
return r.Create(owner, repo, key, title)
}
func (r *RepoKeyResource) DeleteName(owner, repo, label string) error {
key, err := r.FindName(owner, repo, label)
if err != nil {
return nil
}
return r.Delete(owner, repo, key.Id)
}
// Deletes the key specified by the id value.
func (r *RepoKeyResource) Delete(owner, repo string, id int) error {
path := fmt.Sprintf("/repos/%s/%s/keys/%v", owner, repo, id)
return r.client.do("DELETE", path, nil, nil)
}