Skip to content

Commit 10f65ab

Browse files
authored
Add support for User SSH signing keys (#2482)
1 parent 765af9b commit 10f65ab

File tree

7 files changed

+398
-2
lines changed

7 files changed

+398
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Beyang Liu <[email protected]>
5959
Billy Keyes <[email protected]>
6060
Billy Lynch <[email protected]>
6161
Björn Häuser <[email protected]>
62+
Bjorn Neergaard <[email protected]>
6263
6364
Brad Harris <[email protected]>
6465
Brad Moylan <[email protected]>

github/github-accessors.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-stringify_test.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/users_keys_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ func TestUsersService_CreateKey(t *testing.T) {
138138
ctx := context.Background()
139139
key, _, err := client.Users.CreateKey(ctx, input)
140140
if err != nil {
141-
t.Errorf("Users.GetKey returned error: %v", err)
141+
t.Errorf("Users.CreateKey returned error: %v", err)
142142
}
143143

144144
want := &Key{ID: Int64(1)}
145145
if !cmp.Equal(key, want) {
146-
t.Errorf("Users.GetKey returned %+v, want %+v", key, want)
146+
t.Errorf("Users.CreateKey returned %+v, want %+v", key, want)
147147
}
148148

149149
const methodName = "CreateKey"

github/users_ssh_signing_keys.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2022 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+
// SSHSigningKey represents a public SSH key used to sign git commits.
14+
type SSHSigningKey struct {
15+
ID *int64 `json:"id,omitempty"`
16+
Key *string `json:"key,omitempty"`
17+
Title *string `json:"title,omitempty"`
18+
CreatedAt *Timestamp `json:"created_at,omitempty"`
19+
}
20+
21+
func (k SSHSigningKey) String() string {
22+
return Stringify(k)
23+
}
24+
25+
// ListSSHSigningKeys lists the SSH signing keys for a user. Passing an empty
26+
// username string will fetch SSH signing keys for the authenticated user.
27+
//
28+
// GitHub API docs: https://docs.github.com/en/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-the-authenticated-user
29+
// GitHub API docs: https://docs.github.com/en/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-a-user
30+
func (s *UsersService) ListSSHSigningKeys(ctx context.Context, user string, opts *ListOptions) ([]*SSHSigningKey, *Response, error) {
31+
var u string
32+
if user != "" {
33+
u = fmt.Sprintf("users/%v/ssh_signing_keys", user)
34+
} else {
35+
u = "user/ssh_signing_keys"
36+
}
37+
u, err := addOptions(u, opts)
38+
if err != nil {
39+
return nil, nil, err
40+
}
41+
42+
req, err := s.client.NewRequest("GET", u, nil)
43+
if err != nil {
44+
return nil, nil, err
45+
}
46+
47+
var keys []*SSHSigningKey
48+
resp, err := s.client.Do(ctx, req, &keys)
49+
if err != nil {
50+
return nil, resp, err
51+
}
52+
53+
return keys, resp, nil
54+
}
55+
56+
// GetSSHSigningKey fetches a single SSH signing key for the authenticated user.
57+
//
58+
// GitHub API docs: https://docs.github.com/en/rest/users/ssh-signing-keys#get-an-ssh-signing-key-for-the-authenticated-user
59+
func (s *UsersService) GetSSHSigningKey(ctx context.Context, id int64) (*SSHSigningKey, *Response, error) {
60+
u := fmt.Sprintf("user/ssh_signing_keys/%v", id)
61+
62+
req, err := s.client.NewRequest("GET", u, nil)
63+
if err != nil {
64+
return nil, nil, err
65+
}
66+
67+
key := new(SSHSigningKey)
68+
resp, err := s.client.Do(ctx, req, key)
69+
if err != nil {
70+
return nil, resp, err
71+
}
72+
73+
return key, resp, nil
74+
}
75+
76+
// CreateSSHSigningKey adds a SSH signing key for the authenticated user.
77+
//
78+
// GitHub API docs: https://docs.github.com/en/rest/users/ssh-signing-keys#create-a-ssh-signing-key-for-the-authenticated-user
79+
func (s *UsersService) CreateSSHSigningKey(ctx context.Context, key *Key) (*SSHSigningKey, *Response, error) {
80+
u := "user/ssh_signing_keys"
81+
82+
req, err := s.client.NewRequest("POST", u, key)
83+
if err != nil {
84+
return nil, nil, err
85+
}
86+
87+
k := new(SSHSigningKey)
88+
resp, err := s.client.Do(ctx, req, k)
89+
if err != nil {
90+
return nil, resp, err
91+
}
92+
93+
return k, resp, nil
94+
}
95+
96+
// DeleteKey deletes a SSH signing key for the authenticated user.
97+
//
98+
// GitHub API docs: https://docs.github.com/en/rest/users/ssh-signing-keys#delete-an-ssh-signing-key-for-the-authenticated-user
99+
func (s *UsersService) DeleteSSHSigningKey(ctx context.Context, id int64) (*Response, error) {
100+
u := fmt.Sprintf("user/ssh_signing_keys/%v", id)
101+
102+
req, err := s.client.NewRequest("DELETE", u, nil)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
return s.client.Do(ctx, req, nil)
108+
}

0 commit comments

Comments
 (0)