1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13 func (s *DependabotService) getPublicKey(ctx context.Context, url string) (*PublicKey, *Response, error) {
14 req, err := s.client.NewRequest("GET", url, nil)
15 if err != nil {
16 return nil, nil, err
17 }
18
19 pubKey := new(PublicKey)
20 resp, err := s.client.Do(ctx, req, pubKey)
21 if err != nil {
22 return nil, resp, err
23 }
24
25 return pubKey, resp, nil
26 }
27
28
29
30
31 func (s *DependabotService) GetRepoPublicKey(ctx context.Context, owner, repo string) (*PublicKey, *Response, error) {
32 url := fmt.Sprintf("repos/%v/%v/dependabot/secrets/public-key", owner, repo)
33 return s.getPublicKey(ctx, url)
34 }
35
36
37
38
39 func (s *DependabotService) GetOrgPublicKey(ctx context.Context, org string) (*PublicKey, *Response, error) {
40 url := fmt.Sprintf("orgs/%v/dependabot/secrets/public-key", org)
41 return s.getPublicKey(ctx, url)
42 }
43
44 func (s *DependabotService) listSecrets(ctx context.Context, url string, opts *ListOptions) (*Secrets, *Response, error) {
45 u, err := addOptions(url, opts)
46 if err != nil {
47 return nil, nil, err
48 }
49
50 req, err := s.client.NewRequest("GET", u, nil)
51 if err != nil {
52 return nil, nil, err
53 }
54
55 secrets := new(Secrets)
56 resp, err := s.client.Do(ctx, req, &secrets)
57 if err != nil {
58 return nil, resp, err
59 }
60
61 return secrets, resp, nil
62 }
63
64
65
66
67
68 func (s *DependabotService) ListRepoSecrets(ctx context.Context, owner, repo string, opts *ListOptions) (*Secrets, *Response, error) {
69 url := fmt.Sprintf("repos/%v/%v/dependabot/secrets", owner, repo)
70 return s.listSecrets(ctx, url, opts)
71 }
72
73
74
75
76
77 func (s *DependabotService) ListOrgSecrets(ctx context.Context, org string, opts *ListOptions) (*Secrets, *Response, error) {
78 url := fmt.Sprintf("orgs/%v/dependabot/secrets", org)
79 return s.listSecrets(ctx, url, opts)
80 }
81
82 func (s *DependabotService) getSecret(ctx context.Context, url string) (*Secret, *Response, error) {
83 req, err := s.client.NewRequest("GET", url, nil)
84 if err != nil {
85 return nil, nil, err
86 }
87
88 secret := new(Secret)
89 resp, err := s.client.Do(ctx, req, secret)
90 if err != nil {
91 return nil, resp, err
92 }
93
94 return secret, resp, nil
95 }
96
97
98
99
100 func (s *DependabotService) GetRepoSecret(ctx context.Context, owner, repo, name string) (*Secret, *Response, error) {
101 url := fmt.Sprintf("repos/%v/%v/dependabot/secrets/%v", owner, repo, name)
102 return s.getSecret(ctx, url)
103 }
104
105
106
107
108 func (s *DependabotService) GetOrgSecret(ctx context.Context, org, name string) (*Secret, *Response, error) {
109 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v", org, name)
110 return s.getSecret(ctx, url)
111 }
112
113
114
115
116
117
118 type DependabotEncryptedSecret struct {
119 Name string `json:"-"`
120 KeyID string `json:"key_id"`
121 EncryptedValue string `json:"encrypted_value"`
122 Visibility string `json:"visibility,omitempty"`
123 SelectedRepositoryIDs DependabotSecretsSelectedRepoIDs `json:"selected_repository_ids,omitempty"`
124 }
125
126 func (s *DependabotService) putSecret(ctx context.Context, url string, eSecret *DependabotEncryptedSecret) (*Response, error) {
127 req, err := s.client.NewRequest("PUT", url, eSecret)
128 if err != nil {
129 return nil, err
130 }
131
132 return s.client.Do(ctx, req, nil)
133 }
134
135
136
137
138 func (s *DependabotService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *DependabotEncryptedSecret) (*Response, error) {
139 url := fmt.Sprintf("repos/%v/%v/dependabot/secrets/%v", owner, repo, eSecret.Name)
140 return s.putSecret(ctx, url, eSecret)
141 }
142
143
144
145
146 func (s *DependabotService) CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *DependabotEncryptedSecret) (*Response, error) {
147 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v", org, eSecret.Name)
148 return s.putSecret(ctx, url, eSecret)
149 }
150
151 func (s *DependabotService) deleteSecret(ctx context.Context, url string) (*Response, error) {
152 req, err := s.client.NewRequest("DELETE", url, nil)
153 if err != nil {
154 return nil, err
155 }
156
157 return s.client.Do(ctx, req, nil)
158 }
159
160
161
162
163 func (s *DependabotService) DeleteRepoSecret(ctx context.Context, owner, repo, name string) (*Response, error) {
164 url := fmt.Sprintf("repos/%v/%v/dependabot/secrets/%v", owner, repo, name)
165 return s.deleteSecret(ctx, url)
166 }
167
168
169
170
171 func (s *DependabotService) DeleteOrgSecret(ctx context.Context, org, name string) (*Response, error) {
172 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v", org, name)
173 return s.deleteSecret(ctx, url)
174 }
175
176
177
178
179 func (s *DependabotService) ListSelectedReposForOrgSecret(ctx context.Context, org, name string, opts *ListOptions) (*SelectedReposList, *Response, error) {
180 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v/repositories", org, name)
181 u, err := addOptions(url, opts)
182 if err != nil {
183 return nil, nil, err
184 }
185
186 req, err := s.client.NewRequest("GET", u, nil)
187 if err != nil {
188 return nil, nil, err
189 }
190
191 result := new(SelectedReposList)
192 resp, err := s.client.Do(ctx, req, result)
193 if err != nil {
194 return nil, resp, err
195 }
196
197 return result, resp, nil
198 }
199
200
201 type DependabotSecretsSelectedRepoIDs []string
202
203
204
205
206 func (s *DependabotService) SetSelectedReposForOrgSecret(ctx context.Context, org, name string, ids DependabotSecretsSelectedRepoIDs) (*Response, error) {
207 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v/repositories", org, name)
208 type repoIDs struct {
209 SelectedIDs DependabotSecretsSelectedRepoIDs `json:"selected_repository_ids"`
210 }
211
212 req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids})
213 if err != nil {
214 return nil, err
215 }
216
217 return s.client.Do(ctx, req, nil)
218 }
219
220
221
222
223 func (s *DependabotService) AddSelectedRepoToOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
224 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v/repositories/%v", org, name, *repo.ID)
225 req, err := s.client.NewRequest("PUT", url, nil)
226 if err != nil {
227 return nil, err
228 }
229
230 return s.client.Do(ctx, req, nil)
231 }
232
233
234
235
236 func (s *DependabotService) RemoveSelectedRepoFromOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
237 url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v/repositories/%v", org, name, *repo.ID)
238 req, err := s.client.NewRequest("DELETE", url, nil)
239 if err != nil {
240 return nil, err
241 }
242
243 return s.client.Do(ctx, req, nil)
244 }
245
View as plain text