mirror of
https://github.com/google/go-github.git
synced 2026-10-29 00:44:52 +08:00
BREAKING CHANGE: `RepositoriesService.CreateKey`, `UsersService.CreateKey` and `UsersService.CreateSSHSigningKey` now take `CreateDeployKeyRequest`, `CreateUserKeyRequest` and `CreateSSHSigningKeyRequest` by value.
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
// Copyright 2013 The go-github AUTHORS. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package github
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// The Key type is defined in users_keys.go
|
|
|
|
// ListKeys lists the deploy keys for a repository.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys?apiVersion=2022-11-28#list-deploy-keys
|
|
//
|
|
//meta:operation GET /repos/{owner}/{repo}/keys
|
|
func (s *RepositoriesService) ListKeys(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Key, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
|
|
u, err := addOptions(u, opts)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req, err := s.client.NewRequest(ctx, "GET", u, nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var keys []*Key
|
|
resp, err := s.client.Do(req, &keys)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return keys, resp, nil
|
|
}
|
|
|
|
// GetKey fetches a single deploy key.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys?apiVersion=2022-11-28#get-a-deploy-key
|
|
//
|
|
//meta:operation GET /repos/{owner}/{repo}/keys/{key_id}
|
|
func (s *RepositoriesService) GetKey(ctx context.Context, owner, repo string, id int64) (*Key, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
|
|
|
|
req, err := s.client.NewRequest(ctx, "GET", u, nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var key *Key
|
|
resp, err := s.client.Do(req, &key)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return key, resp, nil
|
|
}
|
|
|
|
// CreateDeployKeyRequest represents a request to create a deploy key.
|
|
type CreateDeployKeyRequest struct {
|
|
Title *string `json:"title,omitempty"`
|
|
Key string `json:"key"`
|
|
ReadOnly *bool `json:"read_only,omitempty"`
|
|
}
|
|
|
|
// CreateKey adds a deploy key for a repository.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys?apiVersion=2022-11-28#create-a-deploy-key
|
|
//
|
|
//meta:operation POST /repos/{owner}/{repo}/keys
|
|
func (s *RepositoriesService) CreateKey(ctx context.Context, owner, repo string, body CreateDeployKeyRequest) (*Key, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
|
|
|
|
req, err := s.client.NewRequest(ctx, "POST", u, body)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var k *Key
|
|
resp, err := s.client.Do(req, &k)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return k, resp, nil
|
|
}
|
|
|
|
// DeleteKey deletes a deploy key.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/deploy-keys/deploy-keys?apiVersion=2022-11-28#delete-a-deploy-key
|
|
//
|
|
//meta:operation DELETE /repos/{owner}/{repo}/keys/{key_id}
|
|
func (s *RepositoriesService) DeleteKey(ctx context.Context, owner, repo string, id int64) (*Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
|
|
|
|
req, err := s.client.NewRequest(ctx, "DELETE", u, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s.client.Do(req, nil)
|
|
}
|