Skip to content

Commit 34cec4f

Browse files
refactor!: Split Key request bodies into CreateDeployKeyRequest, CreateUserKeyRequest and CreateSSHSigningKeyRequest and pass by value (#4477)
BREAKING CHANGE: `RepositoriesService.CreateKey`, `UsersService.CreateKey` and `UsersService.CreateSSHSigningKey` now take `CreateDeployKeyRequest`, `CreateUserKeyRequest` and `CreateSSHSigningKeyRequest` by value.
1 parent 27beda9 commit 34cec4f

11 files changed

Lines changed: 178 additions & 13 deletions

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ linters:
225225
- InstallationTokenListRepoOptions
226226
- InstallationTokenOptions
227227
- IssueImportRequest
228-
- Key
229228
- LockIssueOptions
230229
- MaintenanceOptions
231230
- Organization

github/github-accessors.go

Lines changed: 64 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: 79 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: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_keys.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ func (s *RepositoriesService) GetKey(ctx context.Context, owner, repo string, id
6060
return key, resp, nil
6161
}
6262

63+
// CreateDeployKeyRequest represents a request to create a deploy key.
64+
type CreateDeployKeyRequest struct {
65+
Title *string `json:"title,omitempty"`
66+
Key string `json:"key"`
67+
ReadOnly *bool `json:"read_only,omitempty"`
68+
}
69+
6370
// CreateKey adds a deploy key for a repository.
6471
//
6572
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys?apiVersion=2022-11-28#create-a-deploy-key
6673
//
6774
//meta:operation POST /repos/{owner}/{repo}/keys
68-
func (s *RepositoriesService) CreateKey(ctx context.Context, owner, repo string, body *Key) (*Key, *Response, error) {
75+
func (s *RepositoriesService) CreateKey(ctx context.Context, owner, repo string, body CreateDeployKeyRequest) (*Key, *Response, error) {
6976
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
7077

7178
req, err := s.client.NewRequest(ctx, "POST", u, body)

github/repos_keys_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestRepositoriesService_CreateKey(t *testing.T) {
107107
t.Parallel()
108108
client, mux, _ := setup(t)
109109

110-
input := &Key{Key: Ptr("k"), Title: Ptr("t")}
110+
input := CreateDeployKeyRequest{Key: "k", Title: Ptr("t")}
111111

112112
mux.HandleFunc("/repos/o/r/keys", func(w http.ResponseWriter, r *http.Request) {
113113
testMethod(t, r, "POST")
@@ -146,7 +146,7 @@ func TestRepositoriesService_CreateKey_invalidOwner(t *testing.T) {
146146
client, _, _ := setup(t)
147147

148148
ctx := t.Context()
149-
_, _, err := client.Repositories.CreateKey(ctx, "%", "%", nil)
149+
_, _, err := client.Repositories.CreateKey(ctx, "%", "%", CreateDeployKeyRequest{})
150150
testURLParseError(t, err)
151151
}
152152

github/users_keys.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Key struct {
1717
URL *string `json:"url,omitempty"`
1818
Title *string `json:"title,omitempty"`
1919
ReadOnly *bool `json:"read_only,omitempty"`
20+
Enabled *bool `json:"enabled,omitempty"`
2021
Verified *bool `json:"verified,omitempty"`
2122
CreatedAt *Timestamp `json:"created_at,omitempty"`
2223
AddedBy *string `json:"added_by,omitempty"`
@@ -27,6 +28,13 @@ func (k Key) String() string {
2728
return Stringify(k)
2829
}
2930

31+
// CreateUserKeyRequest represents a request to create a public SSH key for the
32+
// authenticated user.
33+
type CreateUserKeyRequest struct {
34+
Title *string `json:"title,omitempty"`
35+
Key string `json:"key"`
36+
}
37+
3038
// ListKeys lists the verified public keys for a user. Passing the empty
3139
// string will fetch keys for the authenticated user.
3240
//
@@ -89,7 +97,7 @@ func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, e
8997
// GitHub API docs: https://docs.github.com/rest/users/keys?apiVersion=2022-11-28#create-a-public-ssh-key-for-the-authenticated-user
9098
//
9199
//meta:operation POST /user/keys
92-
func (s *UsersService) CreateKey(ctx context.Context, body *Key) (*Key, *Response, error) {
100+
func (s *UsersService) CreateKey(ctx context.Context, body CreateUserKeyRequest) (*Key, *Response, error) {
93101
u := "user/keys"
94102

95103
req, err := s.client.NewRequest(ctx, "POST", u, body)

github/users_keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestUsersService_CreateKey(t *testing.T) {
119119
t.Parallel()
120120
client, mux, _ := setup(t)
121121

122-
input := &Key{Key: Ptr("k"), Title: Ptr("t")}
122+
input := CreateUserKeyRequest{Key: "k", Title: Ptr("t")}
123123

124124
mux.HandleFunc("/user/keys", func(w http.ResponseWriter, r *http.Request) {
125125
testMethod(t, r, "POST")

github/users_ssh_signing_keys.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ func (k SSHSigningKey) String() string {
2222
return Stringify(k)
2323
}
2424

25+
// CreateSSHSigningKeyRequest represents a request to create an SSH signing key
26+
// for the authenticated user.
27+
type CreateSSHSigningKeyRequest struct {
28+
Title *string `json:"title,omitempty"`
29+
Key string `json:"key"`
30+
}
31+
2532
// ListSSHSigningKeys lists the SSH signing keys for a user. Passing an empty
2633
// username string will fetch SSH signing keys for the authenticated user.
2734
//
@@ -84,7 +91,7 @@ func (s *UsersService) GetSSHSigningKey(ctx context.Context, id int64) (*SSHSign
8491
// GitHub API docs: https://docs.github.com/rest/users/ssh-signing-keys?apiVersion=2022-11-28#create-a-ssh-signing-key-for-the-authenticated-user
8592
//
8693
//meta:operation POST /user/ssh_signing_keys
87-
func (s *UsersService) CreateSSHSigningKey(ctx context.Context, body *Key) (*SSHSigningKey, *Response, error) {
94+
func (s *UsersService) CreateSSHSigningKey(ctx context.Context, body CreateSSHSigningKeyRequest) (*SSHSigningKey, *Response, error) {
8895
u := "user/ssh_signing_keys"
8996

9097
req, err := s.client.NewRequest(ctx, "POST", u, body)

github/users_ssh_signing_keys_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestUsersService_CreateSSHSigningKey(t *testing.T) {
119119
t.Parallel()
120120
client, mux, _ := setup(t)
121121

122-
input := &Key{Key: Ptr("k"), Title: Ptr("t")}
122+
input := CreateSSHSigningKeyRequest{Key: "k", Title: Ptr("t")}
123123

124124
mux.HandleFunc("/user/ssh_signing_keys", func(w http.ResponseWriter, r *http.Request) {
125125
testMethod(t, r, "POST")
@@ -138,9 +138,9 @@ func TestUsersService_CreateSSHSigningKey(t *testing.T) {
138138
t.Errorf("Users.CreateSSHSigningKey returned %+v, want %+v", key, want)
139139
}
140140

141-
const methodName = "CreateKey"
141+
const methodName = "CreateSSHSigningKey"
142142
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
143-
got, resp, err := client.Users.CreateKey(ctx, input)
143+
got, resp, err := client.Users.CreateSSHSigningKey(ctx, input)
144144
if got != nil {
145145
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
146146
}

0 commit comments

Comments
 (0)