...

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

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

     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  }
   109  

View as plain text