MCPcopy Index your code
hub / github.com/cli/cli / gpgKeyUpload

Function gpgKeyUpload

pkg/cmd/gpg-key/add/http.go:18–69  ·  view source on GitHub ↗
(httpClient *http.Client, hostname string, keyFile io.Reader, title string)

Source from the content-addressed store, hash-verified

16var errWrongFormat = errors.New("key in wrong format")
17
18func gpgKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) error {
19 url := ghinstance.RESTPrefix(hostname) + "user/gpg_keys"
20
21 keyBytes, err := io.ReadAll(keyFile)
22 if err != nil {
23 return err
24 }
25
26 payload := map[string]string{
27 "armored_public_key": string(keyBytes),
28 }
29 if title != "" {
30 payload["name"] = title
31 }
32
33 payloadBytes, err := json.Marshal(payload)
34 if err != nil {
35 return err
36 }
37
38 req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
39 if err != nil {
40 return err
41 }
42
43 resp, err := httpClient.Do(req)
44 if err != nil {
45 return err
46 }
47 defer resp.Body.Close()
48
49 if resp.StatusCode == 404 {
50 return errScopesMissing
51 } else if resp.StatusCode > 299 {
52 err := api.HandleHTTPError(resp)
53 var httpError api.HTTPError
54 if errors.As(err, &httpError) {
55 for _, e := range httpError.Errors {
56 if resp.StatusCode == 422 && e.Field == "key_id" && e.Message == "key_id already exists" {
57 return errDuplicateKey
58 }
59 }
60 }
61 if resp.StatusCode == 422 && !isGpgKeyArmored(keyBytes) {
62 return errWrongFormat
63 }
64 return err
65 }
66
67 _, _ = io.Copy(io.Discard, resp.Body)
68 return nil
69}
70
71func isGpgKeyArmored(keyBytes []byte) bool {
72 return bytes.Contains(keyBytes, []byte("-----BEGIN "))

Callers 1

runAddFunction · 0.85

Calls 6

RESTPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
isGpgKeyArmoredFunction · 0.85
DoMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected