MCPcopy
hub / github.com/cli/cli / uploadDeployKey

Function uploadDeployKey

pkg/cmd/repo/deploy-key/add/http.go:15–56  ·  view source on GitHub ↗
(httpClient *http.Client, repo ghrepo.Interface, keyFile io.Reader, title string, isWritable bool)

Source from the content-addressed store, hash-verified

13)
14
15func uploadDeployKey(httpClient *http.Client, repo ghrepo.Interface, keyFile io.Reader, title string, isWritable bool) error {
16 path := fmt.Sprintf("repos/%s/%s/keys", repo.RepoOwner(), repo.RepoName())
17 url := ghinstance.RESTPrefix(repo.RepoHost()) + path
18
19 keyBytes, err := io.ReadAll(keyFile)
20 if err != nil {
21 return err
22 }
23
24 payload := map[string]interface{}{
25 "title": title,
26 "key": string(keyBytes),
27 "read_only": !isWritable,
28 }
29
30 payloadBytes, err := json.Marshal(payload)
31 if err != nil {
32 return err
33 }
34
35 req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
36 if err != nil {
37 return err
38 }
39
40 resp, err := httpClient.Do(req)
41 if err != nil {
42 return err
43 }
44 defer resp.Body.Close()
45
46 if resp.StatusCode > 299 {
47 return api.HandleHTTPError(resp)
48 }
49
50 _, err = io.Copy(io.Discard, resp.Body)
51 if err != nil {
52 return err
53 }
54
55 return nil
56}

Callers 1

addRunFunction · 0.85

Calls 8

RESTPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RepoHostMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected