2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-05-27 09:48:09 -04:00
|
|
|
"net/url"
|
2019-04-25 13:06:53 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeployKey a deploy key
|
|
|
|
type DeployKey struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
KeyID int64 `json:"key_id"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Fingerprint string `json:"fingerprint"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
ReadOnly bool `json:"read_only"`
|
|
|
|
Repository *Repository `json:"repository,omitempty"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// ListDeployKeysOptions options for listing a repository's deploy keys
|
|
|
|
type ListDeployKeysOptions struct {
|
|
|
|
ListOptions
|
|
|
|
KeyID int64
|
|
|
|
Fingerprint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryEncode turns options into querystring argument
|
|
|
|
func (opt *ListDeployKeysOptions) QueryEncode() string {
|
|
|
|
query := opt.getURLQuery()
|
|
|
|
if opt.KeyID > 0 {
|
|
|
|
query.Add("key_id", fmt.Sprintf("%d", opt.KeyID))
|
|
|
|
}
|
|
|
|
if len(opt.Fingerprint) > 0 {
|
|
|
|
query.Add("fingerprint", opt.Fingerprint)
|
|
|
|
}
|
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// ListDeployKeys list all the deploy keys of one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListDeployKeys(user, repo string, opt ListDeployKeysOptions) ([]*DeployKey, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/keys", user, repo))
|
|
|
|
opt.setDefaults()
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
|
|
|
keys := make([]*DeployKey, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &keys)
|
|
|
|
return keys, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeployKey get one deploy key with key id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
key := new(DeployKey)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key)
|
|
|
|
return key, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDeployKey options when create one deploy key
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
key := new(DeployKey)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
|
|
|
|
return key, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDeployKey delete deploy key with key id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|