| 1 |
// Copyright 2013 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 |
// The Key type is defined in users_keys.go |
| 14 |
|
| 15 |
// ListKeys lists the deploy keys for a repository. |
| 16 |
// |
| 17 |
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys#list-deploy-keys |
| 18 |
// |
| 19 |
//meta:operation GET /repos/{owner}/{repo}/keys |
| 20 |
func (s *RepositoriesService) ListKeys(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*Key, *Response, error) { |
| 21 |
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo) |
| 22 |
u, err := addOptions(u, opts) |
| 23 |
if err != nil { |
| 24 |
return nil, nil, err |
| 25 |
} |
| 26 |
|
| 27 |
req, err := s.client.NewRequest("GET", u, nil) |
| 28 |
if err != nil { |
| 29 |
return nil, nil, err |
| 30 |
} |
| 31 |
|
| 32 |
var keys []*Key |
| 33 |
resp, err := s.client.Do(ctx, req, &keys) |
| 34 |
if err != nil { |
| 35 |
return nil, resp, err |
| 36 |
} |
| 37 |
|
| 38 |
return keys, resp, nil |
| 39 |
} |
| 40 |
|
| 41 |
// GetKey fetches a single deploy key. |
| 42 |
// |
| 43 |
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys#get-a-deploy-key |
| 44 |
// |
| 45 |
//meta:operation GET /repos/{owner}/{repo}/keys/{key_id} |
| 46 |
func (s *RepositoriesService) GetKey(ctx context.Context, owner string, repo string, id int64) (*Key, *Response, error) { |
| 47 |
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id) |
| 48 |
|
| 49 |
req, err := s.client.NewRequest("GET", u, nil) |
| 50 |
if err != nil { |
| 51 |
return nil, nil, err |
| 52 |
} |
| 53 |
|
| 54 |
key := new(Key) |
| 55 |
resp, err := s.client.Do(ctx, req, key) |
| 56 |
if err != nil { |
| 57 |
return nil, resp, err |
| 58 |
} |
| 59 |
|
| 60 |
return key, resp, nil |
| 61 |
} |
| 62 |
|
| 63 |
// CreateKey adds a deploy key for a repository. |
| 64 |
// |
| 65 |
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys#create-a-deploy-key |
| 66 |
// |
| 67 |
//meta:operation POST /repos/{owner}/{repo}/keys |
| 68 |
func (s *RepositoriesService) CreateKey(ctx context.Context, owner string, repo string, key *Key) (*Key, *Response, error) { |
| 69 |
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo) |
| 70 |
|
| 71 |
req, err := s.client.NewRequest("POST", u, key) |
| 72 |
if err != nil { |
| 73 |
return nil, nil, err |
| 74 |
} |
| 75 |
|
| 76 |
k := new(Key) |
| 77 |
resp, err := s.client.Do(ctx, req, k) |
| 78 |
if err != nil { |
| 79 |
return nil, resp, err |
| 80 |
} |
| 81 |
|
| 82 |
return k, resp, nil |
| 83 |
} |
| 84 |
|
| 85 |
// DeleteKey deletes a deploy key. |
| 86 |
// |
| 87 |
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys#delete-a-deploy-key |
| 88 |
// |
| 89 |
//meta:operation DELETE /repos/{owner}/{repo}/keys/{key_id} |
| 90 |
func (s *RepositoriesService) DeleteKey(ctx context.Context, owner string, repo string, id int64) (*Response, error) { |
| 91 |
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id) |
| 92 |
|
| 93 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
|
| 98 |
return s.client.Do(ctx, req, nil) |
| 99 |
} |