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