...

Source file src/github.com/google/go-github/v55/github/users_keys.go

Documentation: github.com/google/go-github/v55/github

     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/en/rest/users/keys#list-public-ssh-keys-for-the-authenticated-user
    34  // GitHub API docs: https://docs.github.com/en/rest/users/keys#list-public-keys-for-a-user
    35  func (s *UsersService) ListKeys(ctx context.Context, user string, opts *ListOptions) ([]*Key, *Response, error) {
    36  	var u string
    37  	if user != "" {
    38  		u = fmt.Sprintf("users/%v/keys", user)
    39  	} else {
    40  		u = "user/keys"
    41  	}
    42  	u, err := addOptions(u, opts)
    43  	if err != nil {
    44  		return nil, nil, err
    45  	}
    46  
    47  	req, err := s.client.NewRequest("GET", u, nil)
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  
    52  	var keys []*Key
    53  	resp, err := s.client.Do(ctx, req, &keys)
    54  	if err != nil {
    55  		return nil, resp, err
    56  	}
    57  
    58  	return keys, resp, nil
    59  }
    60  
    61  // GetKey fetches a single public key.
    62  //
    63  // GitHub API docs: https://docs.github.com/en/rest/users/keys#get-a-public-ssh-key-for-the-authenticated-user
    64  func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, error) {
    65  	u := fmt.Sprintf("user/keys/%v", id)
    66  
    67  	req, err := s.client.NewRequest("GET", u, nil)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	key := new(Key)
    73  	resp, err := s.client.Do(ctx, req, key)
    74  	if err != nil {
    75  		return nil, resp, err
    76  	}
    77  
    78  	return key, resp, nil
    79  }
    80  
    81  // CreateKey adds a public key for the authenticated user.
    82  //
    83  // GitHub API docs: https://docs.github.com/en/rest/users/keys#create-a-public-ssh-key-for-the-authenticated-user
    84  func (s *UsersService) CreateKey(ctx context.Context, key *Key) (*Key, *Response, error) {
    85  	u := "user/keys"
    86  
    87  	req, err := s.client.NewRequest("POST", u, key)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  
    92  	k := new(Key)
    93  	resp, err := s.client.Do(ctx, req, k)
    94  	if err != nil {
    95  		return nil, resp, err
    96  	}
    97  
    98  	return k, resp, nil
    99  }
   100  
   101  // DeleteKey deletes a public key.
   102  //
   103  // GitHub API docs: https://docs.github.com/en/rest/users/keys#delete-a-public-ssh-key-for-the-authenticated-user
   104  func (s *UsersService) DeleteKey(ctx context.Context, id int64) (*Response, error) {
   105  	u := fmt.Sprintf("user/keys/%v", id)
   106  
   107  	req, err := s.client.NewRequest("DELETE", u, nil)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return s.client.Do(ctx, req, nil)
   113  }
   114  

View as plain text