repo_key.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. type DeployKey struct {
  12. ID int64 `json:"id"`
  13. Key string `json:"key"`
  14. URL string `json:"url"`
  15. Title string `json:"title"`
  16. Created time.Time `json:"created_at"`
  17. ReadOnly bool `json:"read_only"`
  18. }
  19. func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) {
  20. keys := make([]*DeployKey, 0, 10)
  21. return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys)
  22. }
  23. func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, error) {
  24. key := new(DeployKey)
  25. return key, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key)
  26. }
  27. type CreateKeyOption struct {
  28. Title string `json:"title" binding:"Required"`
  29. Key string `json:"key" binding:"Required"`
  30. }
  31. func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) {
  32. body, err := json.Marshal(&opt)
  33. if err != nil {
  34. return nil, err
  35. }
  36. key := new(DeployKey)
  37. return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
  38. }
  39. func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {
  40. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil)
  41. return err
  42. }