You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_key.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // DeployKey a deploy key
  12. type DeployKey struct {
  13. ID int64 `json:"id"`
  14. KeyID int64 `json:"key_id"`
  15. Key string `json:"key"`
  16. URL string `json:"url"`
  17. Title string `json:"title"`
  18. Fingerprint string `json:"fingerprint"`
  19. // swagger:strfmt date-time
  20. Created time.Time `json:"created_at"`
  21. ReadOnly bool `json:"read_only"`
  22. Repository *Repository `json:"repository,omitempty"`
  23. }
  24. // ListDeployKeys list all the deploy keys of one repository
  25. func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) {
  26. keys := make([]*DeployKey, 0, 10)
  27. return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys)
  28. }
  29. // GetDeployKey get one deploy key with key id
  30. func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, error) {
  31. key := new(DeployKey)
  32. return key, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key)
  33. }
  34. // CreateKeyOption options when creating a key
  35. type CreateKeyOption struct {
  36. // Title of the key to add
  37. //
  38. // required: true
  39. // unique: true
  40. Title string `json:"title" binding:"Required"`
  41. // An armored SSH key to add
  42. //
  43. // required: true
  44. // unique: true
  45. Key string `json:"key" binding:"Required"`
  46. // Describe if the key has only read access or read/write
  47. //
  48. // required: false
  49. ReadOnly bool `json:"read_only"`
  50. }
  51. // CreateDeployKey options when create one deploy key
  52. func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) {
  53. body, err := json.Marshal(&opt)
  54. if err != nil {
  55. return nil, err
  56. }
  57. key := new(DeployKey)
  58. return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
  59. }
  60. // DeleteDeployKey delete deploy key with key id
  61. func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {
  62. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil)
  63. return err
  64. }