diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 9023bcf..8b0bac1 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -11,10 +11,16 @@ import ( "fmt" ) +// AdminListOrgsOptions options for listing admin's organizations +type AdminListOrgsOptions struct { + ListOptions +} + // AdminListOrgs lists all orgs -func (c *Client) AdminListOrgs() ([]*Organization, error) { - orgs := make([]*Organization, 0, 10) - return orgs, c.getParsedResponse("GET", "/admin/orgs", nil, nil, &orgs) +func (c *Client) AdminListOrgs(opt AdminListOrgsOptions) ([]*Organization, error) { + opt.setDefaults() + orgs := make([]*Organization, 0, opt.PageSize) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs) } // AdminCreateOrg create an organization diff --git a/gitea/admin_test.go b/gitea/admin_test.go index ab0248f..40ec11b 100644 --- a/gitea/admin_test.go +++ b/gitea/admin_test.go @@ -29,7 +29,7 @@ func TestAdminOrg(t *testing.T) { assert.NotEmpty(t, newOrg) assert.EqualValues(t, orgName, newOrg.UserName) - orgs, err := c.AdminListOrgs() + orgs, err := c.AdminListOrgs(AdminListOrgsOptions{}) assert.NoError(t, err) assert.Len(t, orgs, 1) assert.EqualValues(t, newOrg.ID, orgs[0].ID) diff --git a/gitea/admin_user.go b/gitea/admin_user.go index 51152dd..c447934 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -11,10 +11,16 @@ import ( "fmt" ) +// AdminListUsersOptions options for listing admin users +type AdminListUsersOptions struct { + ListOptions +} + // AdminListUsers lists all users -func (c *Client) AdminListUsers() ([]*User, error) { - users := make([]*User, 0, 10) - return users, c.getParsedResponse("GET", "/admin/users", nil, nil, &users) +func (c *Client) AdminListUsers(opt AdminListUsersOptions) ([]*User, error) { + opt.setDefaults() + users := make([]*User, 0, opt.PageSize) + return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", opt.getURLQuery().Encode()), nil, nil, &users) } // CreateUserOption create user options diff --git a/gitea/attachment.go b/gitea/attachment.go index ca45195..a13787b 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -24,11 +24,17 @@ type Attachment struct { DownloadURL string `json:"browser_download_url"` } +// ListReleaseAttachmentsOptions options for listing release's attachments +type ListReleaseAttachmentsOptions struct { + ListOptions +} + // ListReleaseAttachments list release's attachments -func (c *Client) ListReleaseAttachments(user, repo string, release int64) ([]*Attachment, error) { - attachments := make([]*Attachment, 0, 10) +func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt ListReleaseAttachmentsOptions) ([]*Attachment, error) { + opt.setDefaults() + attachments := make([]*Attachment, 0, opt.PageSize) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", user, repo, release, opt.getURLQuery().Encode()), nil, nil, &attachments) return attachments, err } diff --git a/gitea/fork.go b/gitea/fork.go index f7e5cc8..fe19746 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -10,10 +10,18 @@ import ( "fmt" ) +// ListForksOptions options for listing repository's forks +type ListForksOptions struct { + ListOptions +} + // ListForks list a repository's forks -func (c *Client) ListForks(user, repo string) ([]*Repository, error) { - forks := make([]*Repository, 10) - return forks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/forks", user, repo), nil, nil, &forks) +func (c *Client) ListForks(user string, repo string, opt ListForksOptions) ([]*Repository, error) { + opt.setDefaults() + forks := make([]*Repository, opt.PageSize) + return forks, c.getParsedResponse("GET", + fmt.Sprintf("/repos/%s/%s/forks?%s", user, repo, opt.getURLQuery().Encode()), + nil, nil, &forks) } // CreateForkOption options for creating a fork diff --git a/gitea/git_hook.go b/gitea/git_hook.go index 089553f..1162d09 100644 --- a/gitea/git_hook.go +++ b/gitea/git_hook.go @@ -17,10 +17,16 @@ type GitHook struct { Content string `json:"content,omitempty"` } +// ListRepoGitHooksOptions options for listing repository's githooks +type ListRepoGitHooksOptions struct { + ListOptions +} + // ListRepoGitHooks list all the Git hooks of one repository -func (c *Client) ListRepoGitHooks(user, repo string) ([]*GitHook, error) { - hooks := make([]*GitHook, 0, 10) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git", user, repo), nil, nil, &hooks) +func (c *Client) ListRepoGitHooks(user, repo string, opt ListRepoGitHooksOptions) ([]*GitHook, error) { + opt.setDefaults() + hooks := make([]*GitHook, 0, opt.PageSize) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks) } // GetRepoGitHook get a Git hook of a repository diff --git a/gitea/hook.go b/gitea/hook.go index 59c3eaf..f89a6e9 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -24,16 +24,23 @@ type Hook struct { Created time.Time `json:"created_at"` } +// ListHooksOptions options for listing hooks +type ListHooksOptions struct { + ListOptions +} + // ListOrgHooks list all the hooks of one organization -func (c *Client) ListOrgHooks(org string) ([]*Hook, error) { - hooks := make([]*Hook, 0, 10) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks", org), nil, nil, &hooks) +func (c *Client) ListOrgHooks(org string, opt ListHooksOptions) ([]*Hook, error) { + opt.setDefaults() + hooks := make([]*Hook, 0, opt.PageSize) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, opt.getURLQuery().Encode()), nil, nil, &hooks) } // ListRepoHooks list all the hooks of one repository -func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) { - hooks := make([]*Hook, 0, 10) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks) +func (c *Client) ListRepoHooks(user, repo string, opt ListHooksOptions) ([]*Hook, error) { + opt.setDefaults() + hooks := make([]*Hook, 0, opt.PageSize) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks) } // GetOrgHook get a hook of an organization diff --git a/gitea/issue.go b/gitea/issue.go index b515ec5..860ba7b 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -55,7 +55,7 @@ type Issue struct { // ListIssueOption list issue options type ListIssueOption struct { - Page int + ListOptions State StateType Labels []string KeyWord string @@ -75,10 +75,7 @@ const ( // QueryEncode turns options into querystring argument func (opt *ListIssueOption) QueryEncode() string { - query := make(url.Values) - if opt.Page > 0 { - query.Add("page", fmt.Sprintf("%d", opt.Page)) - } + query := opt.getURLQuery() if len(opt.State) > 0 { query.Add("state", string(opt.State)) } @@ -101,8 +98,10 @@ func (opt *ListIssueOption) QueryEncode() string { // ListIssues returns all issues assigned the authenticated user func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { + opt.setDefaults() + issues := make([]*Issue, 0, opt.PageSize) + link, _ := url.Parse("/repos/issues/search") - issues := make([]*Issue, 0, 10) link.RawQuery = opt.QueryEncode() err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil { @@ -117,9 +116,11 @@ func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { // ListRepoIssues returns all issues for a given repository func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { + opt.setDefaults() + issues := make([]*Issue, 0, opt.PageSize) + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo)) link.RawQuery = opt.QueryEncode() - issues := make([]*Issue, 0, 10) err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil { for i := 0; i < len(issues); i++ { diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 151dae0..cc8cf9f 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -28,13 +28,14 @@ type Comment struct { // ListIssueCommentOptions list comment options type ListIssueCommentOptions struct { + ListOptions Since time.Time Before time.Time } // QueryEncode turns options into querystring argument func (opt *ListIssueCommentOptions) QueryEncode() string { - query := make(url.Values) + query := opt.getURLQuery() if !opt.Since.IsZero() { query.Add("since", opt.Since.Format(time.RFC3339)) } @@ -46,17 +47,19 @@ func (opt *ListIssueCommentOptions) QueryEncode() string { // ListIssueComments list comments on an issue. func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssueCommentOptions) ([]*Comment, error) { + opt.setDefaults() link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index)) link.RawQuery = opt.QueryEncode() - comments := make([]*Comment, 0, 10) + comments := make([]*Comment, 0, opt.PageSize) return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments) } // ListRepoIssueComments list comments for a given repo. func (c *Client) ListRepoIssueComments(owner, repo string, opt ListIssueCommentOptions) ([]*Comment, error) { + opt.setDefaults() link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo)) link.RawQuery = opt.QueryEncode() - comments := make([]*Comment, 0, 10) + comments := make([]*Comment, 0, opt.PageSize) return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments) } diff --git a/gitea/issue_label.go b/gitea/issue_label.go index c865062..af4b0a8 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -20,10 +20,16 @@ type Label struct { URL string `json:"url"` } +// ListLabelsOptions options for listing repository's labels +type ListLabelsOptions struct { + ListOptions +} + // ListRepoLabels list labels of one repository -func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) { - labels := make([]*Label, 0, 10) - return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels) +func (c *Client) ListRepoLabels(owner, repo string, opt ListLabelsOptions) ([]*Label, error) { + opt.setDefaults() + labels := make([]*Label, 0, opt.PageSize) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &labels) } // GetRepoLabel get one label of repository by repo it @@ -77,9 +83,9 @@ func (c *Client) DeleteLabel(owner, repo string, id int64) error { } // GetIssueLabels get labels of one issue via issue id -func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) { +func (c *Client) GetIssueLabels(owner, repo string, index int64, opts ListLabelsOptions) ([]*Label, error) { labels := make([]*Label, 0, 5) - return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels?%s", owner, repo, index, opts.getURLQuery().Encode()), nil, nil, &labels) } // IssueLabelsOption a collection of labels diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index a174c19..b3faeeb 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -33,13 +33,7 @@ type ListMilestoneOption struct { // QueryEncode turns options into querystring argument func (opt *ListMilestoneOption) QueryEncode() string { - query := make(url.Values) - if opt.Page > 0 { - query.Add("page", fmt.Sprintf("%d", opt.Page)) - } - if opt.PageSize > 0 { - query.Add("limit", fmt.Sprintf("%d", opt.PageSize)) - } + query := opt.getURLQuery() if opt.State != "" { query.Add("state", string(opt.State)) } @@ -48,9 +42,11 @@ func (opt *ListMilestoneOption) QueryEncode() string { // ListRepoMilestones list all the milestones of one repository func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, error) { + opt.setDefaults() + milestones := make([]*Milestone, 0, opt.PageSize) + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/milestones", owner, repo)) link.RawQuery = opt.QueryEncode() - milestones := make([]*Milestone, 0, 10) return milestones, c.getParsedResponse("GET", link.String(), nil, nil, &milestones) } diff --git a/gitea/issue_test.go b/gitea/issue_test.go index f83a382..94e4701 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -18,6 +18,8 @@ func TestIssue(t *testing.T) { c := newTestClient() createIssue(t, c) + // Little sleep in order to give some time for gitea to properly store all information on database. Without this sleep, CI is a bit unstable + time.Sleep(100 * time.Millisecond) editIssues(t, c) listIssues(t, c) } diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index 7eaf27a..51f1a99 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -37,12 +37,6 @@ func (c *Client) GetRepoTrackedTimes(owner, repo string) ([]*TrackedTime, error) return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times", owner, repo), nil, nil, ×) } -// ListTrackedTimes list tracked times of a single issue for a given repository -func (c *Client) ListTrackedTimes(owner, repo string, index int64) ([]*TrackedTime, error) { - times := make([]*TrackedTime, 0, 10) - return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil, ×) -} - // GetMyTrackedTimes list tracked times of the current user func (c *Client) GetMyTrackedTimes() ([]*TrackedTime, error) { times := make([]*TrackedTime, 0, 10) @@ -70,6 +64,18 @@ func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*T jsonHeader, bytes.NewReader(body), t) } +// ListTrackedTimesOptions options for listing repository's tracked times +type ListTrackedTimesOptions struct { + ListOptions +} + +// ListTrackedTimes list tracked times of a single issue for a given repository +func (c *Client) ListTrackedTimes(owner, repo string, index int64, opt ListTrackedTimesOptions) ([]*TrackedTime, error) { + opt.setDefaults() + times := make([]*TrackedTime, 0, opt.PageSize) + return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, ×) +} + // ResetIssueTime reset tracked time of a single issue for a given repository func (c *Client) ResetIssueTime(owner, repo string, index int64) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil) diff --git a/gitea/list_options.go b/gitea/list_options.go index dae89e8..d56c651 100644 --- a/gitea/list_options.go +++ b/gitea/list_options.go @@ -4,8 +4,34 @@ package gitea +import ( + "fmt" + "net/url" +) + +const defaultPageSize = 10 +const maxPageSize = 50 + // ListOptions options for using Gitea's API pagination type ListOptions struct { Page int PageSize int } + +func (o ListOptions) getURLQuery() url.Values { + query := make(url.Values) + query.Add("page", fmt.Sprintf("%d", o.Page)) + query.Add("limit", fmt.Sprintf("%d", o.PageSize)) + + return query +} + +func (o ListOptions) setDefaults() { + if o.Page < 1 { + o.Page = 1 + } + + if o.PageSize < 0 || o.PageSize > maxPageSize { + o.PageSize = defaultPageSize + } +} diff --git a/gitea/org.go b/gitea/org.go index 4b80361..61ade30 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -23,16 +23,23 @@ type Organization struct { Visibility string `json:"visibility"` } +// ListOrgsOptions options for listing organizations +type ListOrgsOptions struct { + ListOptions +} + // ListMyOrgs list all of current user's organizations -func (c *Client) ListMyOrgs() ([]*Organization, error) { - orgs := make([]*Organization, 0, 5) - return orgs, c.getParsedResponse("GET", "/user/orgs", nil, nil, &orgs) +func (c *Client) ListMyOrgs(opt ListOrgsOptions) ([]*Organization, error) { + opt.setDefaults() + orgs := make([]*Organization, 0, opt.PageSize) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs) } // ListUserOrgs list all of some user's organizations -func (c *Client) ListUserOrgs(user string) ([]*Organization, error) { - orgs := make([]*Organization, 0, 5) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs", user), nil, nil, &orgs) +func (c *Client) ListUserOrgs(user string, opt ListOrgsOptions) ([]*Organization, error) { + opt.setDefaults() + orgs := make([]*Organization, 0, opt.PageSize) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, opt.getURLQuery().Encode()), nil, nil, &orgs) } // GetOrg get one organization by name diff --git a/gitea/org_team.go b/gitea/org_team.go index a8d4373..4c786e3 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -22,16 +22,23 @@ type Team struct { Units []string `json:"units"` } +// ListTeamsOptions options for listing teams +type ListTeamsOptions struct { + ListOptions +} + // ListOrgTeams lists all teams of an organization -func (c *Client) ListOrgTeams(org string) ([]*Team, error) { - teams := make([]*Team, 0, 10) - return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams", org), nil, nil, &teams) +func (c *Client) ListOrgTeams(org string, opt ListTeamsOptions) ([]*Team, error) { + opt.setDefaults() + teams := make([]*Team, 0, opt.PageSize) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, opt.getURLQuery().Encode()), nil, nil, &teams) } // ListMyTeams lists all the teams of the current user -func (c *Client) ListMyTeams() ([]*Team, error) { - teams := make([]*Team, 0, 10) - return teams, c.getParsedResponse("GET", "/user/teams", nil, nil, &teams) +func (c *Client) ListMyTeams(opt *ListTeamsOptions) ([]*Team, error) { + opt.setDefaults() + teams := make([]*Team, 0, opt.PageSize) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", opt.getURLQuery().Encode()), nil, nil, &teams) } // GetTeam gets a team by ID @@ -86,10 +93,16 @@ func (c *Client) DeleteTeam(id int64) error { return err } +// ListTeamMembersOptions options for listing team's members +type ListTeamMembersOptions struct { + ListOptions +} + // ListTeamMembers lists all members of a team -func (c *Client) ListTeamMembers(id int64) ([]*User, error) { - members := make([]*User, 0, 10) - return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members", id), nil, nil, &members) +func (c *Client) ListTeamMembers(id int64, opt ListTeamMembersOptions) ([]*User, error) { + opt.setDefaults() + members := make([]*User, 0, opt.PageSize) + return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, opt.getURLQuery().Encode()), nil, nil, &members) } // GetTeamMember gets a member of a team @@ -110,10 +123,16 @@ func (c *Client) RemoveTeamMember(id int64, user string) error { return err } +// ListTeamRepositoriesOptions options for listing team's repositories +type ListTeamRepositoriesOptions struct { + ListOptions +} + // ListTeamRepositories lists all repositories of a team -func (c *Client) ListTeamRepositories(id int64) ([]*Repository, error) { - repos := make([]*Repository, 0, 10) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos", id), nil, nil, &repos) +func (c *Client) ListTeamRepositories(id int64, opt ListTeamRepositoriesOptions) ([]*Repository, error) { + opt.setDefaults() + repos := make([]*Repository, 0, opt.PageSize) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, opt.getURLQuery().Encode()), nil, nil, &repos) } // AddTeamRepository adds a repository to a team diff --git a/gitea/pull.go b/gitea/pull.go index 1faecc7..f8992e1 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -59,23 +59,16 @@ type PullRequest struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - Page int `json:"page"` + ListOptions State StateType `json:"state"` // oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority - Sort string `json:"sort"` - Milestone int64 `json:"milestone"` + Sort string + Milestone int64 } -// ListRepoPullRequests list PRs of one repository -func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { - // declare variables - link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo)) - prs := make([]*PullRequest, 0, 10) - query := make(url.Values) - // add options to query - if opt.Page > 0 { - query.Add("page", fmt.Sprintf("%d", opt.Page)) - } +// QueryEncode turns options into querystring argument +func (opt *ListPullRequestsOptions) QueryEncode() string { + query := opt.getURLQuery() if len(opt.State) > 0 { query.Add("state", string(opt.State)) } @@ -85,8 +78,16 @@ func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOp if opt.Milestone > 0 { query.Add("milestone", fmt.Sprintf("%d", opt.Milestone)) } - link.RawQuery = query.Encode() - // request + return query.Encode() +} + +// ListRepoPullRequests list PRs of one repository +func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { + opt.setDefaults() + prs := make([]*PullRequest, 0, opt.PageSize) + + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo)) + link.RawQuery = opt.QueryEncode() return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs) } diff --git a/gitea/pull_test.go b/gitea/pull_test.go index 5fb85fd..00df2a8 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -25,9 +25,9 @@ func TestPull(t *testing.T) { // ListRepoPullRequests list PRs of one repository pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{ - Page: 1, - State: StateAll, - Sort: "leastupdate", + ListOptions: ListOptions{Page: 1}, + State: StateAll, + Sort: "leastupdate", }) assert.NoError(t, err) assert.Len(t, pulls, 0) diff --git a/gitea/release.go b/gitea/release.go index 20f63f6..fe5c023 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -29,11 +29,17 @@ type Release struct { Attachments []*Attachment `json:"assets"` } +// ListReleasesOptions options for listing repository's releases +type ListReleasesOptions struct { + ListOptions +} + // ListReleases list releases of a repository -func (c *Client) ListReleases(user, repo string) ([]*Release, error) { - releases := make([]*Release, 0, 10) +func (c *Client) ListReleases(user, repo string, opt ListReleasesOptions) ([]*Release, error) { + opt.setDefaults() + releases := make([]*Release, 0, opt.PageSize) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases", user, repo), + fmt.Sprintf("/repos/%s/%s/releases?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &releases) return releases, err } diff --git a/gitea/repo.go b/gitea/repo.go index 8b143f7..309702e 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -57,26 +57,40 @@ type Repository struct { AvatarURL string `json:"avatar_url"` } +// ListReposOptions options for listing repositories +type ListReposOptions struct { + ListOptions +} + // ListMyRepos lists all repositories for the authenticated user that has access to. -func (c *Client) ListMyRepos() ([]*Repository, error) { - repos := make([]*Repository, 0, 10) - return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos) +func (c *Client) ListMyRepos(opt ListReposOptions) ([]*Repository, error) { + opt.setDefaults() + repos := make([]*Repository, 0, opt.PageSize) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", opt.getURLQuery().Encode()), nil, nil, &repos) } // ListUserRepos list all repositories of one user by user's name -func (c *Client) ListUserRepos(user string) ([]*Repository, error) { - repos := make([]*Repository, 0, 10) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos) +func (c *Client) ListUserRepos(user string, opt ListReposOptions) ([]*Repository, error) { + opt.setDefaults() + repos := make([]*Repository, 0, opt.PageSize) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, opt.getURLQuery().Encode()), nil, nil, &repos) +} + +// ListOrgReposOptions options for a organization's repositories +type ListOrgReposOptions struct { + ListOptions } // ListOrgRepos list all repositories of one organization by organization's name -func (c *Client) ListOrgRepos(org string) ([]*Repository, error) { - repos := make([]*Repository, 0, 10) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos) +func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repository, error) { + opt.setDefaults() + repos := make([]*Repository, 0, opt.PageSize) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", org, opt.getURLQuery().Encode()), nil, nil, &repos) } // SearchRepoOptions options for searching repositories type SearchRepoOptions struct { + ListOptions Keyword string Topic bool IncludeDesc bool @@ -92,7 +106,7 @@ type SearchRepoOptions struct { // QueryEncode turns options into querystring argument func (opt *SearchRepoOptions) QueryEncode() string { - query := make(url.Values) + query := opt.getURLQuery() if opt.Keyword != "" { query.Add("q", opt.Keyword) } @@ -134,6 +148,7 @@ type searchRepoResponse struct { // SearchRepos searches for repositories matching the given filters func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) { + opt.setDefaults() resp := new(searchRepoResponse) link, _ := url.Parse("/repos/search") diff --git a/gitea/repo_branch.go b/gitea/repo_branch.go index 2c7e811..adfc6c2 100644 --- a/gitea/repo_branch.go +++ b/gitea/repo_branch.go @@ -49,10 +49,16 @@ type Branch struct { Commit *PayloadCommit `json:"commit"` } +// ListRepoBranchesOptions options for listing a repository's branches +type ListRepoBranchesOptions struct { + ListOptions +} + // ListRepoBranches list all the branches of one repository -func (c *Client) ListRepoBranches(user, repo string) ([]*Branch, error) { - branches := make([]*Branch, 0, 10) - return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches", user, repo), nil, nil, &branches) +func (c *Client) ListRepoBranches(user, repo string, opt ListRepoBranchesOptions) ([]*Branch, error) { + opt.setDefaults() + branches := make([]*Branch, 0, opt.PageSize) + return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &branches) } // GetRepoBranch get one branch's information of one repository diff --git a/gitea/repo_collaborator.go b/gitea/repo_collaborator.go index 646d34f..2ef9f50 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -10,10 +10,18 @@ import ( "fmt" ) +// ListCollaboratorsOptions options for listing a repository's collaborators +type ListCollaboratorsOptions struct { + ListOptions +} + // ListCollaborators list a repository's collaborators -func (c *Client) ListCollaborators(user, repo string) ([]*User, error) { - collaborators := make([]*User, 0, 10) - return collaborators, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/collaborators", user, repo), nil, nil, &collaborators) +func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, error) { + opt.setDefaults() + collaborators := make([]*User, 0, opt.PageSize) + return collaborators, c.getParsedResponse("GET", + fmt.Sprintf("/repos/%s/%s/collaborators?%s", user, repo, opt.getURLQuery().Encode()), + nil, nil, &collaborators) } // IsCollaborator check if a user is a collaborator of a repository diff --git a/gitea/repo_key.go b/gitea/repo_key.go index 29c885e..96a6d93 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -24,10 +24,16 @@ type DeployKey struct { Repository *Repository `json:"repository,omitempty"` } +// ListDeployKeysOptions options for listing a repository's deploy keys +type ListDeployKeysOptions struct { + ListOptions +} + // ListDeployKeys list all the deploy keys of one repository -func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) { - keys := make([]*DeployKey, 0, 10) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys) +func (c *Client) ListDeployKeys(user, repo string, opt ListDeployKeysOptions) ([]*DeployKey, error) { + opt.setDefaults() + keys := make([]*DeployKey, 0, opt.PageSize) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &keys) } // GetDeployKey get one deploy key with key id diff --git a/gitea/repo_tag.go b/gitea/repo_tag.go index cc9c2e3..b64d90b 100644 --- a/gitea/repo_tag.go +++ b/gitea/repo_tag.go @@ -17,8 +17,14 @@ type Tag struct { TarballURL string `json:"tarball_url"` } -// ListRepoTags list all the branches of one repository -func (c *Client) ListRepoTags(user, repo string) ([]*Tag, error) { - tags := make([]*Tag, 0, 10) - return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags", user, repo), nil, nil, &tags) +// ListRepoTagsOptions options for listing a repository's tags +type ListRepoTagsOptions struct { + ListOptions +} + +// ListRepoTags list all the branches of one repository +func (c *Client) ListRepoTags(user, repo string, opt ListRepoTagsOptions) ([]*Tag, error) { + opt.setDefaults() + tags := make([]*Tag, 0, opt.PageSize) + return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &tags) } diff --git a/gitea/repo_topics.go b/gitea/repo_topics.go index 5850568..8facdba 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -10,15 +10,22 @@ import ( "fmt" ) +// ListRepoTopicsOptions options for listing repo's topics +type ListRepoTopicsOptions struct { + ListOptions +} + // 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) (*TopicsList, error) { +func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) (*TopicsList, error) { + opt.setDefaults() + var list TopicsList - return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics", user, repo), nil, nil, &list) + return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &list) } // SetRepoTopics replaces the list of repo's topics diff --git a/gitea/status.go b/gitea/status.go index 43c1486..f072cf8 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -59,15 +59,16 @@ func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) return status, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha), jsonHeader, bytes.NewReader(body), status) } -// ListStatusesOption holds pagination information +// ListStatusesOption options for listing a repository's commit's statuses type ListStatusesOption struct { - Page int + ListOptions } // ListStatuses returns all statuses for a given Commit -func (c *Client) ListStatuses(owner, repo, sha string, opts ListStatusesOption) ([]*Status, error) { - statuses := make([]*Status, 0, 10) - return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?page=%d", owner, repo, sha, opts.Page), nil, nil, &statuses) +func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOption) ([]*Status, error) { + opt.setDefaults() + statuses := make([]*Status, 0, opt.PageSize) + return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, opt.getURLQuery().Encode()), nil, nil, &statuses) } // CombinedStatus holds the combined state of several statuses for a single commit diff --git a/gitea/user_app.go b/gitea/user_app.go index 096393f..bbb3e13 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -26,10 +26,16 @@ type AccessToken struct { TokenLastEight string `json:"token_last_eight"` } -// ListAccessTokens lista all the access tokens of user -func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) { - tokens := make([]*AccessToken, 0, 10) - return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens", user), +// ListAccessTokensOptions options for listing a users's access tokens +type ListAccessTokensOptions struct { + ListOptions +} + +// ListAccessTokens lists all the access tokens of user +func (c *Client) ListAccessTokens(user, pass string, opts ListAccessTokensOptions) ([]*AccessToken, error) { + opts.setDefaults() + tokens := make([]*AccessToken, 0, opts.PageSize) + return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, opts.getURLQuery().Encode()), http.Header{"Authorization": []string{"Basic " + basicAuthEncode(user, pass)}}, nil, &tokens) } diff --git a/gitea/user_email.go b/gitea/user_email.go index fffafe1..5cb4721 100644 --- a/gitea/user_email.go +++ b/gitea/user_email.go @@ -7,6 +7,7 @@ package gitea import ( "bytes" "encoding/json" + "fmt" ) // Email an email address belonging to a user @@ -16,10 +17,16 @@ type Email struct { Primary bool `json:"primary"` } +// ListEmailsOptions options for listing current's user emails +type ListEmailsOptions struct { + ListOptions +} + // ListEmails all the email addresses of user -func (c *Client) ListEmails() ([]*Email, error) { - emails := make([]*Email, 0, 3) - return emails, c.getParsedResponse("GET", "/user/emails", nil, nil, &emails) +func (c *Client) ListEmails(opt ListEmailsOptions) ([]*Email, error) { + opt.setDefaults() + emails := make([]*Email, 0, opt.PageSize) + return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", opt.getURLQuery().Encode()), nil, nil, &emails) } // CreateEmailOption options when creating email addresses diff --git a/gitea/user_follow.go b/gitea/user_follow.go index a197a7f..fdc53ef 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -6,28 +6,42 @@ package gitea import "fmt" +// ListFollowersOptions options for listing followers +type ListFollowersOptions struct { + ListOptions +} + // ListMyFollowers list all the followers of current user -func (c *Client) ListMyFollowers(page int) ([]*User, error) { - users := make([]*User, 0, 10) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?page=%d", page), nil, nil, &users) +func (c *Client) ListMyFollowers(opt ListFollowersOptions) ([]*User, error) { + opt.setDefaults() + users := make([]*User, 0, opt.PageSize) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", opt.getURLQuery().Encode()), nil, nil, &users) } // ListFollowers list all the followers of one user -func (c *Client) ListFollowers(user string, page int) ([]*User, error) { - users := make([]*User, 0, 10) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?page=%d", user, page), nil, nil, &users) +func (c *Client) ListFollowers(user string, opt ListFollowersOptions) ([]*User, error) { + opt.setDefaults() + users := make([]*User, 0, opt.PageSize) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, opt.getURLQuery().Encode()), nil, nil, &users) +} + +// ListFollowingOptions options for listing a user's users being followed +type ListFollowingOptions struct { + ListOptions } // ListMyFollowing list all the users current user followed -func (c *Client) ListMyFollowing(page int) ([]*User, error) { - users := make([]*User, 0, 10) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?page=%d", page), nil, nil, &users) +func (c *Client) ListMyFollowing(opt ListFollowingOptions) ([]*User, error) { + opt.setDefaults() + users := make([]*User, 0, opt.PageSize) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", opt.getURLQuery().Encode()), nil, nil, &users) } // ListFollowing list all the users the user followed -func (c *Client) ListFollowing(user string, page int) ([]*User, error) { - users := make([]*User, 0, 10) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?page=%d", user, page), nil, nil, &users) +func (c *Client) ListFollowing(user string, opt ListFollowingOptions) ([]*User, error) { + opt.setDefaults() + users := make([]*User, 0, opt.PageSize) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", user, opt.getURLQuery().Encode()), nil, nil, &users) } // IsFollowing if current user followed the target diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index 4830525..026cb1a 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -33,16 +33,23 @@ type GPGKeyEmail struct { Verified bool `json:"verified"` } +// ListGPGKeysOptions options for listing a user's GPGKeys +type ListGPGKeysOptions struct { + ListOptions +} + // ListGPGKeys list all the GPG keys of the user -func (c *Client) ListGPGKeys(user string) ([]*GPGKey, error) { - keys := make([]*GPGKey, 0, 10) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys", user), nil, nil, &keys) +func (c *Client) ListGPGKeys(user string, opt ListGPGKeysOptions) ([]*GPGKey, error) { + opt.setDefaults() + keys := make([]*GPGKey, 0, opt.PageSize) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys) } // ListMyGPGKeys list all the GPG keys of current user -func (c *Client) ListMyGPGKeys() ([]*GPGKey, error) { - keys := make([]*GPGKey, 0, 10) - return keys, c.getParsedResponse("GET", "/user/gpg_keys", nil, nil, &keys) +func (c *Client) ListMyGPGKeys(opt *ListGPGKeysOptions) ([]*GPGKey, error) { + opt.setDefaults() + keys := make([]*GPGKey, 0, opt.PageSize) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys) } // GetGPGKey get current user's GPG key by key id diff --git a/gitea/user_key.go b/gitea/user_key.go index 34bbd1a..6c71487 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -24,16 +24,23 @@ type PublicKey struct { KeyType string `json:"key_type,omitempty"` } +// ListPublicKeysOptions options for listing a user's PublicKeys +type ListPublicKeysOptions struct { + ListOptions +} + // ListPublicKeys list all the public keys of the user -func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) { - keys := make([]*PublicKey, 0, 10) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys) +func (c *Client) ListPublicKeys(user string, opt ListPublicKeysOptions) ([]*PublicKey, error) { + opt.setDefaults() + keys := make([]*PublicKey, 0, opt.PageSize) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys) } // ListMyPublicKeys list all the public keys of current user -func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) { - keys := make([]*PublicKey, 0, 10) - return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys) +func (c *Client) ListMyPublicKeys(opt ListPublicKeysOptions) ([]*PublicKey, error) { + opt.setDefaults() + keys := make([]*PublicKey, 0, opt.PageSize) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys) } // GetPublicKey get current user's public key by key id diff --git a/gitea/user_test.go b/gitea/user_test.go index c9ca328..d8549bf 100644 --- a/gitea/user_test.go +++ b/gitea/user_test.go @@ -29,7 +29,7 @@ func TestUserApp(t *testing.T) { log.Println("== TestUserApp ==") c := newTestClient() - result, err := c.ListAccessTokens(c.username, c.password) + result, err := c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{}) assert.NoError(t, err) assert.Len(t, result, 1) assert.EqualValues(t, "gitea-admin", result[0].Name) @@ -37,12 +37,12 @@ func TestUserApp(t *testing.T) { t1, err := c.CreateAccessToken(c.username, c.password, CreateAccessTokenOption{Name: "TestCreateAccessToken"}) assert.NoError(t, err) assert.EqualValues(t, "TestCreateAccessToken", t1.Name) - result, _ = c.ListAccessTokens(c.username, c.password) + result, _ = c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{}) assert.Len(t, result, 2) err = c.DeleteAccessToken(c.username, c.password, t1.ID) assert.NoError(t, err) - result, _ = c.ListAccessTokens(c.username, c.password) + result, _ = c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{}) assert.Len(t, result, 1) } @@ -107,22 +107,22 @@ func TestUserFollow(t *testing.T) { // ListMyFollowers of me c.sudo = "" - f, err := c.ListMyFollowers(1) + f, err := c.ListMyFollowers(ListFollowersOptions{}) assert.NoError(t, err) assert.Len(t, f, 2) // ListFollowers of A - f, err = c.ListFollowers(uA, 1) + f, err = c.ListFollowers(uA, ListFollowersOptions{}) assert.NoError(t, err) assert.Len(t, f, 1) // ListMyFollowing of me - f, err = c.ListMyFollowing(1) + f, err = c.ListMyFollowing(ListFollowingOptions{}) assert.NoError(t, err) assert.Len(t, f, 0) // ListFollowing of A - f, err = c.ListFollowing(uA, 1) + f, err = c.ListFollowing(uA, ListFollowingOptions{}) assert.NoError(t, err) assert.Len(t, f, 1) assert.EqualValues(t, me.ID, f[0].ID) @@ -139,7 +139,7 @@ func TestUserEmail(t *testing.T) { c.sudo = userN // ListEmails - el, err := c.ListEmails() + el, err := c.ListEmails(ListEmailsOptions{}) assert.NoError(t, err) assert.Len(t, el, 1) assert.EqualValues(t, "testuseremail@gitea.io", el[0].Email) @@ -152,7 +152,7 @@ func TestUserEmail(t *testing.T) { assert.Len(t, el, 2) _, err = c.AddEmail(CreateEmailOption{Emails: []string{mails[1]}}) assert.Error(t, err) - el, err = c.ListEmails() + el, err = c.ListEmails(ListEmailsOptions{}) assert.NoError(t, err) assert.Len(t, el, 3) @@ -162,7 +162,7 @@ func TestUserEmail(t *testing.T) { err = c.DeleteEmail(DeleteEmailOption{Emails: []string{"imaginary@e.de"}}) assert.Error(t, err) - el, err = c.ListEmails() + el, err = c.ListEmails(ListEmailsOptions{}) assert.NoError(t, err) assert.Len(t, el, 2) err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})