Repository navigation
Expand file tree
/
Copy pathdeploy_keys.go
More file actions
108 lines (94 loc) · 2.58 KB
/
Copy pathdeploy_keys.go
File metadata and controls
108 lines (94 loc) · 2.58 KB
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
package github
import (
"context"
forge "github.com/git-pkgs/forge"
"net/http"
"github.com/google/go-github/v82/github"
)
type gitHubDeployKeyService struct {
client *github.Client
}
func (f *gitHubForge) DeployKeys() forge.DeployKeyService {
return &gitHubDeployKeyService{client: f.client}
}
func (s *gitHubDeployKeyService) List(ctx context.Context, owner, repo string, opts forge.ListDeployKeyOpts) ([]forge.DeployKey, error) {
perPage := opts.PerPage
if perPage <= 0 {
perPage = 30
}
page := opts.Page
if page <= 0 {
page = 1
}
ghOpts := &github.ListOptions{PerPage: perPage, Page: page}
var all []forge.DeployKey
for {
keys, resp, err := s.client.Repositories.ListKeys(ctx, owner, repo, ghOpts)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, forge.ErrNotFound
}
return nil, err
}
for _, k := range keys {
all = append(all, forge.DeployKey{
ID: k.GetID(),
Title: k.GetTitle(),
Key: k.GetKey(),
ReadOnly: k.GetReadOnly(),
})
}
if resp.NextPage == 0 || (opts.Limit > 0 && len(all) >= opts.Limit) {
break
}
ghOpts.Page = resp.NextPage
}
if opts.Limit > 0 && len(all) > opts.Limit {
all = all[:opts.Limit]
}
return all, nil
}
func (s *gitHubDeployKeyService) Get(ctx context.Context, owner, repo string, id int64) (*forge.DeployKey, error) {
k, resp, err := s.client.Repositories.GetKey(ctx, owner, repo, id)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, forge.ErrNotFound
}
return nil, err
}
return &forge.DeployKey{
ID: k.GetID(),
Title: k.GetTitle(),
Key: k.GetKey(),
ReadOnly: k.GetReadOnly(),
}, nil
}
func (s *gitHubDeployKeyService) Create(ctx context.Context, owner, repo string, opts forge.CreateDeployKeyOpts) (*forge.DeployKey, error) {
k, resp, err := s.client.Repositories.CreateKey(ctx, owner, repo, &github.Key{
Title: github.Ptr(opts.Title),
Key: github.Ptr(opts.Key),
ReadOnly: github.Ptr(opts.ReadOnly),
})
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, forge.ErrNotFound
}
return nil, err
}
return &forge.DeployKey{
ID: k.GetID(),
Title: k.GetTitle(),
Key: k.GetKey(),
ReadOnly: k.GetReadOnly(),
}, nil
}
func (s *gitHubDeployKeyService) Delete(ctx context.Context, owner, repo string, id int64) error {
resp, err := s.client.Repositories.DeleteKey(ctx, owner, repo, id)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return forge.ErrNotFound
}
return err
}
return nil
}