2020-02-07 08:53:15 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2020-01-03 20:44:33 -05:00
|
|
|
// 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
|
|
|
// ListRepoTopicsOptions options for listing repo's topics
|
|
|
|
type ListRepoTopicsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// topicsList represents a list of repo's topics
|
|
|
|
type topicsList struct {
|
2020-01-03 20:44:33 -05:00
|
|
|
Topics []string `json:"topics"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoTopics list all repository's topics
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) ([]string, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
|
|
|
|
list := new(topicsList)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, list)
|
2020-05-27 09:48:09 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return list.Topics, resp, nil
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRepoTopics replaces the list of repo's topics
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) SetRepoTopics(user, repo string, list []string) (*Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
|
|
|
|
l := topicsList{Topics: list}
|
|
|
|
|
|
|
|
body, err := json.Marshal(&l)
|
2020-01-03 20:44:33 -05:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, err
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics", user, repo), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddRepoTopic adds a topic to a repo's topics list
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) AddRepoTopic(user, repo, topic string) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
|
|
|
return resp, err
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRepoTopic deletes a topic from repo's topics list
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteRepoTopic(user, repo, topic string) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
|
|
|
return resp, err
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|