Debian Codegraph @

github/codespaces_secrets.go

raw
1 // Copyright 2023 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13 // ListUserSecrets list all secrets available for a users codespace
14 //
15 // Lists all secrets available for a user's Codespaces without revealing their encrypted values
16 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint
17 // GitHub Apps must have read access to the codespaces_user_secrets user permission to use this endpoint.
18 //
19 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#list-secrets-for-the-authenticated-user
20 //
21 //meta:operation GET /user/codespaces/secrets
22 func (s *CodespacesService) ListUserSecrets(ctx context.Context, opts *ListOptions) (*Secrets, *Response, error) {
23 u, err := addOptions("user/codespaces/secrets", opts)
24 if err != nil {
25 return nil, nil, err
26 }
27 return s.listSecrets(ctx, u)
28 }
29
30 // ListOrgSecrets list all secrets available to an org
31 //
32 // Lists all Codespaces secrets available at the organization-level without revealing their encrypted values. You must authenticate using an access token with the admin:org scope to use this endpoint.
33 //
34 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#list-organization-secrets
35 //
36 //meta:operation GET /orgs/{org}/codespaces/secrets
37 func (s *CodespacesService) ListOrgSecrets(ctx context.Context, org string, opts *ListOptions) (*Secrets, *Response, error) {
38 u := fmt.Sprintf("orgs/%v/codespaces/secrets", org)
39 u, err := addOptions(u, opts)
40 if err != nil {
41 return nil, nil, err
42 }
43 return s.listSecrets(ctx, u)
44 }
45
46 // ListRepoSecrets list all secrets available to a repo
47 //
48 // Lists all secrets available in a repository without revealing their encrypted values. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have write access to the codespaces_secrets repository permission to use this endpoint.
49 //
50 // GitHub API docs: https://docs.github.com/rest/codespaces/repository-secrets#list-repository-secrets
51 //
52 //meta:operation GET /repos/{owner}/{repo}/codespaces/secrets
53 func (s *CodespacesService) ListRepoSecrets(ctx context.Context, owner, repo string, opts *ListOptions) (*Secrets, *Response, error) {
54 u := fmt.Sprintf("repos/%v/%v/codespaces/secrets", owner, repo)
55 u, err := addOptions(u, opts)
56 if err != nil {
57 return nil, nil, err
58 }
59 return s.listSecrets(ctx, u)
60 }
61
62 func (s *CodespacesService) listSecrets(ctx context.Context, url string) (*Secrets, *Response, error) {
63 req, err := s.client.NewRequest("GET", url, nil)
64 if err != nil {
65 return nil, nil, err
66 }
67
68 var secrets *Secrets
69 resp, err := s.client.Do(ctx, req, &secrets)
70 if err != nil {
71 return nil, resp, err
72 }
73
74 return secrets, resp, nil
75 }
76
77 // GetUserPublicKey gets the users public key for encrypting codespace secrets
78 //
79 // Gets your public key, which you need to encrypt secrets. You need to encrypt a secret before you can create or update secrets.
80 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint.
81 // GitHub Apps must have read access to the codespaces_user_secrets user permission to use this endpoint.
82 //
83 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#get-public-key-for-the-authenticated-user
84 //
85 //meta:operation GET /user/codespaces/secrets/public-key
86 func (s *CodespacesService) GetUserPublicKey(ctx context.Context) (*PublicKey, *Response, error) {
87 return s.getPublicKey(ctx, "user/codespaces/secrets/public-key")
88 }
89
90 // GetOrgPublicKey gets the org public key for encrypting codespace secrets
91 //
92 // Gets a public key for an organization, which is required in order to encrypt secrets. You need to encrypt the value of a secret before you can create or update secrets. You must authenticate using an access token with the admin:org scope to use this endpoint.
93 //
94 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#get-an-organization-public-key
95 //
96 //meta:operation GET /orgs/{org}/codespaces/secrets/public-key
97 func (s *CodespacesService) GetOrgPublicKey(ctx context.Context, org string) (*PublicKey, *Response, error) {
98 return s.getPublicKey(ctx, fmt.Sprintf("orgs/%v/codespaces/secrets/public-key", org))
99 }
100
101 // GetRepoPublicKey gets the repo public key for encrypting codespace secrets
102 //
103 // Gets your public key, which you need to encrypt secrets. You need to encrypt a secret before you can create or update secrets. Anyone with read access to the repository can use this endpoint. If the repository is private you must use an access token with the repo scope. GitHub Apps must have write access to the codespaces_secrets repository permission to use this endpoint.
104 //
105 // GitHub API docs: https://docs.github.com/rest/codespaces/repository-secrets#get-a-repository-public-key
106 //
107 //meta:operation GET /repos/{owner}/{repo}/codespaces/secrets/public-key
108 func (s *CodespacesService) GetRepoPublicKey(ctx context.Context, owner, repo string) (*PublicKey, *Response, error) {
109 return s.getPublicKey(ctx, fmt.Sprintf("repos/%v/%v/codespaces/secrets/public-key", owner, repo))
110 }
111
112 func (s *CodespacesService) getPublicKey(ctx context.Context, url string) (*PublicKey, *Response, error) {
113 req, err := s.client.NewRequest("GET", url, nil)
114 if err != nil {
115 return nil, nil, err
116 }
117
118 var publicKey *PublicKey
119 resp, err := s.client.Do(ctx, req, &publicKey)
120 if err != nil {
121 return nil, resp, err
122 }
123
124 return publicKey, resp, nil
125 }
126
127 // GetUserSecret gets a users codespace secret
128 //
129 // Gets a secret available to a user's codespaces without revealing its encrypted value.
130 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint.
131 // GitHub Apps must have read access to the codespaces_user_secrets user permission to use this endpoint.
132 //
133 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#get-a-secret-for-the-authenticated-user
134 //
135 //meta:operation GET /user/codespaces/secrets/{secret_name}
136 func (s *CodespacesService) GetUserSecret(ctx context.Context, name string) (*Secret, *Response, error) {
137 u := fmt.Sprintf("user/codespaces/secrets/%v", name)
138 return s.getSecret(ctx, u)
139 }
140
141 // GetOrgSecret gets an org codespace secret
142 //
143 // Gets an organization secret without revealing its encrypted value. You must authenticate using an access token with the admin:org scope to use this endpoint.
144 //
145 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#get-an-organization-secret
146 //
147 //meta:operation GET /orgs/{org}/codespaces/secrets/{secret_name}
148 func (s *CodespacesService) GetOrgSecret(ctx context.Context, org, name string) (*Secret, *Response, error) {
149 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v", org, name)
150 return s.getSecret(ctx, u)
151 }
152
153 // GetRepoSecret gets a repo codespace secret
154 //
155 // Gets a single repository secret without revealing its encrypted value. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have write access to the codespaces_secrets repository permission to use this endpoint.
156 //
157 // GitHub API docs: https://docs.github.com/rest/codespaces/repository-secrets#get-a-repository-secret
158 //
159 //meta:operation GET /repos/{owner}/{repo}/codespaces/secrets/{secret_name}
160 func (s *CodespacesService) GetRepoSecret(ctx context.Context, owner, repo, name string) (*Secret, *Response, error) {
161 u := fmt.Sprintf("repos/%v/%v/codespaces/secrets/%v", owner, repo, name)
162 return s.getSecret(ctx, u)
163 }
164
165 func (s *CodespacesService) getSecret(ctx context.Context, url string) (*Secret, *Response, error) {
166 req, err := s.client.NewRequest("GET", url, nil)
167 if err != nil {
168 return nil, nil, err
169 }
170
171 var secret *Secret
172 resp, err := s.client.Do(ctx, req, &secret)
173 if err != nil {
174 return nil, resp, err
175 }
176
177 return secret, resp, nil
178 }
179
180 // CreateOrUpdateUserSecret creates or updates a users codespace secret
181 //
182 // Creates or updates a secret for a user's codespace with an encrypted value. Encrypt your secret using LibSodium.
183 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must also have Codespaces access to use this endpoint.
184 // GitHub Apps must have write access to the codespaces_user_secrets user permission and codespaces_secrets repository permission on all referenced repositories to use this endpoint.
185 //
186 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#create-or-update-a-secret-for-the-authenticated-user
187 //
188 //meta:operation PUT /user/codespaces/secrets/{secret_name}
189 func (s *CodespacesService) CreateOrUpdateUserSecret(ctx context.Context, eSecret *EncryptedSecret) (*Response, error) {
190 u := fmt.Sprintf("user/codespaces/secrets/%v", eSecret.Name)
191 return s.createOrUpdateSecret(ctx, u, eSecret)
192 }
193
194 // CreateOrUpdateOrgSecret creates or updates an orgs codespace secret
195 //
196 // Creates or updates an organization secret with an encrypted value. Encrypt your secret using LibSodium. You must authenticate using an access token with the admin:org scope to use this endpoint.
197 //
198 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#create-or-update-an-organization-secret
199 //
200 //meta:operation PUT /orgs/{org}/codespaces/secrets/{secret_name}
201 func (s *CodespacesService) CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *EncryptedSecret) (*Response, error) {
202 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v", org, eSecret.Name)
203 return s.createOrUpdateSecret(ctx, u, eSecret)
204 }
205
206 // CreateOrUpdateRepoSecret creates or updates a repos codespace secret
207 //
208 // Creates or updates a repository secret with an encrypted value. Encrypt your secret using LibSodium. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have write access to the codespaces_secrets repository permission to use this endpoint.
209 //
210 // GitHub API docs: https://docs.github.com/rest/codespaces/repository-secrets#create-or-update-a-repository-secret
211 //
212 //meta:operation PUT /repos/{owner}/{repo}/codespaces/secrets/{secret_name}
213 func (s *CodespacesService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *EncryptedSecret) (*Response, error) {
214 u := fmt.Sprintf("repos/%v/%v/codespaces/secrets/%v", owner, repo, eSecret.Name)
215 return s.createOrUpdateSecret(ctx, u, eSecret)
216 }
217
218 func (s *CodespacesService) createOrUpdateSecret(ctx context.Context, url string, eSecret *EncryptedSecret) (*Response, error) {
219 req, err := s.client.NewRequest("PUT", url, eSecret)
220 if err != nil {
221 return nil, err
222 }
223
224 resp, err := s.client.Do(ctx, req, nil)
225 if err != nil {
226 return resp, err
227 }
228
229 return resp, nil
230 }
231
232 // DeleteUserSecret deletes a users codespace secret
233 //
234 // Deletes a secret from a user's codespaces using the secret name. Deleting the secret will remove access from all codespaces that were allowed to access the secret.
235 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint.
236 // GitHub Apps must have write access to the codespaces_user_secrets user permission to use this endpoint.
237 //
238 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#delete-a-secret-for-the-authenticated-user
239 //
240 //meta:operation DELETE /user/codespaces/secrets/{secret_name}
241 func (s *CodespacesService) DeleteUserSecret(ctx context.Context, name string) (*Response, error) {
242 u := fmt.Sprintf("user/codespaces/secrets/%v", name)
243 return s.deleteSecret(ctx, u)
244 }
245
246 // DeleteOrgSecret deletes an orgs codespace secret
247 //
248 // Deletes an organization secret using the secret name. You must authenticate using an access token with the admin:org scope to use this endpoint.
249 //
250 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#delete-an-organization-secret
251 //
252 //meta:operation DELETE /orgs/{org}/codespaces/secrets/{secret_name}
253 func (s *CodespacesService) DeleteOrgSecret(ctx context.Context, org, name string) (*Response, error) {
254 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v", org, name)
255 return s.deleteSecret(ctx, u)
256 }
257
258 // DeleteRepoSecret deletes a repos codespace secret
259 //
260 // Deletes a secret in a repository using the secret name. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have write access to the codespaces_secrets repository permission to use this endpoint.
261 //
262 // GitHub API docs: https://docs.github.com/rest/codespaces/repository-secrets#delete-a-repository-secret
263 //
264 //meta:operation DELETE /repos/{owner}/{repo}/codespaces/secrets/{secret_name}
265 func (s *CodespacesService) DeleteRepoSecret(ctx context.Context, owner, repo, name string) (*Response, error) {
266 u := fmt.Sprintf("repos/%v/%v/codespaces/secrets/%v", owner, repo, name)
267 return s.deleteSecret(ctx, u)
268 }
269
270 func (s *CodespacesService) deleteSecret(ctx context.Context, url string) (*Response, error) {
271 req, err := s.client.NewRequest("DELETE", url, nil)
272 if err != nil {
273 return nil, err
274 }
275
276 resp, err := s.client.Do(ctx, req, nil)
277 if err != nil {
278 return resp, err
279 }
280
281 return resp, nil
282 }
283
284 // ListSelectedReposForUserSecret lists the repositories that have been granted the ability to use a user's codespace secret.
285 //
286 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint.
287 // GitHub Apps must have read access to the codespaces_user_secrets user permission and write access to the codespaces_secrets repository permission on all referenced repositories to use this endpoint.
288 //
289 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#list-selected-repositories-for-a-user-secret
290 //
291 //meta:operation GET /user/codespaces/secrets/{secret_name}/repositories
292 func (s *CodespacesService) ListSelectedReposForUserSecret(ctx context.Context, name string, opts *ListOptions) (*SelectedReposList, *Response, error) {
293 u := fmt.Sprintf("user/codespaces/secrets/%v/repositories", name)
294 u, err := addOptions(u, opts)
295 if err != nil {
296 return nil, nil, err
297 }
298
299 return s.listSelectedReposForSecret(ctx, u)
300 }
301
302 // ListSelectedReposForOrgSecret lists the repositories that have been granted the ability to use an organization's codespace secret.
303 //
304 // Lists all repositories that have been selected when the visibility for repository access to a secret is set to selected. You must authenticate using an access token with the admin:org scope to use this endpoint.
305 //
306 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#list-selected-repositories-for-an-organization-secret
307 //
308 //meta:operation GET /orgs/{org}/codespaces/secrets/{secret_name}/repositories
309 func (s *CodespacesService) ListSelectedReposForOrgSecret(ctx context.Context, org, name string, opts *ListOptions) (*SelectedReposList, *Response, error) {
310 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v/repositories", org, name)
311 u, err := addOptions(u, opts)
312 if err != nil {
313 return nil, nil, err
314 }
315
316 return s.listSelectedReposForSecret(ctx, u)
317 }
318
319 func (s *CodespacesService) listSelectedReposForSecret(ctx context.Context, url string) (*SelectedReposList, *Response, error) {
320 req, err := s.client.NewRequest("GET", url, nil)
321 if err != nil {
322 return nil, nil, err
323 }
324
325 var repositories *SelectedReposList
326 resp, err := s.client.Do(ctx, req, &repositories)
327 if err != nil {
328 return nil, resp, err
329 }
330
331 return repositories, resp, nil
332 }
333
334 // SetSelectedReposForUserSecret sets the repositories that have been granted the ability to use a user's codespace secret.
335 //
336 // You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint.
337 // GitHub Apps must have write access to the codespaces_user_secrets user permission and write access to the codespaces_secrets repository permission on all referenced repositories to use this endpoint.
338 //
339 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#set-selected-repositories-for-a-user-secret
340 //
341 //meta:operation PUT /user/codespaces/secrets/{secret_name}/repositories
342 func (s *CodespacesService) SetSelectedReposForUserSecret(ctx context.Context, name string, ids SelectedRepoIDs) (*Response, error) {
343 u := fmt.Sprintf("user/codespaces/secrets/%v/repositories", name)
344 return s.setSelectedRepoForSecret(ctx, u, ids)
345 }
346
347 // SetSelectedReposForOrgSecret sets the repositories that have been granted the ability to use a user's codespace secret.
348 //
349 // Replaces all repositories for an organization secret when the visibility for repository access is set to selected. The visibility is set when you Create or update an organization secret. You must authenticate using an access token with the admin:org scope to use this endpoint.
350 //
351 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#set-selected-repositories-for-an-organization-secret
352 //
353 //meta:operation PUT /orgs/{org}/codespaces/secrets/{secret_name}/repositories
354 func (s *CodespacesService) SetSelectedReposForOrgSecret(ctx context.Context, org, name string, ids SelectedRepoIDs) (*Response, error) {
355 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v/repositories", org, name)
356 return s.setSelectedRepoForSecret(ctx, u, ids)
357 }
358
359 func (s *CodespacesService) setSelectedRepoForSecret(ctx context.Context, url string, ids SelectedRepoIDs) (*Response, error) {
360 type repoIDs struct {
361 SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"`
362 }
363
364 req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids})
365 if err != nil {
366 return nil, err
367 }
368
369 resp, err := s.client.Do(ctx, req, nil)
370 if err != nil {
371 return resp, err
372 }
373
374 return resp, nil
375 }
376
377 // AddSelectedRepoToUserSecret adds a repository to the list of repositories that have been granted the ability to use a user's codespace secret.
378 //
379 // Adds a repository to the selected repositories for a user's codespace secret. You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint. GitHub Apps must have write access to the codespaces_user_secrets user permission and write access to the codespaces_secrets repository permission on the referenced repository to use this endpoint.
380 //
381 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#add-a-selected-repository-to-a-user-secret
382 //
383 //meta:operation PUT /user/codespaces/secrets/{secret_name}/repositories/{repository_id}
384 func (s *CodespacesService) AddSelectedRepoToUserSecret(ctx context.Context, name string, repo *Repository) (*Response, error) {
385 u := fmt.Sprintf("user/codespaces/secrets/%v/repositories/%v", name, *repo.ID)
386 return s.addSelectedRepoToSecret(ctx, u)
387 }
388
389 // AddSelectedRepoToOrgSecret adds a repository to the list of repositories that have been granted the ability to use an organization's codespace secret.
390 //
391 // Adds a repository to an organization secret when the visibility for repository access is set to selected. The visibility is set when you Create or update an organization secret. You must authenticate using an access token with the admin:org scope to use this endpoint.
392 //
393 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#add-selected-repository-to-an-organization-secret
394 //
395 //meta:operation PUT /orgs/{org}/codespaces/secrets/{secret_name}/repositories/{repository_id}
396 func (s *CodespacesService) AddSelectedRepoToOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
397 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v/repositories/%v", org, name, *repo.ID)
398 return s.addSelectedRepoToSecret(ctx, u)
399 }
400
401 func (s *CodespacesService) addSelectedRepoToSecret(ctx context.Context, url string) (*Response, error) {
402 req, err := s.client.NewRequest("PUT", url, nil)
403 if err != nil {
404 return nil, err
405 }
406
407 resp, err := s.client.Do(ctx, req, nil)
408 if err != nil {
409 return resp, err
410 }
411
412 return resp, nil
413 }
414
415 // RemoveSelectedRepoFromUserSecret removes a repository from the list of repositories that have been granted the ability to use a user's codespace secret.
416 //
417 // Removes a repository from the selected repositories for a user's codespace secret. You must authenticate using an access token with the codespace or codespace:secrets scope to use this endpoint. User must have Codespaces access to use this endpoint. GitHub Apps must have write access to the codespaces_user_secrets user permission to use this endpoint.
418 //
419 // GitHub API docs: https://docs.github.com/rest/codespaces/secrets#remove-a-selected-repository-from-a-user-secret
420 //
421 //meta:operation DELETE /user/codespaces/secrets/{secret_name}/repositories/{repository_id}
422 func (s *CodespacesService) RemoveSelectedRepoFromUserSecret(ctx context.Context, name string, repo *Repository) (*Response, error) {
423 u := fmt.Sprintf("user/codespaces/secrets/%v/repositories/%v", name, *repo.ID)
424 return s.removeSelectedRepoFromSecret(ctx, u)
425 }
426
427 // RemoveSelectedRepoFromOrgSecret removes a repository from the list of repositories that have been granted the ability to use an organization's codespace secret.
428 //
429 // Removes a repository from an organization secret when the visibility for repository access is set to selected. The visibility is set when you Create or update an organization secret. You must authenticate using an access token with the admin:org scope to use this endpoint.
430 //
431 // GitHub API docs: https://docs.github.com/rest/codespaces/organization-secrets#remove-selected-repository-from-an-organization-secret
432 //
433 //meta:operation DELETE /orgs/{org}/codespaces/secrets/{secret_name}/repositories/{repository_id}
434 func (s *CodespacesService) RemoveSelectedRepoFromOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
435 u := fmt.Sprintf("orgs/%v/codespaces/secrets/%v/repositories/%v", org, name, *repo.ID)
436 return s.removeSelectedRepoFromSecret(ctx, u)
437 }
438
439 func (s *CodespacesService) removeSelectedRepoFromSecret(ctx context.Context, url string) (*Response, error) {
440 req, err := s.client.NewRequest("DELETE", url, nil)
441 if err != nil {
442 return nil, err
443 }
444
445 resp, err := s.client.Do(ctx, req, nil)
446 if err != nil {
447 return resp, err
448 }
449
450 return resp, nil
451 }