2019-05-07 03:12:51 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2021-08-10 11:49:43 +02:00
|
|
|
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-deploy-keys
|
2020-07-31 16:22:34 +02:00
|
|
|
func (s *RepositoriesService) ListKeys(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*Key, *Response, error) {
|
2019-05-07 03:12:51 +02:00
|
|
|
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
|
2020-07-31 16:22:34 +02:00
|
|
|
u, err := addOptions(u, opts)
|
2019-05-07 03:12:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []*Key
|
|
|
|
resp, err := s.client.Do(ctx, req, &keys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKey fetches a single deploy key.
|
|
|
|
//
|
2021-08-10 11:49:43 +02:00
|
|
|
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-deploy-key
|
2019-05-07 03:12:51 +02:00
|
|
|
func (s *RepositoriesService) GetKey(ctx context.Context, owner string, repo string, id int64) (*Key, *Response, error) {
|
|
|
|
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := new(Key)
|
|
|
|
resp, err := s.client.Do(ctx, req, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateKey adds a deploy key for a repository.
|
|
|
|
//
|
2021-08-10 11:49:43 +02:00
|
|
|
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-deploy-key
|
2019-05-07 03:12:51 +02:00
|
|
|
func (s *RepositoriesService) CreateKey(ctx context.Context, owner string, repo string, key *Key) (*Key, *Response, error) {
|
|
|
|
u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("POST", u, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k := new(Key)
|
|
|
|
resp, err := s.client.Do(ctx, req, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteKey deletes a deploy key.
|
|
|
|
//
|
2021-08-10 11:49:43 +02:00
|
|
|
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-deploy-key
|
2019-05-07 03:12:51 +02:00
|
|
|
func (s *RepositoriesService) DeleteKey(ctx context.Context, owner string, repo string, id int64) (*Response, error) {
|
|
|
|
u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|