(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 | 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 | } |
no test coverage detected