Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

user_key.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gitea
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // PublicKey publickey is a user key to push code to repository
  12. type PublicKey struct {
  13. ID int64 `json:"id"`
  14. Key string `json:"key"`
  15. URL string `json:"url,omitempty"`
  16. Title string `json:"title,omitempty"`
  17. Fingerprint string `json:"fingerprint,omitempty"`
  18. // swagger:strfmt date-time
  19. Created time.Time `json:"created_at,omitempty"`
  20. }
  21. // ListPublicKeys list all the public keys of the user
  22. func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) {
  23. keys := make([]*PublicKey, 0, 10)
  24. return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys)
  25. }
  26. // ListMyPublicKeys list all the public keys of current user
  27. func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) {
  28. keys := make([]*PublicKey, 0, 10)
  29. return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys)
  30. }
  31. // GetPublicKey get current user's public key by key id
  32. func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
  33. key := new(PublicKey)
  34. return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
  35. }
  36. // CreatePublicKey create public key with options
  37. func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
  38. body, err := json.Marshal(&opt)
  39. if err != nil {
  40. return nil, err
  41. }
  42. key := new(PublicKey)
  43. return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
  44. }
  45. // DeletePublicKey delete public key with key id
  46. func (c *Client) DeletePublicKey(keyID int64) error {
  47. _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
  48. return err
  49. }