Add Repo Un-Star Functions (#483)
close #482 Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/483 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
063d97fe74
commit
fd7e38f7b5
2 changed files with 152 additions and 0 deletions
81
gitea/repo_stars.go
Normal file
81
gitea/repo_stars.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
// Copyright 2021 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 (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ListStargazersOptions options for listing a repository's stargazers
|
||||
type ListStargazersOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
// ListRepoStargazers list a repository's stargazers
|
||||
func (c *Client) ListRepoStargazers(user, repo string, opt ListStargazersOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
stargazers := make([]*User, 0, opt.PageSize)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/stargazers?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &stargazers)
|
||||
return stargazers, resp, err
|
||||
}
|
||||
|
||||
// GetStarredRepos returns the repos that the given user has starred
|
||||
func (c *Client) GetStarredRepos(user string) ([]*Repository, *Response, error) {
|
||||
repos := make([]*Repository, 0, 10)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/starred", user), jsonHeader, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// GetMyStarredRepos returns the repos that the authenticated user has starred
|
||||
func (c *Client) GetMyStarredRepos() ([]*Repository, *Response, error) {
|
||||
repos := make([]*Repository, 0, 10)
|
||||
resp, err := c.getParsedResponse("GET", "/user/starred", jsonHeader, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// IsRepoStarring returns whether the authenticated user has starred the repo or not
|
||||
func (c *Client) IsRepoStarring(user, repo string) (bool, *Response, error) {
|
||||
_, resp, err := c.getResponse("GET", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil)
|
||||
if resp != nil {
|
||||
switch resp.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
return false, resp, nil
|
||||
case http.StatusNoContent:
|
||||
return true, resp, nil
|
||||
default:
|
||||
return false, resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
// StarRepo star specified repo as the authenticated user
|
||||
func (c *Client) StarRepo(user, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil)
|
||||
if resp != nil {
|
||||
switch resp.StatusCode {
|
||||
case http.StatusNoContent:
|
||||
return resp, nil
|
||||
default:
|
||||
return resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// UnStarRepo remove star to specified repo as the authenticated user
|
||||
func (c *Client) UnStarRepo(user, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil)
|
||||
if resp != nil {
|
||||
switch resp.StatusCode {
|
||||
case http.StatusNoContent:
|
||||
return resp, nil
|
||||
default:
|
||||
return resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
71
gitea/repo_stars_test.go
Normal file
71
gitea/repo_stars_test.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
// Copyright 2021 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 (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepoStaring(t *testing.T) {
|
||||
log.Println("== TestRepoStaring ==")
|
||||
|
||||
// init user2
|
||||
c := newTestClient()
|
||||
|
||||
user1, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
userA := createTestUser(t, "stargazer_a", c)
|
||||
userB := createTestUser(t, "stargazer_b", c)
|
||||
|
||||
repo, _ := createTestRepo(t, "toStar", c)
|
||||
if repo == nil {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
is, _, err := c.IsRepoStarring(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, is)
|
||||
|
||||
repos, _, err := c.GetMyStarredRepos()
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, repos, 0)
|
||||
|
||||
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
c.SetSudo(userA.UserName)
|
||||
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
c.SetSudo(userB.UserName)
|
||||
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
users, _, err := c.ListRepoStargazers(repo.Owner.UserName, repo.Name, ListStargazersOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, users, 3)
|
||||
assert.EqualValues(t, user1.UserName, users[0].UserName)
|
||||
|
||||
_, err = c.UnStarRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.UnStarRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
c.SetSudo("")
|
||||
|
||||
users, _, err = c.ListRepoStargazers(repo.Owner.UserName, repo.Name, ListStargazersOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, users, 2)
|
||||
|
||||
repos, _, err = c.GetMyStarredRepos()
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, repos, 1)
|
||||
|
||||
reposNew, _, err := c.GetStarredRepos(user1.UserName)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, repos, 1)
|
||||
assert.EqualValues(t, repos, reposNew)
|
||||
}
|
Loading…
Reference in a new issue