Refactor List/SetRepoTopics (#276)
Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/276 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
parent
b6504ee1db
commit
ba7916819f
2 changed files with 70 additions and 9 deletions
|
@ -15,27 +15,33 @@ type ListRepoTopicsOptions struct {
|
|||
ListOptions
|
||||
}
|
||||
|
||||
// TopicsList represents a list of repo's topics
|
||||
type TopicsList struct {
|
||||
// topicsList represents a list of repo's topics
|
||||
type topicsList struct {
|
||||
Topics []string `json:"topics"`
|
||||
}
|
||||
|
||||
// ListRepoTopics list all repository's topics
|
||||
func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) (*TopicsList, error) {
|
||||
func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) ([]string, error) {
|
||||
opt.setDefaults()
|
||||
|
||||
var list TopicsList
|
||||
return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &list)
|
||||
list := new(topicsList)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Topics, nil
|
||||
}
|
||||
|
||||
// SetRepoTopics replaces the list of repo's topics
|
||||
func (c *Client) SetRepoTopics(user, repo, list TopicsList) error {
|
||||
body, err := json.Marshal(&list)
|
||||
func (c *Client) SetRepoTopics(user, repo string, list []string) error {
|
||||
|
||||
l := topicsList{Topics: list}
|
||||
|
||||
body, err := json.Marshal(&l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics", user, repo), nil, bytes.NewReader(body))
|
||||
_, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics", user, repo), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
55
gitea/repo_topics_test.go
Normal file
55
gitea/repo_topics_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Copyright 2020 The Gitea 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 (
|
||||
"log"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRepoTopics(t *testing.T) {
|
||||
log.Println("== TestRepoTopics ==")
|
||||
c := newTestClient()
|
||||
repo, err := createTestRepo(t, "RandomTopic", c)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Add
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "git")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "gitea")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Get List
|
||||
tl, err := c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, tl, 4)
|
||||
|
||||
// Del
|
||||
err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
assert.NoError(t, err)
|
||||
err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
assert.NoError(t, err)
|
||||
tl, err = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, tl, 2)
|
||||
|
||||
// Set List
|
||||
newTopics := []string{"analog", "digital", "cat"}
|
||||
err = c.SetRepoTopics(repo.Owner.UserName, repo.Name, newTopics)
|
||||
assert.NoError(t, err)
|
||||
tl, _ = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.Len(t, tl, 3)
|
||||
|
||||
sort.Strings(tl)
|
||||
sort.Strings(newTopics)
|
||||
assert.EqualValues(t, newTopics, tl)
|
||||
}
|
Loading…
Reference in a new issue