cli/pkg/cmd/ssh-key/shared/user_keys.go
William Martin d4cd79e28c Use int64 for GitHub database IDs and add lint check
Change all struct fields representing GitHub database IDs from int to
int64 to match the API spec and prevent potential overflow on 32-bit
architectures.

Add a custom go/analysis linter (idtype-checker) that flags struct
fields with ID-like names or JSON tags using int instead of int64,
integrated into make lint.

Closes #9247

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-13 13:16:43 +02:00

95 lines
1.8 KiB
Go

package shared
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghinstance"
)
const (
AuthenticationKey = "authentication"
SigningKey = "signing"
)
type sshKey struct {
ID int64
Key string
Title string
Type string
CreatedAt time.Time `json:"created_at"`
}
func UserKeys(httpClient *http.Client, host, userHandle string) ([]sshKey, error) {
resource := "user/keys"
if userHandle != "" {
resource = fmt.Sprintf("users/%s/keys", userHandle)
}
url := fmt.Sprintf("%s%s?per_page=%d", ghinstance.RESTPrefix(host), resource, 100)
keys, err := getUserKeys(httpClient, url)
if err != nil {
return nil, err
}
for i := 0; i < len(keys); i++ {
keys[i].Type = AuthenticationKey
}
return keys, nil
}
func UserSigningKeys(httpClient *http.Client, host, userHandle string) ([]sshKey, error) {
resource := "user/ssh_signing_keys"
if userHandle != "" {
resource = fmt.Sprintf("users/%s/ssh_signing_keys", userHandle)
}
url := fmt.Sprintf("%s%s?per_page=%d", ghinstance.RESTPrefix(host), resource, 100)
keys, err := getUserKeys(httpClient, url)
if err != nil {
return nil, err
}
for i := 0; i < len(keys); i++ {
keys[i].Type = SigningKey
}
return keys, nil
}
func getUserKeys(httpClient *http.Client, url string) ([]sshKey, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
return nil, api.HandleHTTPError(resp)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var keys []sshKey
err = json.Unmarshal(b, &keys)
if err != nil {
return nil, err
}
return keys, nil
}