| 12 | ) |
| 13 | |
| 14 | func uploadDeployKey(httpClient *http.Client, repo ghrepo.Interface, keyFile io.Reader, title string, isWritable bool) error { |
| 15 | path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "keys") |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | |
| 20 | keyBytes, err := io.ReadAll(keyFile) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | payload := map[string]any{ |
| 26 | "title": title, |
| 27 | "key": string(keyBytes), |
| 28 | "read_only": !isWritable, |
| 29 | } |
| 30 | |
| 31 | payloadBytes, err := json.Marshal(payload) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | // TODO(api-client-rollout) |
| 37 | // This line of code is part of a mechanical roll out of the api client. |
| 38 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 39 | return api.NewClientFromHTTP(httpClient).REST(repo.RepoHost(), "POST", path.String(), bytes.NewBuffer(payloadBytes), nil) |
| 40 | } |