|
| 1 | +// Copyright 2021 The Gitea 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 | + |
| 5 | +package gitea |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// ListStargazersOptions options for listing a repository's stargazers |
| 13 | +type ListStargazersOptions struct { |
| 14 | + ListOptions |
| 15 | +} |
| 16 | + |
| 17 | +// ListRepoStargazers list a repository's stargazers |
| 18 | +func (c *Client) ListRepoStargazers(user, repo string, opt ListStargazersOptions) ([]*User, *Response, error) { |
| 19 | + opt.setDefaults() |
| 20 | + stargazers := make([]*User, 0, opt.PageSize) |
| 21 | + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/stargazers?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &stargazers) |
| 22 | + return stargazers, resp, err |
| 23 | +} |
| 24 | + |
| 25 | +// GetStarredRepos returns the repos that the given user has starred |
| 26 | +func (c *Client) GetStarredRepos(user string) ([]*Repository, *Response, error) { |
| 27 | + repos := make([]*Repository, 0, 10) |
| 28 | + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/starred", user), jsonHeader, nil, &repos) |
| 29 | + return repos, resp, err |
| 30 | +} |
| 31 | + |
| 32 | +// GetMyStarredRepos returns the repos that the authenticated user has starred |
| 33 | +func (c *Client) GetMyStarredRepos() ([]*Repository, *Response, error) { |
| 34 | + repos := make([]*Repository, 0, 10) |
| 35 | + resp, err := c.getParsedResponse("GET", "/user/starred", jsonHeader, nil, &repos) |
| 36 | + return repos, resp, err |
| 37 | +} |
| 38 | + |
| 39 | +// IsRepoStarring returns whether the authenticated user has starred the repo or not |
| 40 | +func (c *Client) IsRepoStarring(user, repo string) (bool, *Response, error) { |
| 41 | + _, resp, err := c.getResponse("GET", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil) |
| 42 | + if resp != nil { |
| 43 | + switch resp.StatusCode { |
| 44 | + case http.StatusNotFound: |
| 45 | + return false, resp, nil |
| 46 | + case http.StatusNoContent: |
| 47 | + return true, resp, nil |
| 48 | + default: |
| 49 | + return false, resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) |
| 50 | + } |
| 51 | + } |
| 52 | + return false, nil, err |
| 53 | +} |
| 54 | + |
| 55 | +// StarRepo star specified repo as the authenticated user |
| 56 | +func (c *Client) StarRepo(user, repo string) (*Response, error) { |
| 57 | + _, resp, err := c.getResponse("PUT", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil) |
| 58 | + if resp != nil { |
| 59 | + switch resp.StatusCode { |
| 60 | + case http.StatusNoContent: |
| 61 | + return resp, nil |
| 62 | + default: |
| 63 | + return resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) |
| 64 | + } |
| 65 | + } |
| 66 | + return nil, err |
| 67 | +} |
| 68 | + |
| 69 | +// UnStarRepo remove star to specified repo as the authenticated user |
| 70 | +func (c *Client) UnStarRepo(user, repo string) (*Response, error) { |
| 71 | + _, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/starred/%s/%s", user, repo), jsonHeader, nil) |
| 72 | + if resp != nil { |
| 73 | + switch resp.StatusCode { |
| 74 | + case http.StatusNoContent: |
| 75 | + return resp, nil |
| 76 | + default: |
| 77 | + return resp, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) |
| 78 | + } |
| 79 | + } |
| 80 | + return nil, err |
| 81 | +} |
0 commit comments