Debian Codegraph @

github/users_keys.go

raw
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 // Key represents a public SSH key used to authenticate a user or deploy script.
14 type Key struct {
15 ID *int64 `json:"id,omitempty"`
16 Key *string `json:"key,omitempty"`
17 URL *string `json:"url,omitempty"`
18 Title *string `json:"title,omitempty"`
19 ReadOnly *bool `json:"read_only,omitempty"`
20 Verified *bool `json:"verified,omitempty"`
21 CreatedAt *Timestamp `json:"created_at,omitempty"`
22 AddedBy *string `json:"added_by,omitempty"`
23 LastUsed *Timestamp `json:"last_used,omitempty"`
24 }
25
26 func (k Key) String() string {
27 return Stringify(k)
28 }
29
30 // ListKeys lists the verified public keys for a user. Passing the empty
31 // string will fetch keys for the authenticated user.
32 //
33 // GitHub API docs: https://docs.github.com/rest/users/keys#list-public-keys-for-a-user
34 // GitHub API docs: https://docs.github.com/rest/users/keys#list-public-ssh-keys-for-the-authenticated-user
35 //
36 //meta:operation GET /user/keys
37 //meta:operation GET /users/{username}/keys
38 func (s *UsersService) ListKeys(ctx context.Context, user string, opts *ListOptions) ([]*Key, *Response, error) {
39 var u string
40 if user != "" {
41 u = fmt.Sprintf("users/%v/keys", user)
42 } else {
43 u = "user/keys"
44 }
45 u, err := addOptions(u, 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 var keys []*Key
56 resp, err := s.client.Do(ctx, req, &keys)
57 if err != nil {
58 return nil, resp, err
59 }
60
61 return keys, resp, nil
62 }
63
64 // GetKey fetches a single public key.
65 //
66 // GitHub API docs: https://docs.github.com/rest/users/keys#get-a-public-ssh-key-for-the-authenticated-user
67 //
68 //meta:operation GET /user/keys/{key_id}
69 func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, error) {
70 u := fmt.Sprintf("user/keys/%v", id)
71
72 req, err := s.client.NewRequest("GET", u, nil)
73 if err != nil {
74 return nil, nil, err
75 }
76
77 key := new(Key)
78 resp, err := s.client.Do(ctx, req, key)
79 if err != nil {
80 return nil, resp, err
81 }
82
83 return key, resp, nil
84 }
85
86 // CreateKey adds a public key for the authenticated user.
87 //
88 // GitHub API docs: https://docs.github.com/rest/users/keys#create-a-public-ssh-key-for-the-authenticated-user
89 //
90 //meta:operation POST /user/keys
91 func (s *UsersService) CreateKey(ctx context.Context, key *Key) (*Key, *Response, error) {
92 u := "user/keys"
93
94 req, err := s.client.NewRequest("POST", u, key)
95 if err != nil {
96 return nil, nil, err
97 }
98
99 k := new(Key)
100 resp, err := s.client.Do(ctx, req, k)
101 if err != nil {
102 return nil, resp, err
103 }
104
105 return k, resp, nil
106 }
107
108 // DeleteKey deletes a public key.
109 //
110 // GitHub API docs: https://docs.github.com/rest/users/keys#delete-a-public-ssh-key-for-the-authenticated-user
111 //
112 //meta:operation DELETE /user/keys/{key_id}
113 func (s *UsersService) DeleteKey(ctx context.Context, id int64) (*Response, error) {
114 u := fmt.Sprintf("user/keys/%v", id)
115
116 req, err := s.client.NewRequest("DELETE", u, nil)
117 if err != nil {
118 return nil, err
119 }
120
121 return s.client.Do(ctx, req, nil)
122 }