From 2a47a77e4b5695a00ff1bf92f3c1d1d46b68df95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Thu, 19 Dec 2019 22:49:31 +0000 Subject: [PATCH 01/27] added AdminListOrgsOptions --- gitea/admin_org.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 9023bcf..e589fe7 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -11,10 +11,38 @@ import ( "fmt" ) +// AdminListOrgsOptions options for listing admin's organizations +type AdminListOrgsOptions struct { + Page int + Limit int +} + // 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(options *AdminListOrgsOptions) ([]*Organization, error) { + defaultPage := 1 + defaultLimit := 10 + + if options == nil { + options = &AdminListOrgsOptions{ + Page: defaultPage, + Limit: defaultLimit, + } + } + + if options.Page < 1 { + options.Page = defaultPage + } + + if options.Limit < 0 { + options.Limit = defaultLimit + } + + if options.Limit > 50 { + options.Limit = 50 + } + + orgs := make([]*Organization, 0, options.Limit) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?page=%d&limit=%d", options.Page, options.Limit), nil, nil, &orgs) } // AdminCreateOrg create an organization -- 2.40.1 From c3bbf7aad11e155d962119441a2cd11d939d7799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 20 Dec 2019 12:09:52 +0000 Subject: [PATCH 02/27] added ListOptions struct; added pagination options for all list requests --- gitea/admin_org.go | 27 +++-------------- gitea/admin_user.go | 15 ++++++++-- gitea/attachment.go | 14 +++++++-- gitea/fork.go | 13 +++++++-- gitea/git_hook.go | 13 +++++++-- gitea/hook.go | 25 ++++++++++++---- gitea/issue.go | 37 ++++++++++++++--------- gitea/issue_comment.go | 27 +++++++++++++---- gitea/issue_label.go | 14 +++++++-- gitea/issue_milestone.go | 13 +++++++-- gitea/issue_tracked_time.go | 16 +++++++--- gitea/list_options.go | 33 +++++++++++++++++++++ gitea/org.go | 27 +++++++++++++---- gitea/org_team.go | 51 ++++++++++++++++++++++++-------- gitea/pull.go | 14 +++++---- gitea/release.go | 13 +++++++-- gitea/repo.go | 39 +++++++++++++++++++------ gitea/repo_branch.go | 13 +++++++-- gitea/repo_collaborator.go | 13 +++++++-- gitea/repo_key.go | 13 +++++++-- gitea/repo_tag.go | 15 +++++++--- gitea/status.go | 15 ++++++---- gitea/user_app.go | 15 +++++++--- gitea/user_email.go | 12 ++++++-- gitea/user_follow.go | 58 +++++++++++++++++++++++++++++-------- gitea/user_gpgkey.go | 27 +++++++++++++---- gitea/user_key.go | 27 +++++++++++++---- 27 files changed, 443 insertions(+), 156 deletions(-) create mode 100644 gitea/list_options.go diff --git a/gitea/admin_org.go b/gitea/admin_org.go index e589fe7..220c689 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -13,36 +13,17 @@ import ( // AdminListOrgsOptions options for listing admin's organizations type AdminListOrgsOptions struct { - Page int - Limit int + ListOptions } // AdminListOrgs lists all orgs func (c *Client) AdminListOrgs(options *AdminListOrgsOptions) ([]*Organization, error) { - defaultPage := 1 - defaultLimit := 10 - if options == nil { - options = &AdminListOrgsOptions{ - Page: defaultPage, - Limit: defaultLimit, - } + options = &AdminListOrgsOptions{} } - if options.Page < 1 { - options.Page = defaultPage - } - - if options.Limit < 0 { - options.Limit = defaultLimit - } - - if options.Limit > 50 { - options.Limit = 50 - } - - orgs := make([]*Organization, 0, options.Limit) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?page=%d&limit=%d", options.Page, options.Limit), nil, nil, &orgs) + orgs := make([]*Organization, 0, options.getPerPage()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", options.getURLQuery()), nil, nil, &orgs) } // AdminCreateOrg create an organization diff --git a/gitea/admin_user.go b/gitea/admin_user.go index 51152dd..2933670 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -11,10 +11,19 @@ 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(options *AdminListUsersOptions) ([]*User, error) { + if options == nil { + options = &AdminListUsersOptions{} + } + + users := make([]*User, 0, options.getPerPage()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", options.getURLQuery()), nil, nil, &users) } // CreateUserOption create user options diff --git a/gitea/attachment.go b/gitea/attachment.go index ca45195..f637961 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -24,11 +24,19 @@ type Attachment struct { DownloadURL string `json:"browser_download_url"` } +// ListReleaseAttachmentsOptions options for listing release's attachments +type ListReleaseAttachmentsOptions struct { + ListOptions + User string + Repo string + Release int64 +} + // 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(options ListReleaseAttachmentsOptions) ([]*Attachment, error) { + attachments := make([]*Attachment, 0, options.getPerPage()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", options.User, options.Repo, options.Release, options.getURLQuery()), nil, nil, &attachments) return attachments, err } diff --git a/gitea/fork.go b/gitea/fork.go index 5722249..fb431d0 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -10,11 +10,18 @@ import ( "fmt" ) +// ListForksOptions options for listing repository's forks +type ListForksOptions struct { + ListOptions + User string + Repo string +} + // ListForks list a repository's forks -func (c *Client) ListForks(user, repo string) ([]*Repository, error) { - forks := make([]*Repository, 10) +func (c *Client) ListForks(options ListForksOptions) ([]*Repository, error) { + forks := make([]*Repository, options.getPerPage()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/forks", user, repo), + fmt.Sprintf("/repos/%s/%s/forks?%s", options.User, options.Repo, options.getURLQuery()), nil, nil, &forks) return forks, err } diff --git a/gitea/git_hook.go b/gitea/git_hook.go index 089553f..0575fd2 100644 --- a/gitea/git_hook.go +++ b/gitea/git_hook.go @@ -17,10 +17,17 @@ type GitHook struct { Content string `json:"content,omitempty"` } +// ListRepoGitHooksOptions options for listing repository's githooks +type ListRepoGitHooksOptions struct { + ListOptions + User string + Repo string +} + // 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(options ListRepoGitHooksOptions) ([]*GitHook, error) { + hooks := make([]*GitHook, 0, options.getPerPage()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", options.User, options.Repo, options.getURLQuery()), nil, nil, &hooks) } // GetRepoGitHook get a Git hook of a repository diff --git a/gitea/hook.go b/gitea/hook.go index 59c3eaf..68a6d6c 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -24,16 +24,29 @@ type Hook struct { Created time.Time `json:"created_at"` } +// ListOrgHooksOptions options for listing organization's hooks +type ListOrgHooksOptions struct { + ListOptions + Org string +} + // 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(options ListOrgHooksOptions) ([]*Hook, error) { + hooks := make([]*Hook, 0, options.getPerPage()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", options.Org, options.getURLQuery()), nil, nil, &hooks) +} + +// ListRepoHooksOptions options for listing repository's hooks +type ListRepoHooksOptions struct { + ListOptions + User string + Repo string } // 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(options ListRepoHooksOptions) ([]*Hook, error) { + hooks := make([]*Hook, 0, options.getPerPage()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", options.User, options.Repo, options.getURLQuery()), nil, nil, &hooks) } // GetOrgHook get a hook of an organization diff --git a/gitea/issue.go b/gitea/issue.go index 35c1a1b..1c8db22 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -42,28 +42,39 @@ type Issue struct { PullRequest *PullRequestMeta `json:"pull_request"` } -// ListIssueOption list issue options -type ListIssueOption struct { - Page int - State string +// ListIssuesOptions options for listing issues +type ListIssuesOptions struct { + ListOptions } // ListIssues returns all issues assigned the authenticated user -func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { - issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/issues?page=%d", opt.Page), nil, nil, &issues) +func (c *Client) ListIssues(options ListIssuesOptions) ([]*Issue, error) { + issues := make([]*Issue, 0, options.getPerPage()) + return issues, c.getParsedResponse("GET", fmt.Sprintf("/issues?%s", options.getURLQuery()), nil, nil, &issues) +} + +// ListUserIssuesOptions options for listing user's issues +type ListUserIssuesOptions struct { + ListOptions } // ListUserIssues returns all issues assigned to the authenticated user -func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { - issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues) +func (c *Client) ListUserIssues(options ListUserIssuesOptions) ([]*Issue, error) { + issues := make([]*Issue, 0, options.getPerPage()) + return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?%s", options.getURLQuery()), nil, nil, &issues) +} + +// ListRepoIssuesOptions options for listing repository's issues +type ListRepoIssuesOptions struct { + ListOptions + Owner string + Repo string } // ListRepoIssues returns all issues for a given repository -func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { - issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues) +func (c *Client) ListRepoIssues(options ListRepoIssuesOptions) ([]*Issue, error) { + issues := make([]*Issue, 0, options.getPerPage()) + return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?%s", options.Owner, options.Repo, options.getURLQuery()), nil, nil, &issues) } // GetIssue returns a single issue for a given repository diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 119f7e5..6336d30 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -25,16 +25,31 @@ type Comment struct { Updated time.Time `json:"updated_at"` } +// ListIssueCommentsOptions options for listing issue's comments +type ListIssueCommentsOptions struct { + ListOptions + Owner string + Repo string + Index int64 +} + // ListIssueComments list comments on an issue. -func (c *Client) ListIssueComments(owner, repo string, index int64) ([]*Comment, error) { - comments := make([]*Comment, 0, 10) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), nil, nil, &comments) +func (c *Client) ListIssueComments(options ListIssueCommentsOptions) ([]*Comment, error) { + comments := make([]*Comment, 0, options.getPerPage()) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", options.Owner, options.Repo, options.Index, options.getURLQuery()), nil, nil, &comments) +} + +// ListRepoIssueCommentsOptions options for listing repository's issue's comments +type ListRepoIssueCommentsOptions struct { + ListOptions + Owner string + Repo string } // ListRepoIssueComments list comments for a given repo. -func (c *Client) ListRepoIssueComments(owner, repo string) ([]*Comment, error) { - comments := make([]*Comment, 0, 10) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo), nil, nil, &comments) +func (c *Client) ListRepoIssueComments(options ListRepoIssueCommentsOptions) ([]*Comment, error) { + comments := make([]*Comment, 0, options.getPerPage()) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", options.Owner, options.Repo, options.getURLQuery()), nil, nil, &comments) } // CreateIssueCommentOption options for creating a comment on an issue diff --git a/gitea/issue_label.go b/gitea/issue_label.go index c865062..e573a5d 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -20,10 +20,17 @@ type Label struct { URL string `json:"url"` } +// ListRepoLabelsOptions options for listing repository's labels +type ListRepoLabelsOptions struct { + ListOptions + Owner string + Repo string +} + // 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(options ListRepoLabelsOptions) ([]*Label, error) { + labels := make([]*Label, 0, options.getPerPage()) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", options.Owner, options.Repo, options.getURLQuery()), nil, nil, &labels) } // GetRepoLabel get one label of repository by repo it @@ -77,6 +84,7 @@ func (c *Client) DeleteLabel(owner, repo string, id int64) error { } // GetIssueLabels get labels of one issue via issue id +// TODO: is this a "ListIssueLabels" request? If so, add ListOptions func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*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) diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index f316e17..e448fdc 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -35,10 +35,17 @@ type Milestone struct { Deadline *time.Time `json:"due_on"` } +// ListRepoMilestonesOptions options for listing repository's milestones +type ListRepoMilestonesOptions struct { + ListOptions + Owner string + Repo string +} + // ListRepoMilestones list all the milestones of one repository -func (c *Client) ListRepoMilestones(owner, repo string) ([]*Milestone, error) { - milestones := make([]*Milestone, 0, 10) - return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), nil, nil, &milestones) +func (c *Client) ListRepoMilestones(options ListRepoMilestonesOptions) ([]*Milestone, error) { + milestones := make([]*Milestone, 0, options.getPerPage()) + return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones?%s", options.Owner, options.Repo, options.getURLQuery()), nil, nil, &milestones) } // GetMilestone get one milestone by repo name and milestone id diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index faec0a0..88eaff0 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -56,8 +56,16 @@ func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*T jsonHeader, bytes.NewReader(body), t) } -// ListTrackedTimes get tracked times of one issue via issue id -func (c *Client) ListTrackedTimes(owner, repo string, index int64) ([]*TrackedTime, error) { - times := make([]*TrackedTime, 0, 5) - return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil, ×) +// ListTrackedTimesOptions options for listing repository's tracked times +type ListTrackedTimesOptions struct { + ListOptions + Owner string + Repo string + Index int64 +} + +// ListTrackedTimes get tracked times of one issue via issue id +func (c *Client) ListTrackedTimes(options ListTrackedTimesOptions) ([]*TrackedTime, error) { + times := make([]*TrackedTime, 0, options.getPerPage()) + return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", options.Owner, options.Repo, options.Index, options.getURLQuery()), nil, nil, ×) } diff --git a/gitea/list_options.go b/gitea/list_options.go new file mode 100644 index 0000000..d414d87 --- /dev/null +++ b/gitea/list_options.go @@ -0,0 +1,33 @@ +package gitea + +import "fmt" + +// ListOptions options for using Gitea's API pagination +type ListOptions struct { + Page int + PerPage int +} + +func (o ListOptions) getURLQuery() string { + o.setDefaults() + + return fmt.Sprintf("page=%d&limit=%d", o.Page, o.PerPage) +} + +func (o ListOptions) setDefaults() { + if o.Page < 1 { + o.Page = 1 + } + + if o.PerPage < 0 || o.PerPage > 50 { + o.PerPage = 10 + } +} + +func (o ListOptions) getPerPage() int { + if o.PerPage < 0 || o.PerPage > 50 { + o.PerPage = 10 + } + + return o.PerPage +} diff --git a/gitea/org.go b/gitea/org.go index 4b80361..bf3e031 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -23,16 +23,31 @@ type Organization struct { Visibility string `json:"visibility"` } +// ListMyOrgsOptions options for listing current user's organizations +type ListMyOrgsOptions 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(options *ListMyOrgsOptions) ([]*Organization, error) { + if options == nil { + options = &ListMyOrgsOptions{} + } + + orgs := make([]*Organization, 0, options.getPerPage()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQuery()), nil, nil, &orgs) +} + +// ListUserOrgsOptions options for listing an user's organizations +type ListUserOrgsOptions struct { + ListOptions + User string } // 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(options ListUserOrgsOptions) ([]*Organization, error) { + orgs := make([]*Organization, 0, options.getPerPage()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", options.User, options.getURLQuery()), nil, nil, &orgs) } // GetOrg get one organization by name diff --git a/gitea/org_team.go b/gitea/org_team.go index a8d4373..be6f320 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -22,16 +22,31 @@ type Team struct { Units []string `json:"units"` } +// ListOrgTeamsOptions options for listing organization's teams +type ListOrgTeamsOptions struct { + ListOptions + Org string +} + // 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(options ListOrgTeamsOptions) ([]*Team, error) { + teams := make([]*Team, 0, options.getPerPage()) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", options.Org, options.getURLQuery()), nil, nil, &teams) +} + +// ListMyTeamsOptions options for current's user teams +type ListMyTeamsOptions struct { + ListOptions } // 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(options *ListMyOrgsOptions) ([]*Team, error) { + if options == nil { + options = &ListMyOrgsOptions{} + } + + teams := make([]*Team, 0, options.getPerPage()) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQuery()), nil, nil, &teams) } // GetTeam gets a team by ID @@ -86,10 +101,16 @@ func (c *Client) DeleteTeam(id int64) error { return err } +// ListTeamMembersOptions options for listing team's members +type ListTeamMembersOptions struct { + ListOptions + ID int64 +} + // 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(options ListTeamMembersOptions) ([]*User, error) { + members := make([]*User, 0, options.getPerPage()) + return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", options.ID, options.getURLQuery()), nil, nil, &members) } // GetTeamMember gets a member of a team @@ -110,10 +131,16 @@ func (c *Client) RemoveTeamMember(id int64, user string) error { return err } +// ListTeamRepositoriesOptions options for listing team's repositories +type ListTeamRepositoriesOptions struct { + ListOptions + ID int64 +} + // 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(options ListTeamRepositoriesOptions) ([]*Repository, error) { + repos := make([]*Repository, 0, options.getPerPage()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", options.ID, options.getURLQuery()), nil, nil, &repos) } // AddTeamRepository adds a repository to a team diff --git a/gitea/pull.go b/gitea/pull.go index 0e03a05..8e0206e 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -57,18 +57,20 @@ type PullRequest struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - Page int `json:"page"` - State string `json:"state"` + ListOptions `json:"-"` + Owner string `json:"-"` + Repo string `json:"-"` + State string `json:"state"` } // ListRepoPullRequests list PRs of one repository -func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { - body, err := json.Marshal(&opt) +func (c *Client) ListRepoPullRequests(options ListPullRequestsOptions) ([]*PullRequest, error) { + body, err := json.Marshal(&options) if err != nil { return nil, err } - prs := make([]*PullRequest, 0, 10) - return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs) + prs := make([]*PullRequest, 0, options.getPerPage()) + return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls?%s", options.Owner, options.Repo, options.getURLQuery()), jsonHeader, bytes.NewReader(body), &prs) } // GetPullRequest get information of one PR diff --git a/gitea/release.go b/gitea/release.go index 20f63f6..ade1923 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -29,11 +29,18 @@ type Release struct { Attachments []*Attachment `json:"assets"` } +// ListReleasesOptions options for listing repository's releases +type ListReleasesOptions struct { + ListOptions + User string + Repo string +} + // ListReleases list releases of a repository -func (c *Client) ListReleases(user, repo string) ([]*Release, error) { - releases := make([]*Release, 0, 10) +func (c *Client) ListReleases(options ListReleasesOptions) ([]*Release, error) { + releases := make([]*Release, 0, options.getPerPage()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases", user, repo), + fmt.Sprintf("/repos/%s/%s/releases?%s", options.User, options.Repo, options.getURLQuery()), nil, nil, &releases) return releases, err } diff --git a/gitea/repo.go b/gitea/repo.go index 4556f17..b206e62 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -56,22 +56,43 @@ type Repository struct { AvatarURL string `json:"avatar_url"` } +// ListMyReposOptions options for listing current's user repositories +type ListMyReposOptions 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(options *ListMyReposOptions) ([]*Repository, error) { + if options == nil { + options = &ListMyReposOptions{} + } + + repos := make([]*Repository, 0, options.getPerPage()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQuery()), nil, nil, &repos) +} + +// ListUserReposOptions options for listing a user's repositories +type ListUserReposOptions struct { + ListOptions + User string } // 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(options ListUserReposOptions) ([]*Repository, error) { + repos := make([]*Repository, 0, options.getPerPage()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", options.User, options.getURLQuery()), nil, nil, &repos) +} + +// ListOrgReposOptions options for a organization's repositories +type ListOrgReposOptions struct { + ListOptions + Org string } // 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(options ListOrgReposOptions) ([]*Repository, error) { + repos := make([]*Repository, 0, options.getPerPage()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", options.Org, options.getURLQuery()), nil, nil, &repos) } // CreateRepoOption options when creating repository diff --git a/gitea/repo_branch.go b/gitea/repo_branch.go index 2c7e811..30f9edf 100644 --- a/gitea/repo_branch.go +++ b/gitea/repo_branch.go @@ -49,10 +49,17 @@ type Branch struct { Commit *PayloadCommit `json:"commit"` } +// ListRepoBranchesOptions options for listing a repository's branches +type ListRepoBranchesOptions struct { + ListOptions + User string + Repo string +} + // 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(options ListRepoBranchesOptions) ([]*Branch, error) { + branches := make([]*Branch, 0, options.getPerPage()) + return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", options.User, options.Repo, options.getURLQuery()), 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 bd61a22..77ddff9 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -10,11 +10,18 @@ import ( "fmt" ) +// ListCollaboratorsOptions options for listing a repository's collaborators +type ListCollaboratorsOptions struct { + ListOptions + User string + Repo string +} + // ListCollaborators list a repository's collaborators -func (c *Client) ListCollaborators(user, repo string) ([]*User, error) { - collaborators := make([]*User, 0, 10) +func (c *Client) ListCollaborators(options ListCollaboratorsOptions) ([]*User, error) { + collaborators := make([]*User, 0, options.getPerPage()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/collaborators", user, repo), + fmt.Sprintf("/repos/%s/%s/collaborators", options.User, options.Repo), nil, nil, &collaborators) return collaborators, err } diff --git a/gitea/repo_key.go b/gitea/repo_key.go index 29c885e..4b95e34 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -24,10 +24,17 @@ type DeployKey struct { Repository *Repository `json:"repository,omitempty"` } +// ListDeployKeysOptions options for listing a repository's deploy keys +type ListDeployKeysOptions struct { + ListOptions + User string + Repo string +} + // 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(options ListDeployKeysOptions) ([]*DeployKey, error) { + keys := make([]*DeployKey, 0, options.getPerPage()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", options.User, options.Repo, options.getURLQuery()), 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..4c0e6cc 100644 --- a/gitea/repo_tag.go +++ b/gitea/repo_tag.go @@ -17,8 +17,15 @@ 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 + User string + Repo string +} + +// ListRepoTags list all the branches of one repository +func (c *Client) ListRepoTags(options ListRepoTagsOptions) ([]*Tag, error) { + tags := make([]*Tag, 0, options.getPerPage()) + return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", options.User, options.Repo, options.getURLQuery()), nil, nil, &tags) } diff --git a/gitea/status.go b/gitea/status.go index ba9c18c..6ed99ed 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -62,17 +62,20 @@ func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) jsonHeader, bytes.NewReader(body), status) } -// ListStatusesOption holds pagination information -type ListStatusesOption struct { - Page int +// ListStatusesOptions options for listing a repository's commit's statuses +type ListStatusesOptions struct { + ListOptions + Owner string + Repo string + SHA string } // ListStatuses returns all statuses for a given Commit // // GET /repos/:owner/:repo/commits/:ref/statuses -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(options ListStatusesOptions) ([]*Status, error) { + statuses := make([]*Status, 0, options.getPerPage()) + return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", options.Owner, options.Repo, options.SHA, options.getURLQuery()), 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 773dcf3..5e1a872 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -26,11 +26,18 @@ type AccessToken struct { TokenLastEight string `json:"token_last_eight"` } +// ListAccessTokens options for listing a users's access tokens +type ListAccessTokens struct { + ListOptions + User string + Pass string +} + // 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), - http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, &tokens) +func (c *Client) ListAccessTokens(options ListAccessTokens) ([]*AccessToken, error) { + tokens := make([]*AccessToken, 0, options.getPerPage()) + return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", options.User, options.getURLQuery()), + http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(options.User, options.Pass)}}, nil, &tokens) } // CreateAccessTokenOption options when create access token diff --git a/gitea/user_email.go b/gitea/user_email.go index 7823db1..9219513 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,15 @@ 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(options ListEmailsOptions) ([]*Email, error) { + emails := make([]*Email, 0, options.getPerPage()) + return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", options.getURLQuery()), nil, nil, &emails) } // CreateEmailOption options when creating email addresses diff --git a/gitea/user_follow.go b/gitea/user_follow.go index a197a7f..da73fe1 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -6,28 +6,62 @@ package gitea import "fmt" +// ListMyFollowersOptions options for listing current's user's followers +type ListMyFollowersOptions 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(options *ListMyFollowersOptions) ([]*User, error) { + if options == nil { + options = &ListMyFollowersOptions{} + } + + users := make([]*User, 0, options.getPerPage()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQuery()), nil, nil, &users) +} + +// ListFollowersOptions options for listing a user's followers +type ListFollowersOptions struct { + ListOptions + User string } // 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(options ListFollowersOptions) ([]*User, error) { + users := make([]*User, 0, options.getPerPage()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", options.User, options.getURLQuery()), nil, nil, &users) +} + +// ListMyFollowingOptions options for listing current's user's users being followed +type ListMyFollowingOptions 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(options *ListMyFollowingOptions) ([]*User, error) { + if options == nil { + options = &ListMyFollowingOptions{} + } + + users := make([]*User, 0, options.getPerPage()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQuery()), nil, nil, &users) +} + +// ListFollowingOptions options for listing a user's users being followed +type ListFollowingOptions struct { + ListOptions + User string } // 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(options *ListFollowingOptions) ([]*User, error) { + if options == nil { + options = &ListFollowingOptions{} + } + + users := make([]*User, 0, options.getPerPage()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", options.User, options.getURLQuery()), nil, nil, &users) } // IsFollowing if current user followed the target diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index 4830525..44206da 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -33,16 +33,31 @@ type GPGKeyEmail struct { Verified bool `json:"verified"` } +// ListGPGKeys options for listing a user's GPGKeys +type ListGPGKeys struct { + ListOptions + User string +} + // 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(options ListGPGKeys) ([]*GPGKey, error) { + keys := make([]*GPGKey, 0, options.getPerPage()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", options.User, options.getURLQuery()), nil, nil, &keys) +} + +// ListMyGPGKeysOptions options for listing current's user GPGKeys +type ListMyGPGKeysOptions struct { + ListOptions } // 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(options *ListMyGPGKeysOptions) ([]*GPGKey, error) { + if options == nil { + options = &ListMyGPGKeysOptions{} + } + + keys := make([]*GPGKey, 0, options.getPerPage()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.getURLQuery()), 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..fece6cf 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -24,16 +24,31 @@ type PublicKey struct { KeyType string `json:"key_type,omitempty"` } +// ListPublicKeysOptions options for listing a user's PublicKeys +type ListPublicKeysOptions struct { + ListOptions + User string +} + // 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(options ListPublicKeysOptions) ([]*PublicKey, error) { + keys := make([]*PublicKey, 0, options.getPerPage()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", options.User, options.getURLQuery()), nil, nil, &keys) +} + +// ListMyPublicKeysOptions options for listing current's user PublicKeys +type ListMyPublicKeysOptions struct { + ListOptions } // 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(options *ListMyPublicKeysOptions) ([]*PublicKey, error) { + if options == nil { + options = &ListMyPublicKeysOptions{} + } + + keys := make([]*PublicKey, 0, options.getPerPage()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQuery()), nil, nil, &keys) } // GetPublicKey get current user's public key by key id -- 2.40.1 From 30764e5eb603e4a556fbcfc63b9fc5ffb53e40ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 3 Jan 2020 17:28:33 +0000 Subject: [PATCH 03/27] added pagination to topics list --- gitea/repo_topics.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gitea/repo_topics.go b/gitea/repo_topics.go index 1276a53..2b9a939 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -10,15 +10,22 @@ import ( "fmt" ) +// ListRepoTopics options for listing repo's topics +type ListRepoTopics struct { + User string + Repo string + 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(options ListRepoTopics) (*TopicsList, error) { 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", options.User, options.Repo, options.getURLQuery()), nil, nil, &list) } // SetRepoTopics replaces the list of repo's topics -- 2.40.1 From 8192454f66161412188370c04ee87896ab0f97b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Thu, 30 Jan 2020 16:58:10 +0000 Subject: [PATCH 04/27] added version check --- gitea/issue_comment.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 3388d28..252d320 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -49,6 +49,9 @@ type ListRepoIssueCommentsOptions struct { // ListRepoIssueComments list comments for a given repo. func (c *Client) ListRepoIssueComments(options ListRepoIssueCommentsOptions) ([]*Comment, error) { comments := make([]*Comment, 0, options.getPerPage()) + if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil { + return comments, err + } return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", options.Owner, options.Repo, options.getURLQueryEncoded()), nil, nil, &comments) } -- 2.40.1 From 4db65aa8645fc5fc48f1dc9f4d05a90b6b696f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 12:45:39 +0000 Subject: [PATCH 05/27] removed getURLQueryEncoded method; added header to list_options --- gitea/admin_org.go | 4 ++-- gitea/admin_user.go | 4 ++-- gitea/attachment.go | 4 ++-- gitea/fork.go | 4 ++-- gitea/git_hook.go | 4 ++-- gitea/hook.go | 8 ++++---- gitea/issue_comment.go | 8 ++++---- gitea/issue_label.go | 4 ++-- gitea/issue_milestone.go | 4 ++-- gitea/issue_tracked_time.go | 4 ++-- gitea/list_options.go | 32 +++++++++++++------------------- gitea/org.go | 8 ++++---- gitea/org_team.go | 16 ++++++++-------- gitea/release.go | 4 ++-- gitea/repo.go | 12 ++++++------ gitea/repo_branch.go | 4 ++-- gitea/repo_collaborator.go | 2 +- gitea/repo_key.go | 4 ++-- gitea/repo_tag.go | 4 ++-- gitea/repo_topics.go | 2 +- gitea/status.go | 4 ++-- gitea/user_app.go | 4 ++-- gitea/user_email.go | 4 ++-- gitea/user_follow.go | 16 ++++++++-------- gitea/user_gpgkey.go | 8 ++++---- gitea/user_key.go | 8 ++++---- 26 files changed, 87 insertions(+), 93 deletions(-) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 7f1e9b4..8a3050c 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -22,8 +22,8 @@ func (c *Client) AdminListOrgs(options *AdminListOrgsOptions) ([]*Organization, options = &AdminListOrgsOptions{} } - orgs := make([]*Organization, 0, options.getPerPage()) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", options.getURLQueryEncoded()), nil, nil, &orgs) + orgs := make([]*Organization, 0, options.getPageSize()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", options.getURLQuery().Encode()), nil, nil, &orgs) } // AdminCreateOrg create an organization diff --git a/gitea/admin_user.go b/gitea/admin_user.go index 9aa15ef..75ce1db 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -22,8 +22,8 @@ func (c *Client) AdminListUsers(options *AdminListUsersOptions) ([]*User, error) options = &AdminListUsersOptions{} } - users := make([]*User, 0, options.getPerPage()) - return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", options.getURLQueryEncoded()), nil, nil, &users) + users := make([]*User, 0, options.getPageSize()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", options.getURLQuery().Encode()), nil, nil, &users) } // CreateUserOption create user options diff --git a/gitea/attachment.go b/gitea/attachment.go index 4869fc6..8cc832b 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -34,9 +34,9 @@ type ListReleaseAttachmentsOptions struct { // ListReleaseAttachments list release's attachments func (c *Client) ListReleaseAttachments(options ListReleaseAttachmentsOptions) ([]*Attachment, error) { - attachments := make([]*Attachment, 0, options.getPerPage()) + attachments := make([]*Attachment, 0, options.getPageSize()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", options.User, options.Repo, options.Release, options.getURLQueryEncoded()), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", options.User, options.Repo, options.Release, options.getURLQuery().Encode()), nil, nil, &attachments) return attachments, err } diff --git a/gitea/fork.go b/gitea/fork.go index bbe22f6..8fb5cbe 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -19,9 +19,9 @@ type ListForksOptions struct { // ListForks list a repository's forks func (c *Client) ListForks(options ListForksOptions) ([]*Repository, error) { - forks := make([]*Repository, options.getPerPage()) + forks := make([]*Repository, options.getPageSize()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/forks?%s", options.User, options.Repo, options.getURLQueryEncoded()), + fmt.Sprintf("/repos/%s/%s/forks?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &forks) return forks, err } diff --git a/gitea/git_hook.go b/gitea/git_hook.go index 00a6861..a14a722 100644 --- a/gitea/git_hook.go +++ b/gitea/git_hook.go @@ -26,8 +26,8 @@ type ListRepoGitHooksOptions struct { // ListRepoGitHooks list all the Git hooks of one repository func (c *Client) ListRepoGitHooks(options ListRepoGitHooksOptions) ([]*GitHook, error) { - hooks := make([]*GitHook, 0, options.getPerPage()) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &hooks) + hooks := make([]*GitHook, 0, options.getPageSize()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &hooks) } // GetRepoGitHook get a Git hook of a repository diff --git a/gitea/hook.go b/gitea/hook.go index 9d278b1..09a20ec 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -32,8 +32,8 @@ type ListOrgHooksOptions struct { // ListOrgHooks list all the hooks of one organization func (c *Client) ListOrgHooks(options ListOrgHooksOptions) ([]*Hook, error) { - hooks := make([]*Hook, 0, options.getPerPage()) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", options.Org, options.getURLQueryEncoded()), nil, nil, &hooks) + hooks := make([]*Hook, 0, options.getPageSize()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", options.Org, options.getURLQuery().Encode()), nil, nil, &hooks) } // ListRepoHooksOptions options for listing repository's hooks @@ -45,8 +45,8 @@ type ListRepoHooksOptions struct { // ListRepoHooks list all the hooks of one repository func (c *Client) ListRepoHooks(options ListRepoHooksOptions) ([]*Hook, error) { - hooks := make([]*Hook, 0, options.getPerPage()) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &hooks) + hooks := make([]*Hook, 0, options.getPageSize()) + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &hooks) } // GetOrgHook get a hook of an organization diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 252d320..c568d93 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -35,8 +35,8 @@ type ListIssueCommentsOptions struct { // ListIssueComments list comments on an issue. func (c *Client) ListIssueComments(options ListIssueCommentsOptions) ([]*Comment, error) { - comments := make([]*Comment, 0, options.getPerPage()) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", options.Owner, options.Repo, options.Index, options.getURLQueryEncoded()), nil, nil, &comments) + comments := make([]*Comment, 0, options.getPageSize()) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", options.Owner, options.Repo, options.Index, options.getURLQuery().Encode()), nil, nil, &comments) } // ListRepoIssueCommentsOptions options for listing repository's issue's comments @@ -48,11 +48,11 @@ type ListRepoIssueCommentsOptions struct { // ListRepoIssueComments list comments for a given repo. func (c *Client) ListRepoIssueComments(options ListRepoIssueCommentsOptions) ([]*Comment, error) { - comments := make([]*Comment, 0, options.getPerPage()) + comments := make([]*Comment, 0, options.getPageSize()) if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil { return comments, err } - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", options.Owner, options.Repo, options.getURLQueryEncoded()), nil, nil, &comments) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", options.Owner, options.Repo, options.getURLQuery().Encode()), nil, nil, &comments) } // CreateIssueCommentOption options for creating a comment on an issue diff --git a/gitea/issue_label.go b/gitea/issue_label.go index aa13457..920f539 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -29,8 +29,8 @@ type ListRepoLabelsOptions struct { // ListRepoLabels list labels of one repository func (c *Client) ListRepoLabels(options ListRepoLabelsOptions) ([]*Label, error) { - labels := make([]*Label, 0, options.getPerPage()) - return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", options.Owner, options.Repo, options.getURLQueryEncoded()), nil, nil, &labels) + labels := make([]*Label, 0, options.getPageSize()) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", options.Owner, options.Repo, options.getURLQuery().Encode()), nil, nil, &labels) } // GetRepoLabel get one label of repository by repo it diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index 4d0415f..d837f39 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -44,8 +44,8 @@ type ListRepoMilestonesOptions struct { // ListRepoMilestones list all the milestones of one repository func (c *Client) ListRepoMilestones(options ListRepoMilestonesOptions) ([]*Milestone, error) { - milestones := make([]*Milestone, 0, options.getPerPage()) - return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones?%s", options.Owner, options.Repo, options.getURLQueryEncoded()), nil, nil, &milestones) + milestones := make([]*Milestone, 0, options.getPageSize()) + return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones?%s", options.Owner, options.Repo, options.getURLQuery().Encode()), nil, nil, &milestones) } // GetMilestone get one milestone by repo name and milestone id diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index 81a8be4..2cc37ee 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -74,8 +74,8 @@ type ListTrackedTimesOptions struct { // ListTrackedTimes list tracked times of a single issue for a given repository func (c *Client) ListTrackedTimes(options ListTrackedTimesOptions) ([]*TrackedTime, error) { - times := make([]*TrackedTime, 0, options.getPerPage()) - return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", options.Owner, options.Repo, options.Index, options.getURLQueryEncoded()), nil, nil, ×) + times := make([]*TrackedTime, 0, options.getPageSize()) + return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", options.Owner, options.Repo, options.Index, options.getURLQuery().Encode()), nil, nil, ×) } // ResetIssueTime reset tracked time of a single issue for a given repository diff --git a/gitea/list_options.go b/gitea/list_options.go index 0448f32..9b15e1b 100644 --- a/gitea/list_options.go +++ b/gitea/list_options.go @@ -1,3 +1,7 @@ +// 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 ( @@ -7,18 +11,8 @@ import ( // ListOptions options for using Gitea's API pagination type ListOptions struct { - Page int - PerPage int -} - -func (o ListOptions) getURLQueryEncoded() string { - o.setDefaults() - - query := make(url.Values) - query.Add("page", fmt.Sprintf("%d", o.Page)) - query.Add("limit", fmt.Sprintf("%d", o.PerPage)) - - return query.Encode() + Page int + PageSize int } func (o ListOptions) getURLQuery() url.Values { @@ -26,7 +20,7 @@ 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.PerPage)) + query.Add("limit", fmt.Sprintf("%d", o.PageSize)) return query } @@ -36,15 +30,15 @@ func (o ListOptions) setDefaults() { o.Page = 1 } - if o.PerPage < 0 || o.PerPage > 50 { - o.PerPage = 10 + if o.PageSize < 0 || o.PageSize > 50 { + o.PageSize = 10 } } -func (o ListOptions) getPerPage() int { - if o.PerPage < 0 || o.PerPage > 50 { - o.PerPage = 10 +func (o ListOptions) getPageSize() int { + if o.PageSize < 0 || o.PageSize > 50 { + o.PageSize = 10 } - return o.PerPage + return o.PageSize } diff --git a/gitea/org.go b/gitea/org.go index 147ad03..c60b68e 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -34,8 +34,8 @@ func (c *Client) ListMyOrgs(options *ListMyOrgsOptions) ([]*Organization, error) options = &ListMyOrgsOptions{} } - orgs := make([]*Organization, 0, options.getPerPage()) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQueryEncoded()), nil, nil, &orgs) + orgs := make([]*Organization, 0, options.getPageSize()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQuery().Encode()), nil, nil, &orgs) } // ListUserOrgsOptions options for listing an user's organizations @@ -46,8 +46,8 @@ type ListUserOrgsOptions struct { // ListUserOrgs list all of some user's organizations func (c *Client) ListUserOrgs(options ListUserOrgsOptions) ([]*Organization, error) { - orgs := make([]*Organization, 0, options.getPerPage()) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", options.User, options.getURLQueryEncoded()), nil, nil, &orgs) + orgs := make([]*Organization, 0, options.getPageSize()) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", options.User, options.getURLQuery().Encode()), nil, nil, &orgs) } // GetOrg get one organization by name diff --git a/gitea/org_team.go b/gitea/org_team.go index 6b76beb..4e59313 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -30,8 +30,8 @@ type ListOrgTeamsOptions struct { // ListOrgTeams lists all teams of an organization func (c *Client) ListOrgTeams(options ListOrgTeamsOptions) ([]*Team, error) { - teams := make([]*Team, 0, options.getPerPage()) - return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", options.Org, options.getURLQueryEncoded()), nil, nil, &teams) + teams := make([]*Team, 0, options.getPageSize()) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", options.Org, options.getURLQuery().Encode()), nil, nil, &teams) } // ListMyTeamsOptions options for current's user teams @@ -45,8 +45,8 @@ func (c *Client) ListMyTeams(options *ListMyOrgsOptions) ([]*Team, error) { options = &ListMyOrgsOptions{} } - teams := make([]*Team, 0, options.getPerPage()) - return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQueryEncoded()), nil, nil, &teams) + teams := make([]*Team, 0, options.getPageSize()) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQuery().Encode()), nil, nil, &teams) } // GetTeam gets a team by ID @@ -109,8 +109,8 @@ type ListTeamMembersOptions struct { // ListTeamMembers lists all members of a team func (c *Client) ListTeamMembers(options ListTeamMembersOptions) ([]*User, error) { - members := make([]*User, 0, options.getPerPage()) - return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", options.ID, options.getURLQueryEncoded()), nil, nil, &members) + members := make([]*User, 0, options.getPageSize()) + return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", options.ID, options.getURLQuery().Encode()), nil, nil, &members) } // GetTeamMember gets a member of a team @@ -139,8 +139,8 @@ type ListTeamRepositoriesOptions struct { // ListTeamRepositories lists all repositories of a team func (c *Client) ListTeamRepositories(options ListTeamRepositoriesOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPerPage()) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", options.ID, options.getURLQueryEncoded()), nil, nil, &repos) + repos := make([]*Repository, 0, options.getPageSize()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", options.ID, options.getURLQuery().Encode()), nil, nil, &repos) } // AddTeamRepository adds a repository to a team diff --git a/gitea/release.go b/gitea/release.go index 22cbd00..8baaeaf 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -38,9 +38,9 @@ type ListReleasesOptions struct { // ListReleases list releases of a repository func (c *Client) ListReleases(options ListReleasesOptions) ([]*Release, error) { - releases := make([]*Release, 0, options.getPerPage()) + releases := make([]*Release, 0, options.getPageSize()) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/releases?%s", options.User, options.Repo, options.getURLQueryEncoded()), + fmt.Sprintf("/repos/%s/%s/releases?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &releases) return releases, err } diff --git a/gitea/repo.go b/gitea/repo.go index 3a17951..b07b1c2 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -67,8 +67,8 @@ func (c *Client) ListMyRepos(options *ListMyReposOptions) ([]*Repository, error) options = &ListMyReposOptions{} } - repos := make([]*Repository, 0, options.getPerPage()) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQueryEncoded()), nil, nil, &repos) + repos := make([]*Repository, 0, options.getPageSize()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQuery().Encode()), nil, nil, &repos) } // ListUserReposOptions options for listing a user's repositories @@ -79,8 +79,8 @@ type ListUserReposOptions struct { // ListUserRepos list all repositories of one user by user's name func (c *Client) ListUserRepos(options ListUserReposOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPerPage()) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", options.User, options.getURLQueryEncoded()), nil, nil, &repos) + repos := make([]*Repository, 0, options.getPageSize()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", options.User, options.getURLQuery().Encode()), nil, nil, &repos) } // ListOrgReposOptions options for a organization's repositories @@ -91,8 +91,8 @@ type ListOrgReposOptions struct { // ListOrgRepos list all repositories of one organization by organization's name func (c *Client) ListOrgRepos(options ListOrgReposOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPerPage()) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", options.Org, options.getURLQueryEncoded()), nil, nil, &repos) + repos := make([]*Repository, 0, options.getPageSize()) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", options.Org, options.getURLQuery().Encode()), nil, nil, &repos) } // CreateRepoOption options when creating repository diff --git a/gitea/repo_branch.go b/gitea/repo_branch.go index f749ed1..37deeec 100644 --- a/gitea/repo_branch.go +++ b/gitea/repo_branch.go @@ -58,8 +58,8 @@ type ListRepoBranchesOptions struct { // ListRepoBranches list all the branches of one repository func (c *Client) ListRepoBranches(options ListRepoBranchesOptions) ([]*Branch, error) { - branches := make([]*Branch, 0, options.getPerPage()) - return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &branches) + branches := make([]*Branch, 0, options.getPageSize()) + return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", options.User, options.Repo, options.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 77ddff9..46059aa 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -19,7 +19,7 @@ type ListCollaboratorsOptions struct { // ListCollaborators list a repository's collaborators func (c *Client) ListCollaborators(options ListCollaboratorsOptions) ([]*User, error) { - collaborators := make([]*User, 0, options.getPerPage()) + collaborators := make([]*User, 0, options.getPageSize()) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/collaborators", options.User, options.Repo), nil, nil, &collaborators) diff --git a/gitea/repo_key.go b/gitea/repo_key.go index e5eb137..d7a2e53 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -33,8 +33,8 @@ type ListDeployKeysOptions struct { // ListDeployKeys list all the deploy keys of one repository func (c *Client) ListDeployKeys(options ListDeployKeysOptions) ([]*DeployKey, error) { - keys := make([]*DeployKey, 0, options.getPerPage()) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &keys) + keys := make([]*DeployKey, 0, options.getPageSize()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", options.User, options.Repo, options.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 52c934f..2bf1e2c 100644 --- a/gitea/repo_tag.go +++ b/gitea/repo_tag.go @@ -26,6 +26,6 @@ type ListRepoTagsOptions struct { // ListRepoTags list all the branches of one repository func (c *Client) ListRepoTags(options ListRepoTagsOptions) ([]*Tag, error) { - tags := make([]*Tag, 0, options.getPerPage()) - return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &tags) + tags := make([]*Tag, 0, options.getPageSize()) + return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &tags) } diff --git a/gitea/repo_topics.go b/gitea/repo_topics.go index 73186ce..721a304 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -25,7 +25,7 @@ type TopicsList struct { // ListRepoTopics list all repository's topics func (c *Client) ListRepoTopics(options ListRepoTopics) (*TopicsList, error) { var list TopicsList - return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", options.User, options.Repo, options.getURLQueryEncoded()), nil, nil, &list) + return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", options.User, options.Repo, options.getURLQuery().Encode()), nil, nil, &list) } // SetRepoTopics replaces the list of repo's topics diff --git a/gitea/status.go b/gitea/status.go index 5e6152e..53949e8 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -74,8 +74,8 @@ type ListStatusesOptions struct { // // GET /repos/:owner/:repo/commits/:ref/statuses func (c *Client) ListStatuses(options ListStatusesOptions) ([]*Status, error) { - statuses := make([]*Status, 0, options.getPerPage()) - return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", options.Owner, options.Repo, options.SHA, options.getURLQueryEncoded()), nil, nil, &statuses) + statuses := make([]*Status, 0, options.getPageSize()) + return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", options.Owner, options.Repo, options.SHA, options.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 bb48c82..5f88404 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -35,8 +35,8 @@ type ListAccessTokens struct { // ListAccessTokens lista all the access tokens of user func (c *Client) ListAccessTokens(options ListAccessTokens) ([]*AccessToken, error) { - tokens := make([]*AccessToken, 0, options.getPerPage()) - return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", options.User, options.getURLQueryEncoded()), + tokens := make([]*AccessToken, 0, options.getPageSize()) + return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", options.User, options.getURLQuery().Encode()), http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(options.User, options.Pass)}}, nil, &tokens) } diff --git a/gitea/user_email.go b/gitea/user_email.go index 02115f2..4d220cd 100644 --- a/gitea/user_email.go +++ b/gitea/user_email.go @@ -24,8 +24,8 @@ type ListEmailsOptions struct { // ListEmails all the email addresses of user func (c *Client) ListEmails(options ListEmailsOptions) ([]*Email, error) { - emails := make([]*Email, 0, options.getPerPage()) - return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", options.getURLQueryEncoded()), nil, nil, &emails) + emails := make([]*Email, 0, options.getPageSize()) + return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", options.getURLQuery().Encode()), nil, nil, &emails) } // CreateEmailOption options when creating email addresses diff --git a/gitea/user_follow.go b/gitea/user_follow.go index b7ff41e..4b0b8c6 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -17,8 +17,8 @@ func (c *Client) ListMyFollowers(options *ListMyFollowersOptions) ([]*User, erro options = &ListMyFollowersOptions{} } - users := make([]*User, 0, options.getPerPage()) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQueryEncoded()), nil, nil, &users) + users := make([]*User, 0, options.getPageSize()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQuery().Encode()), nil, nil, &users) } // ListFollowersOptions options for listing a user's followers @@ -29,8 +29,8 @@ type ListFollowersOptions struct { // ListFollowers list all the followers of one user func (c *Client) ListFollowers(options ListFollowersOptions) ([]*User, error) { - users := make([]*User, 0, options.getPerPage()) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", options.User, options.getURLQueryEncoded()), nil, nil, &users) + users := make([]*User, 0, options.getPageSize()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", options.User, options.getURLQuery().Encode()), nil, nil, &users) } // ListMyFollowingOptions options for listing current's user's users being followed @@ -44,8 +44,8 @@ func (c *Client) ListMyFollowing(options *ListMyFollowingOptions) ([]*User, erro options = &ListMyFollowingOptions{} } - users := make([]*User, 0, options.getPerPage()) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQueryEncoded()), nil, nil, &users) + users := make([]*User, 0, options.getPageSize()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQuery().Encode()), nil, nil, &users) } // ListFollowingOptions options for listing a user's users being followed @@ -60,8 +60,8 @@ func (c *Client) ListFollowing(options *ListFollowingOptions) ([]*User, error) { options = &ListFollowingOptions{} } - users := make([]*User, 0, options.getPerPage()) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", options.User, options.getURLQueryEncoded()), nil, nil, &users) + users := make([]*User, 0, options.getPageSize()) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", options.User, options.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 8b9cc17..2c80559 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -41,8 +41,8 @@ type ListGPGKeys struct { // ListGPGKeys list all the GPG keys of the user func (c *Client) ListGPGKeys(options ListGPGKeys) ([]*GPGKey, error) { - keys := make([]*GPGKey, 0, options.getPerPage()) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", options.User, options.getURLQueryEncoded()), nil, nil, &keys) + keys := make([]*GPGKey, 0, options.getPageSize()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", options.User, options.getURLQuery().Encode()), nil, nil, &keys) } // ListMyGPGKeysOptions options for listing current's user GPGKeys @@ -56,8 +56,8 @@ func (c *Client) ListMyGPGKeys(options *ListMyGPGKeysOptions) ([]*GPGKey, error) options = &ListMyGPGKeysOptions{} } - keys := make([]*GPGKey, 0, options.getPerPage()) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.getURLQueryEncoded()), nil, nil, &keys) + keys := make([]*GPGKey, 0, options.getPageSize()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.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 daaa65b..c64ae0a 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -32,8 +32,8 @@ type ListPublicKeysOptions struct { // ListPublicKeys list all the public keys of the user func (c *Client) ListPublicKeys(options ListPublicKeysOptions) ([]*PublicKey, error) { - keys := make([]*PublicKey, 0, options.getPerPage()) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", options.User, options.getURLQueryEncoded()), nil, nil, &keys) + keys := make([]*PublicKey, 0, options.getPageSize()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", options.User, options.getURLQuery().Encode()), nil, nil, &keys) } // ListMyPublicKeysOptions options for listing current's user PublicKeys @@ -47,8 +47,8 @@ func (c *Client) ListMyPublicKeys(options *ListMyPublicKeysOptions) ([]*PublicKe options = &ListMyPublicKeysOptions{} } - keys := make([]*PublicKey, 0, options.getPerPage()) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQueryEncoded()), nil, nil, &keys) + keys := make([]*PublicKey, 0, options.getPageSize()) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) } // GetPublicKey get current user's public key by key id -- 2.40.1 From 49aa11d0b57bb84b9a8042311eab8c8510572526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 13:19:58 +0000 Subject: [PATCH 06/27] fixed golint --- gitea/issue_label.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/issue_label.go b/gitea/issue_label.go index b162d5f..7c5478a 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -80,7 +80,7 @@ func (c *Client) DeleteLabel(owner, repo string, id int64) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil) return err } - +// ListIssueLabelsOptions options for listing issues' labels type ListIssueLabelsOptions struct { ListOptions } -- 2.40.1 From 8b4037e0d6b227607127f5e70dd056969a6e3ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 13:38:54 +0000 Subject: [PATCH 07/27] removed getPageSize function; fixed some List requests memory allocation --- gitea/admin_org.go | 3 ++- gitea/admin_user.go | 9 +++------ gitea/attachment.go | 3 ++- gitea/fork.go | 3 ++- gitea/git_hook.go | 3 ++- gitea/hook.go | 6 ++++-- gitea/issue.go | 16 ++++++++++------ gitea/issue_comment.go | 6 ++++-- gitea/issue_label.go | 3 ++- gitea/issue_milestone.go | 8 +++++--- gitea/issue_tracked_time.go | 3 ++- gitea/list_options.go | 8 -------- gitea/org.go | 12 +++++------- gitea/org_team.go | 16 ++++++++-------- gitea/pull.go | 10 +++++----- gitea/release.go | 3 ++- gitea/repo.go | 13 ++++++------- gitea/repo_branch.go | 3 ++- gitea/repo_collaborator.go | 3 ++- gitea/repo_key.go | 3 ++- gitea/repo_tag.go | 3 ++- gitea/status.go | 3 ++- gitea/user_app.go | 3 ++- gitea/user_email.go | 3 ++- gitea/user_follow.go | 26 +++++++++----------------- gitea/user_gpgkey.go | 10 ++++------ gitea/user_key.go | 12 +++++------- 27 files changed, 96 insertions(+), 98 deletions(-) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 69933b6..09538a4 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -18,7 +18,8 @@ type AdminListOrgsOptions struct { // AdminListOrgs lists all orgs func (c *Client) AdminListOrgs(options AdminListOrgsOptions) ([]*Organization, error) { - orgs := make([]*Organization, 0, options.getPageSize()) + options.setDefaults() + orgs := make([]*Organization, 0, options.PageSize) return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", options.getURLQuery().Encode()), nil, nil, &orgs) } diff --git a/gitea/admin_user.go b/gitea/admin_user.go index 75ce1db..b3318f5 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -17,12 +17,9 @@ type AdminListUsersOptions struct { } // AdminListUsers lists all users -func (c *Client) AdminListUsers(options *AdminListUsersOptions) ([]*User, error) { - if options == nil { - options = &AdminListUsersOptions{} - } - - users := make([]*User, 0, options.getPageSize()) +func (c *Client) AdminListUsers(options AdminListUsersOptions) ([]*User, error) { + options.setDefaults() + users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", options.getURLQuery().Encode()), nil, nil, &users) } diff --git a/gitea/attachment.go b/gitea/attachment.go index c8039f1..ebd520e 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -31,7 +31,8 @@ type ListReleaseAttachmentsOptions struct { // ListReleaseAttachments list release's attachments func (c *Client) ListReleaseAttachments(user, repo string, release int64, options ListReleaseAttachmentsOptions) ([]*Attachment, error) { - attachments := make([]*Attachment, 0, options.getPageSize()) + options.setDefaults() + attachments := make([]*Attachment, 0, options.PageSize) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", user, repo, release, options.getURLQuery().Encode()), nil, nil, &attachments) diff --git a/gitea/fork.go b/gitea/fork.go index ec851d6..04e2192 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -17,7 +17,8 @@ type ListForksOptions struct { // ListForks list a repository's forks func (c *Client) ListForks(user string, repo string, options ListForksOptions) ([]*Repository, error) { - forks := make([]*Repository, options.getPageSize()) + options.setDefaults() + forks := make([]*Repository, options.PageSize) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/forks?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &forks) diff --git a/gitea/git_hook.go b/gitea/git_hook.go index 58ae12c..09bf8ef 100644 --- a/gitea/git_hook.go +++ b/gitea/git_hook.go @@ -24,7 +24,8 @@ type ListRepoGitHooksOptions struct { // ListRepoGitHooks list all the Git hooks of one repository func (c *Client) ListRepoGitHooks(user, repo string, options ListRepoGitHooksOptions) ([]*GitHook, error) { - hooks := make([]*GitHook, 0, options.getPageSize()) + options.setDefaults() + hooks := make([]*GitHook, 0, options.PageSize) return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &hooks) } diff --git a/gitea/hook.go b/gitea/hook.go index 7092652..298ccde 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -31,7 +31,8 @@ type ListOrgHooksOptions struct { // ListOrgHooks list all the hooks of one organization func (c *Client) ListOrgHooks(org string, options ListOrgHooksOptions) ([]*Hook, error) { - hooks := make([]*Hook, 0, options.getPageSize()) + options.setDefaults() + hooks := make([]*Hook, 0, options.PageSize) return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, options.getURLQuery().Encode()), nil, nil, &hooks) } @@ -42,7 +43,8 @@ type ListRepoHooksOptions struct { // ListRepoHooks list all the hooks of one repository func (c *Client) ListRepoHooks(user, repo string, options ListRepoHooksOptions) ([]*Hook, error) { - hooks := make([]*Hook, 0, options.getPageSize()) + options.setDefaults() + hooks := make([]*Hook, 0, options.PageSize) return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &hooks) } diff --git a/gitea/issue.go b/gitea/issue.go index 3c5eb2c..d991da4 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -77,10 +77,12 @@ func (opt *ListIssueOption) QueryEncode() string { } // ListIssues returns all issues assigned the authenticated user -func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { +func (c *Client) ListIssues(options ListIssueOption) ([]*Issue, error) { + options.setDefaults() + issues := make([]*Issue, 0, options.PageSize) + link, _ := url.Parse("/repos/issues/search") - issues := make([]*Issue, 0, 10) - link.RawQuery = opt.QueryEncode() + link.RawQuery = options.QueryEncode() return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } @@ -106,10 +108,12 @@ func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { } // ListRepoIssues returns all issues for a given repository -func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { +func (c *Client) ListRepoIssues(owner, repo string, options ListIssueOption) ([]*Issue, error) { + options.setDefaults() + issues := make([]*Issue, 0, options.PageSize) + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo)) - link.RawQuery = opt.QueryEncode() - issues := make([]*Issue, 0, 10) + link.RawQuery = options.QueryEncode() return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 6003c3e..2f4bdd4 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -32,7 +32,8 @@ type ListIssueCommentsOptions struct { // ListIssueComments list comments on an issue. func (c *Client) ListIssueComments(owner, repo string, index int64, options ListIssueCommentsOptions) ([]*Comment, error) { - comments := make([]*Comment, 0, options.getPageSize()) + options.setDefaults() + comments := make([]*Comment, 0, options.PageSize) return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", owner, repo, index, options.getURLQuery().Encode()), nil, nil, &comments) } @@ -43,7 +44,8 @@ type ListRepoIssueCommentsOptions struct { // ListRepoIssueComments list comments for a given repo. func (c *Client) ListRepoIssueComments(owner, repo string, options ListRepoIssueCommentsOptions) ([]*Comment, error) { - comments := make([]*Comment, 0, options.getPageSize()) + options.setDefaults() + comments := make([]*Comment, 0, options.PageSize) if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil { return comments, err } diff --git a/gitea/issue_label.go b/gitea/issue_label.go index 7c5478a..4e4786b 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -27,7 +27,8 @@ type ListRepoLabelsOptions struct { // ListRepoLabels list labels of one repository func (c *Client) ListRepoLabels(owner, repo string, options ListRepoLabelsOptions) ([]*Label, error) { - labels := make([]*Label, 0, options.getPageSize()) + options.setDefaults() + labels := make([]*Label, 0, options.PageSize) return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, options.getURLQuery().Encode()), nil, nil, &labels) } diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index f05e89f..55413dd 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -53,10 +53,12 @@ func (opt *ListMilestoneOption) QueryEncode() string { } // ListRepoMilestones list all the milestones of one repository -func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, error) { +func (c *Client) ListRepoMilestones(owner, repo string, options ListMilestoneOption) ([]*Milestone, error) { + options.setDefaults() + milestones := make([]*Milestone, 0, options.PageSize) + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/milestones", owner, repo)) - link.RawQuery = opt.QueryEncode() - milestones := make([]*Milestone, 0, 10) + link.RawQuery = options.QueryEncode() return milestones, c.getParsedResponse("GET", link.String(), nil, nil, &milestones) } diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index 9253aa8..b8f820d 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -71,7 +71,8 @@ type ListTrackedTimesOptions struct { // ListTrackedTimes list tracked times of a single issue for a given repository func (c *Client) ListTrackedTimes(owner, repo string, index int64, options ListTrackedTimesOptions) ([]*TrackedTime, error) { - times := make([]*TrackedTime, 0, options.getPageSize()) + options.setDefaults() + times := make([]*TrackedTime, 0, options.PageSize) return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, options.getURLQuery().Encode()), nil, nil, ×) } diff --git a/gitea/list_options.go b/gitea/list_options.go index 9b15e1b..c25ecfb 100644 --- a/gitea/list_options.go +++ b/gitea/list_options.go @@ -34,11 +34,3 @@ func (o ListOptions) setDefaults() { o.PageSize = 10 } } - -func (o ListOptions) getPageSize() int { - if o.PageSize < 0 || o.PageSize > 50 { - o.PageSize = 10 - } - - return o.PageSize -} diff --git a/gitea/org.go b/gitea/org.go index e58b929..ca4854d 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -29,12 +29,9 @@ type ListMyOrgsOptions struct { } // ListMyOrgs list all of current user's organizations -func (c *Client) ListMyOrgs(options *ListMyOrgsOptions) ([]*Organization, error) { - if options == nil { - options = &ListMyOrgsOptions{} - } - - orgs := make([]*Organization, 0, options.getPageSize()) +func (c *Client) ListMyOrgs(options ListMyOrgsOptions) ([]*Organization, error) { + options.setDefaults() + orgs := make([]*Organization, 0, options.PageSize) return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQuery().Encode()), nil, nil, &orgs) } @@ -45,7 +42,8 @@ type ListUserOrgsOptions struct { // ListUserOrgs list all of some user's organizations func (c *Client) ListUserOrgs(user string, options ListUserOrgsOptions) ([]*Organization, error) { - orgs := make([]*Organization, 0, options.getPageSize()) + options.setDefaults() + orgs := make([]*Organization, 0, options.PageSize) return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, options.getURLQuery().Encode()), nil, nil, &orgs) } diff --git a/gitea/org_team.go b/gitea/org_team.go index 322c2b3..657d9b5 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -29,7 +29,8 @@ type ListOrgTeamsOptions struct { // ListOrgTeams lists all teams of an organization func (c *Client) ListOrgTeams(org string, options ListOrgTeamsOptions) ([]*Team, error) { - teams := make([]*Team, 0, options.getPageSize()) + options.setDefaults() + teams := make([]*Team, 0, options.PageSize) return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, options.getURLQuery().Encode()), nil, nil, &teams) } @@ -40,11 +41,8 @@ type ListMyTeamsOptions struct { // ListMyTeams lists all the teams of the current user func (c *Client) ListMyTeams(options *ListMyOrgsOptions) ([]*Team, error) { - if options == nil { - options = &ListMyOrgsOptions{} - } - - teams := make([]*Team, 0, options.getPageSize()) + options.setDefaults() + teams := make([]*Team, 0, options.PageSize) return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQuery().Encode()), nil, nil, &teams) } @@ -107,7 +105,8 @@ type ListTeamMembersOptions struct { // ListTeamMembers lists all members of a team func (c *Client) ListTeamMembers(id int64, options ListTeamMembersOptions) ([]*User, error) { - members := make([]*User, 0, options.getPageSize()) + options.setDefaults() + members := make([]*User, 0, options.PageSize) return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, options.getURLQuery().Encode()), nil, nil, &members) } @@ -136,7 +135,8 @@ type ListTeamRepositoriesOptions struct { // ListTeamRepositories lists all repositories of a team func (c *Client) ListTeamRepositories(id int64, options ListTeamRepositoriesOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPageSize()) + options.setDefaults() + repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, options.getURLQuery().Encode()), nil, nil, &repos) } diff --git a/gitea/pull.go b/gitea/pull.go index 70ab6d4..1faaac2 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -83,12 +83,12 @@ func (opt *ListPullRequestsOptions) QueryEncode() string { } // ListRepoPullRequests list PRs of one repository -func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { - // declare variables +func (c *Client) ListRepoPullRequests(owner, repo string, options ListPullRequestsOptions) ([]*PullRequest, error) { + options.setDefaults() + prs := make([]*PullRequest, 0, options.PageSize) + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo)) - prs := make([]*PullRequest, 0, opt.getPageSize()) - link.RawQuery = opt.QueryEncode() - // request + link.RawQuery = options.QueryEncode() return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs) } diff --git a/gitea/release.go b/gitea/release.go index 6bc8adf..c5ffc38 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -36,7 +36,8 @@ type ListReleasesOptions struct { // ListReleases list releases of a repository func (c *Client) ListReleases(user, repo string, options ListReleasesOptions) ([]*Release, error) { - releases := make([]*Release, 0, options.getPageSize()) + options.setDefaults() + releases := make([]*Release, 0, options.PageSize) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/releases?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &releases) diff --git a/gitea/repo.go b/gitea/repo.go index 3afabea..1b29cd3 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -63,11 +63,8 @@ type ListMyReposOptions struct { // ListMyRepos lists all repositories for the authenticated user that has access to. func (c *Client) ListMyRepos(options *ListMyReposOptions) ([]*Repository, error) { - if options == nil { - options = &ListMyReposOptions{} - } - - repos := make([]*Repository, 0, options.getPageSize()) + options.setDefaults() + repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQuery().Encode()), nil, nil, &repos) } @@ -78,7 +75,8 @@ type ListUserReposOptions struct { // ListUserRepos list all repositories of one user by user's name func (c *Client) ListUserRepos(user string, options ListUserReposOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPageSize()) + options.setDefaults() + repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, options.getURLQuery().Encode()), nil, nil, &repos) } @@ -89,7 +87,8 @@ type ListOrgReposOptions struct { // ListOrgRepos list all repositories of one organization by organization's name func (c *Client) ListOrgRepos(org string, options ListOrgReposOptions) ([]*Repository, error) { - repos := make([]*Repository, 0, options.getPageSize()) + options.setDefaults() + repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", org, options.getURLQuery().Encode()), nil, nil, &repos) } diff --git a/gitea/repo_branch.go b/gitea/repo_branch.go index e231249..38316f3 100644 --- a/gitea/repo_branch.go +++ b/gitea/repo_branch.go @@ -56,7 +56,8 @@ type ListRepoBranchesOptions struct { // ListRepoBranches list all the branches of one repository func (c *Client) ListRepoBranches(user, repo string, options ListRepoBranchesOptions) ([]*Branch, error) { - branches := make([]*Branch, 0, options.getPageSize()) + options.setDefaults() + branches := make([]*Branch, 0, options.PageSize) return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &branches) } diff --git a/gitea/repo_collaborator.go b/gitea/repo_collaborator.go index cebbcfd..af348fa 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -17,7 +17,8 @@ type ListCollaboratorsOptions struct { // ListCollaborators list a repository's collaborators func (c *Client) ListCollaborators(user, repo string, options ListCollaboratorsOptions) ([]*User, error) { - collaborators := make([]*User, 0, options.getPageSize()) + options.setDefaults() + collaborators := make([]*User, 0, options.PageSize) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/collaborators", user, repo), nil, nil, &collaborators) diff --git a/gitea/repo_key.go b/gitea/repo_key.go index 86dc649..db01795 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -31,7 +31,8 @@ type ListDeployKeysOptions struct { // ListDeployKeys list all the deploy keys of one repository func (c *Client) ListDeployKeys(user, repo string, options ListDeployKeysOptions) ([]*DeployKey, error) { - keys := make([]*DeployKey, 0, options.getPageSize()) + options.setDefaults() + keys := make([]*DeployKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &keys) } diff --git a/gitea/repo_tag.go b/gitea/repo_tag.go index 1b48ff4..98b9a55 100644 --- a/gitea/repo_tag.go +++ b/gitea/repo_tag.go @@ -24,6 +24,7 @@ type ListRepoTagsOptions struct { // ListRepoTags list all the branches of one repository func (c *Client) ListRepoTags(user, repo string, options ListRepoTagsOptions) ([]*Tag, error) { - tags := make([]*Tag, 0, options.getPageSize()) + options.setDefaults() + tags := make([]*Tag, 0, options.PageSize) return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &tags) } diff --git a/gitea/status.go b/gitea/status.go index eb8216c..5aae94f 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -71,7 +71,8 @@ type ListStatusesOptions struct { // // GET /repos/:owner/:repo/commits/:ref/statuses func (c *Client) ListStatuses(owner, repo, sha string, options ListStatusesOptions) ([]*Status, error) { - statuses := make([]*Status, 0, options.getPageSize()) + options.setDefaults() + statuses := make([]*Status, 0, options.PageSize) return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, options.getURLQuery().Encode()), nil, nil, &statuses) } diff --git a/gitea/user_app.go b/gitea/user_app.go index 7bc9a76..4d52164 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -33,7 +33,8 @@ type ListAccessTokens struct { // ListAccessTokens lista all the access tokens of user func (c *Client) ListAccessTokens(user, pass string, options ListAccessTokens) ([]*AccessToken, error) { - tokens := make([]*AccessToken, 0, options.getPageSize()) + options.setDefaults() + tokens := make([]*AccessToken, 0, options.PageSize) return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, options.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 4d220cd..8ce6e6b 100644 --- a/gitea/user_email.go +++ b/gitea/user_email.go @@ -24,7 +24,8 @@ type ListEmailsOptions struct { // ListEmails all the email addresses of user func (c *Client) ListEmails(options ListEmailsOptions) ([]*Email, error) { - emails := make([]*Email, 0, options.getPageSize()) + options.setDefaults() + emails := make([]*Email, 0, options.PageSize) return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", options.getURLQuery().Encode()), nil, nil, &emails) } diff --git a/gitea/user_follow.go b/gitea/user_follow.go index 0241b4c..2ae1082 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -13,11 +13,8 @@ type ListMyFollowersOptions struct { // ListMyFollowers list all the followers of current user func (c *Client) ListMyFollowers(options *ListMyFollowersOptions) ([]*User, error) { - if options == nil { - options = &ListMyFollowersOptions{} - } - - users := make([]*User, 0, options.getPageSize()) + options.setDefaults() + users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQuery().Encode()), nil, nil, &users) } @@ -28,7 +25,8 @@ type ListFollowersOptions struct { // ListFollowers list all the followers of one user func (c *Client) ListFollowers(user string, options ListFollowersOptions) ([]*User, error) { - users := make([]*User, 0, options.getPageSize()) + options.setDefaults() + users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, options.getURLQuery().Encode()), nil, nil, &users) } @@ -38,12 +36,9 @@ type ListMyFollowingOptions struct { } // ListMyFollowing list all the users current user followed -func (c *Client) ListMyFollowing(options *ListMyFollowingOptions) ([]*User, error) { - if options == nil { - options = &ListMyFollowingOptions{} - } - - users := make([]*User, 0, options.getPageSize()) +func (c *Client) ListMyFollowing(options ListMyFollowingOptions) ([]*User, error) { + options.setDefaults() + users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQuery().Encode()), nil, nil, &users) } @@ -54,11 +49,8 @@ type ListFollowingOptions struct { // ListFollowing list all the users the user followed func (c *Client) ListFollowing(user string, options *ListFollowingOptions) ([]*User, error) { - if options == nil { - options = &ListFollowingOptions{} - } - - users := make([]*User, 0, options.getPageSize()) + options.setDefaults() + users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", user, options.getURLQuery().Encode()), nil, nil, &users) } diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index 26ae2a0..d857727 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -40,7 +40,8 @@ type ListGPGKeys struct { // ListGPGKeys list all the GPG keys of the user func (c *Client) ListGPGKeys(user string, options ListGPGKeys) ([]*GPGKey, error) { - keys := make([]*GPGKey, 0, options.getPageSize()) + options.setDefaults() + keys := make([]*GPGKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, options.getURLQuery().Encode()), nil, nil, &keys) } @@ -51,11 +52,8 @@ type ListMyGPGKeysOptions struct { // ListMyGPGKeys list all the GPG keys of current user func (c *Client) ListMyGPGKeys(options *ListMyGPGKeysOptions) ([]*GPGKey, error) { - if options == nil { - options = &ListMyGPGKeysOptions{} - } - - keys := make([]*GPGKey, 0, options.getPageSize()) + options.setDefaults() + keys := make([]*GPGKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) } diff --git a/gitea/user_key.go b/gitea/user_key.go index c91ab97..0ef06b7 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -31,7 +31,8 @@ type ListPublicKeysOptions struct { // ListPublicKeys list all the public keys of the user func (c *Client) ListPublicKeys(user string, options ListPublicKeysOptions) ([]*PublicKey, error) { - keys := make([]*PublicKey, 0, options.getPageSize()) + options.setDefaults() + keys := make([]*PublicKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, options.getURLQuery().Encode()), nil, nil, &keys) } @@ -41,12 +42,9 @@ type ListMyPublicKeysOptions struct { } // ListMyPublicKeys list all the public keys of current user -func (c *Client) ListMyPublicKeys(options *ListMyPublicKeysOptions) ([]*PublicKey, error) { - if options == nil { - options = &ListMyPublicKeysOptions{} - } - - keys := make([]*PublicKey, 0, options.getPageSize()) +func (c *Client) ListMyPublicKeys(options ListMyPublicKeysOptions) ([]*PublicKey, error) { + options.setDefaults() + keys := make([]*PublicKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) } -- 2.40.1 From 42cf4b329de45204ec0f8129bfc7d7d6857d00f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 16:42:41 +0000 Subject: [PATCH 08/27] added constants to list_options; removed duplicated structures --- gitea/hook.go | 13 ++++--------- gitea/issue_comment.go | 3 --- gitea/list_options.go | 9 +++++---- gitea/org.go | 13 ++++--------- gitea/org_team.go | 13 ++++--------- gitea/pull.go | 2 +- gitea/repo.go | 13 ++++--------- gitea/user_follow.go | 22 ++++++---------------- gitea/user_gpgkey.go | 7 +------ gitea/user_key.go | 7 +------ 10 files changed, 30 insertions(+), 72 deletions(-) diff --git a/gitea/hook.go b/gitea/hook.go index 298ccde..f3162fb 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -24,25 +24,20 @@ type Hook struct { Created time.Time `json:"created_at"` } -// ListOrgHooksOptions options for listing organization's hooks -type ListOrgHooksOptions struct { +// ListOrgHooksOptions options for listing hooks +type ListHooksOptions struct { ListOptions } // ListOrgHooks list all the hooks of one organization -func (c *Client) ListOrgHooks(org string, options ListOrgHooksOptions) ([]*Hook, error) { +func (c *Client) ListOrgHooks(org string, options ListHooksOptions) ([]*Hook, error) { options.setDefaults() hooks := make([]*Hook, 0, options.PageSize) return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, options.getURLQuery().Encode()), nil, nil, &hooks) } -// ListRepoHooksOptions options for listing repository's hooks -type ListRepoHooksOptions struct { - ListOptions -} - // ListRepoHooks list all the hooks of one repository -func (c *Client) ListRepoHooks(user, repo string, options ListRepoHooksOptions) ([]*Hook, error) { +func (c *Client) ListRepoHooks(user, repo string, options ListHooksOptions) ([]*Hook, error) { options.setDefaults() hooks := make([]*Hook, 0, options.PageSize) return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &hooks) diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 2f4bdd4..7886f07 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -46,9 +46,6 @@ type ListRepoIssueCommentsOptions struct { func (c *Client) ListRepoIssueComments(owner, repo string, options ListRepoIssueCommentsOptions) ([]*Comment, error) { options.setDefaults() comments := make([]*Comment, 0, options.PageSize) - if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil { - return comments, err - } return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", owner, repo, options.getURLQuery().Encode()), nil, nil, &comments) } diff --git a/gitea/list_options.go b/gitea/list_options.go index c25ecfb..d56c651 100644 --- a/gitea/list_options.go +++ b/gitea/list_options.go @@ -9,6 +9,9 @@ import ( "net/url" ) +const defaultPageSize = 10 +const maxPageSize = 50 + // ListOptions options for using Gitea's API pagination type ListOptions struct { Page int @@ -16,8 +19,6 @@ type ListOptions struct { } func (o ListOptions) getURLQuery() url.Values { - o.setDefaults() - query := make(url.Values) query.Add("page", fmt.Sprintf("%d", o.Page)) query.Add("limit", fmt.Sprintf("%d", o.PageSize)) @@ -30,7 +31,7 @@ func (o ListOptions) setDefaults() { o.Page = 1 } - if o.PageSize < 0 || o.PageSize > 50 { - o.PageSize = 10 + if o.PageSize < 0 || o.PageSize > maxPageSize { + o.PageSize = defaultPageSize } } diff --git a/gitea/org.go b/gitea/org.go index ca4854d..a640147 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -23,25 +23,20 @@ type Organization struct { Visibility string `json:"visibility"` } -// ListMyOrgsOptions options for listing current user's organizations -type ListMyOrgsOptions struct { +// ListOrgsOptions options for listing organizations +type ListOrgsOptions struct { ListOptions } // ListMyOrgs list all of current user's organizations -func (c *Client) ListMyOrgs(options ListMyOrgsOptions) ([]*Organization, error) { +func (c *Client) ListMyOrgs(options ListOrgsOptions) ([]*Organization, error) { options.setDefaults() orgs := make([]*Organization, 0, options.PageSize) return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQuery().Encode()), nil, nil, &orgs) } -// ListUserOrgsOptions options for listing an user's organizations -type ListUserOrgsOptions struct { - ListOptions -} - // ListUserOrgs list all of some user's organizations -func (c *Client) ListUserOrgs(user string, options ListUserOrgsOptions) ([]*Organization, error) { +func (c *Client) ListUserOrgs(user string, options ListOrgsOptions) ([]*Organization, error) { options.setDefaults() orgs := make([]*Organization, 0, options.PageSize) return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, options.getURLQuery().Encode()), nil, nil, &orgs) diff --git a/gitea/org_team.go b/gitea/org_team.go index 657d9b5..160ccd6 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -22,25 +22,20 @@ type Team struct { Units []string `json:"units"` } -// ListOrgTeamsOptions options for listing organization's teams -type ListOrgTeamsOptions struct { +// ListTeamsOptions options for listing teams +type ListTeamsOptions struct { ListOptions } // ListOrgTeams lists all teams of an organization -func (c *Client) ListOrgTeams(org string, options ListOrgTeamsOptions) ([]*Team, error) { +func (c *Client) ListOrgTeams(org string, options ListTeamsOptions) ([]*Team, error) { options.setDefaults() teams := make([]*Team, 0, options.PageSize) return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, options.getURLQuery().Encode()), nil, nil, &teams) } -// ListMyTeamsOptions options for current's user teams -type ListMyTeamsOptions struct { - ListOptions -} - // ListMyTeams lists all the teams of the current user -func (c *Client) ListMyTeams(options *ListMyOrgsOptions) ([]*Team, error) { +func (c *Client) ListMyTeams(options *ListTeamsOptions) ([]*Team, error) { options.setDefaults() teams := make([]*Team, 0, options.PageSize) return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQuery().Encode()), nil, nil, &teams) diff --git a/gitea/pull.go b/gitea/pull.go index 1faaac2..39f8b8a 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -59,7 +59,7 @@ type PullRequest struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - ListOptions `json:"-"` + ListOptions // open, closed, all State string `json:"state"` // oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority diff --git a/gitea/repo.go b/gitea/repo.go index 1b29cd3..a7fbc44 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -56,25 +56,20 @@ type Repository struct { AvatarURL string `json:"avatar_url"` } -// ListMyReposOptions options for listing current's user repositories -type ListMyReposOptions struct { +// 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(options *ListMyReposOptions) ([]*Repository, error) { +func (c *Client) ListMyRepos(options *ListReposOptions) ([]*Repository, error) { options.setDefaults() repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQuery().Encode()), nil, nil, &repos) } -// ListUserReposOptions options for listing a user's repositories -type ListUserReposOptions struct { - ListOptions -} - // ListUserRepos list all repositories of one user by user's name -func (c *Client) ListUserRepos(user string, options ListUserReposOptions) ([]*Repository, error) { +func (c *Client) ListUserRepos(user string, options ListReposOptions) ([]*Repository, error) { options.setDefaults() repos := make([]*Repository, 0, options.PageSize) return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, options.getURLQuery().Encode()), nil, nil, &repos) diff --git a/gitea/user_follow.go b/gitea/user_follow.go index 2ae1082..3b11c12 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -6,23 +6,18 @@ package gitea import "fmt" -// ListMyFollowersOptions options for listing current's user's followers -type ListMyFollowersOptions struct { +// ListFollowersOptions options for listing followers +type ListFollowersOptions struct { ListOptions } // ListMyFollowers list all the followers of current user -func (c *Client) ListMyFollowers(options *ListMyFollowersOptions) ([]*User, error) { +func (c *Client) ListMyFollowers(options *ListFollowersOptions) ([]*User, error) { options.setDefaults() users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQuery().Encode()), nil, nil, &users) } -// ListFollowersOptions options for listing a user's followers -type ListFollowersOptions struct { - ListOptions -} - // ListFollowers list all the followers of one user func (c *Client) ListFollowers(user string, options ListFollowersOptions) ([]*User, error) { options.setDefaults() @@ -30,23 +25,18 @@ func (c *Client) ListFollowers(user string, options ListFollowersOptions) ([]*Us return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, options.getURLQuery().Encode()), nil, nil, &users) } -// ListMyFollowingOptions options for listing current's user's users being followed -type ListMyFollowingOptions struct { +// 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(options ListMyFollowingOptions) ([]*User, error) { +func (c *Client) ListMyFollowing(options ListFollowingOptions) ([]*User, error) { options.setDefaults() users := make([]*User, 0, options.PageSize) return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQuery().Encode()), nil, nil, &users) } -// ListFollowingOptions options for listing a user's users being followed -type ListFollowingOptions struct { - ListOptions -} - // ListFollowing list all the users the user followed func (c *Client) ListFollowing(user string, options *ListFollowingOptions) ([]*User, error) { options.setDefaults() diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index d857727..1891d79 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -45,13 +45,8 @@ func (c *Client) ListGPGKeys(user string, options ListGPGKeys) ([]*GPGKey, error return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, options.getURLQuery().Encode()), nil, nil, &keys) } -// ListMyGPGKeysOptions options for listing current's user GPGKeys -type ListMyGPGKeysOptions struct { - ListOptions -} - // ListMyGPGKeys list all the GPG keys of current user -func (c *Client) ListMyGPGKeys(options *ListMyGPGKeysOptions) ([]*GPGKey, error) { +func (c *Client) ListMyGPGKeys(options *ListGPGKeys) ([]*GPGKey, error) { options.setDefaults() keys := make([]*GPGKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) diff --git a/gitea/user_key.go b/gitea/user_key.go index 0ef06b7..c9181c3 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -36,13 +36,8 @@ func (c *Client) ListPublicKeys(user string, options ListPublicKeysOptions) ([]* return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, options.getURLQuery().Encode()), nil, nil, &keys) } -// ListMyPublicKeysOptions options for listing current's user PublicKeys -type ListMyPublicKeysOptions struct { - ListOptions -} - // ListMyPublicKeys list all the public keys of current user -func (c *Client) ListMyPublicKeys(options ListMyPublicKeysOptions) ([]*PublicKey, error) { +func (c *Client) ListMyPublicKeys(options ListPublicKeysOptions) ([]*PublicKey, error) { options.setDefaults() keys := make([]*PublicKey, 0, options.PageSize) return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) -- 2.40.1 From aac44cbd2eee92891b07abd670693bc788506644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 16:52:04 +0000 Subject: [PATCH 09/27] fixed lint issue --- gitea/hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/hook.go b/gitea/hook.go index f3162fb..1f12e9c 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -24,7 +24,7 @@ type Hook struct { Created time.Time `json:"created_at"` } -// ListOrgHooksOptions options for listing hooks +// ListHooksOptions options for listing hooks type ListHooksOptions struct { ListOptions } -- 2.40.1 From 74d32b9d0285a483bf4ff162c2cc2d2cd84773fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 17:50:02 +0000 Subject: [PATCH 10/27] refactored options to opt; added repos search --- gitea/admin_org.go | 8 ++-- gitea/admin_user.go | 8 ++-- gitea/attachment.go | 8 ++-- gitea/fork.go | 8 ++-- gitea/git_hook.go | 8 ++-- gitea/hook.go | 16 +++---- gitea/issue.go | 16 +++---- gitea/issue_comment.go | 16 +++---- gitea/issue_label.go | 8 ++-- gitea/issue_milestone.go | 8 ++-- gitea/issue_tracked_time.go | 8 ++-- gitea/org.go | 16 +++---- gitea/org_team.go | 32 ++++++------- gitea/pull.go | 8 ++-- gitea/release.go | 8 ++-- gitea/repo.go | 91 ++++++++++++++++++++++++++++++++----- gitea/repo_branch.go | 8 ++-- gitea/repo_collaborator.go | 6 +-- gitea/repo_key.go | 8 ++-- gitea/repo_tag.go | 8 ++-- gitea/repo_topics.go | 4 +- gitea/status.go | 8 ++-- gitea/user_app.go | 8 ++-- gitea/user_email.go | 8 ++-- gitea/user_follow.go | 32 ++++++------- gitea/user_gpgkey.go | 16 +++---- gitea/user_key.go | 16 +++---- 27 files changed, 228 insertions(+), 161 deletions(-) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 09538a4..8b0bac1 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -17,10 +17,10 @@ type AdminListOrgsOptions struct { } // AdminListOrgs lists all orgs -func (c *Client) AdminListOrgs(options AdminListOrgsOptions) ([]*Organization, error) { - options.setDefaults() - orgs := make([]*Organization, 0, options.PageSize) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", options.getURLQuery().Encode()), 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_user.go b/gitea/admin_user.go index b3318f5..c447934 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -17,10 +17,10 @@ type AdminListUsersOptions struct { } // AdminListUsers lists all users -func (c *Client) AdminListUsers(options AdminListUsersOptions) ([]*User, error) { - options.setDefaults() - users := make([]*User, 0, options.PageSize) - return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", options.getURLQuery().Encode()), 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 ebd520e..a13787b 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -30,11 +30,11 @@ type ListReleaseAttachmentsOptions struct { } // ListReleaseAttachments list release's attachments -func (c *Client) ListReleaseAttachments(user, repo string, release int64, options ListReleaseAttachmentsOptions) ([]*Attachment, error) { - options.setDefaults() - attachments := make([]*Attachment, 0, options.PageSize) +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?%s", user, repo, release, options.getURLQuery().Encode()), + 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 04e2192..55cadb1 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -16,11 +16,11 @@ type ListForksOptions struct { } // ListForks list a repository's forks -func (c *Client) ListForks(user string, repo string, options ListForksOptions) ([]*Repository, error) { - options.setDefaults() - forks := make([]*Repository, options.PageSize) +func (c *Client) ListForks(user string, repo string, opt ListForksOptions) ([]*Repository, error) { + opt.setDefaults() + forks := make([]*Repository, opt.PageSize) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/forks?%s", user, repo, options.getURLQuery().Encode()), + fmt.Sprintf("/repos/%s/%s/forks?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &forks) return forks, err } diff --git a/gitea/git_hook.go b/gitea/git_hook.go index 09bf8ef..1162d09 100644 --- a/gitea/git_hook.go +++ b/gitea/git_hook.go @@ -23,10 +23,10 @@ type ListRepoGitHooksOptions struct { } // ListRepoGitHooks list all the Git hooks of one repository -func (c *Client) ListRepoGitHooks(user, repo string, options ListRepoGitHooksOptions) ([]*GitHook, error) { - options.setDefaults() - hooks := make([]*GitHook, 0, options.PageSize) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, options.getURLQuery().Encode()), 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 1f12e9c..f89a6e9 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -30,17 +30,17 @@ type ListHooksOptions struct { } // ListOrgHooks list all the hooks of one organization -func (c *Client) ListOrgHooks(org string, options ListHooksOptions) ([]*Hook, error) { - options.setDefaults() - hooks := make([]*Hook, 0, options.PageSize) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, options.getURLQuery().Encode()), 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, options ListHooksOptions) ([]*Hook, error) { - options.setDefaults() - hooks := make([]*Hook, 0, options.PageSize) - return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, options.getURLQuery().Encode()), 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 d991da4..e8541ba 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -77,12 +77,12 @@ func (opt *ListIssueOption) QueryEncode() string { } // ListIssues returns all issues assigned the authenticated user -func (c *Client) ListIssues(options ListIssueOption) ([]*Issue, error) { - options.setDefaults() - issues := make([]*Issue, 0, options.PageSize) +func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { + opt.setDefaults() + issues := make([]*Issue, 0, opt.PageSize) link, _ := url.Parse("/repos/issues/search") - link.RawQuery = options.QueryEncode() + link.RawQuery = opt.QueryEncode() return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } @@ -108,12 +108,12 @@ func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { } // ListRepoIssues returns all issues for a given repository -func (c *Client) ListRepoIssues(owner, repo string, options ListIssueOption) ([]*Issue, error) { - options.setDefaults() - issues := make([]*Issue, 0, options.PageSize) +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 = options.QueryEncode() + link.RawQuery = opt.QueryEncode() return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 7886f07..45f8be4 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -31,10 +31,10 @@ type ListIssueCommentsOptions struct { } // ListIssueComments list comments on an issue. -func (c *Client) ListIssueComments(owner, repo string, index int64, options ListIssueCommentsOptions) ([]*Comment, error) { - options.setDefaults() - comments := make([]*Comment, 0, options.PageSize) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", owner, repo, index, options.getURLQuery().Encode()), nil, nil, &comments) +func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssueCommentsOptions) ([]*Comment, error) { + opt.setDefaults() + comments := make([]*Comment, 0, opt.PageSize) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, &comments) } // ListRepoIssueCommentsOptions options for listing repository's issue's comments @@ -43,10 +43,10 @@ type ListRepoIssueCommentsOptions struct { } // ListRepoIssueComments list comments for a given repo. -func (c *Client) ListRepoIssueComments(owner, repo string, options ListRepoIssueCommentsOptions) ([]*Comment, error) { - options.setDefaults() - comments := make([]*Comment, 0, options.PageSize) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", owner, repo, options.getURLQuery().Encode()), nil, nil, &comments) +func (c *Client) ListRepoIssueComments(owner, repo string, opt ListRepoIssueCommentsOptions) ([]*Comment, error) { + opt.setDefaults() + comments := make([]*Comment, 0, opt.PageSize) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &comments) } // CreateIssueCommentOption options for creating a comment on an issue diff --git a/gitea/issue_label.go b/gitea/issue_label.go index 4e4786b..4b5b0cd 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -26,10 +26,10 @@ type ListRepoLabelsOptions struct { } // ListRepoLabels list labels of one repository -func (c *Client) ListRepoLabels(owner, repo string, options ListRepoLabelsOptions) ([]*Label, error) { - options.setDefaults() - labels := make([]*Label, 0, options.PageSize) - return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, options.getURLQuery().Encode()), nil, nil, &labels) +func (c *Client) ListRepoLabels(owner, repo string, opt ListRepoLabelsOptions) ([]*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 diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index 55413dd..2e0552e 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -53,12 +53,12 @@ func (opt *ListMilestoneOption) QueryEncode() string { } // ListRepoMilestones list all the milestones of one repository -func (c *Client) ListRepoMilestones(owner, repo string, options ListMilestoneOption) ([]*Milestone, error) { - options.setDefaults() - milestones := make([]*Milestone, 0, options.PageSize) +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 = options.QueryEncode() + link.RawQuery = opt.QueryEncode() return milestones, c.getParsedResponse("GET", link.String(), nil, nil, &milestones) } diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index b8f820d..51f1a99 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -70,10 +70,10 @@ type ListTrackedTimesOptions struct { } // ListTrackedTimes list tracked times of a single issue for a given repository -func (c *Client) ListTrackedTimes(owner, repo string, index int64, options ListTrackedTimesOptions) ([]*TrackedTime, error) { - options.setDefaults() - times := make([]*TrackedTime, 0, options.PageSize) - return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, options.getURLQuery().Encode()), nil, nil, ×) +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 diff --git a/gitea/org.go b/gitea/org.go index a640147..61ade30 100644 --- a/gitea/org.go +++ b/gitea/org.go @@ -29,17 +29,17 @@ type ListOrgsOptions struct { } // ListMyOrgs list all of current user's organizations -func (c *Client) ListMyOrgs(options ListOrgsOptions) ([]*Organization, error) { - options.setDefaults() - orgs := make([]*Organization, 0, options.PageSize) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", options.getURLQuery().Encode()), 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, options ListOrgsOptions) ([]*Organization, error) { - options.setDefaults() - orgs := make([]*Organization, 0, options.PageSize) - return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, options.getURLQuery().Encode()), 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 160ccd6..4c786e3 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -28,17 +28,17 @@ type ListTeamsOptions struct { } // ListOrgTeams lists all teams of an organization -func (c *Client) ListOrgTeams(org string, options ListTeamsOptions) ([]*Team, error) { - options.setDefaults() - teams := make([]*Team, 0, options.PageSize) - return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, options.getURLQuery().Encode()), 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(options *ListTeamsOptions) ([]*Team, error) { - options.setDefaults() - teams := make([]*Team, 0, options.PageSize) - return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", options.getURLQuery().Encode()), 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 @@ -99,10 +99,10 @@ type ListTeamMembersOptions struct { } // ListTeamMembers lists all members of a team -func (c *Client) ListTeamMembers(id int64, options ListTeamMembersOptions) ([]*User, error) { - options.setDefaults() - members := make([]*User, 0, options.PageSize) - return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, options.getURLQuery().Encode()), 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 @@ -129,10 +129,10 @@ type ListTeamRepositoriesOptions struct { } // ListTeamRepositories lists all repositories of a team -func (c *Client) ListTeamRepositories(id int64, options ListTeamRepositoriesOptions) ([]*Repository, error) { - options.setDefaults() - repos := make([]*Repository, 0, options.PageSize) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, options.getURLQuery().Encode()), 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 39f8b8a..e3c01a0 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -83,12 +83,12 @@ func (opt *ListPullRequestsOptions) QueryEncode() string { } // ListRepoPullRequests list PRs of one repository -func (c *Client) ListRepoPullRequests(owner, repo string, options ListPullRequestsOptions) ([]*PullRequest, error) { - options.setDefaults() - prs := make([]*PullRequest, 0, options.PageSize) +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 = options.QueryEncode() + link.RawQuery = opt.QueryEncode() return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs) } diff --git a/gitea/release.go b/gitea/release.go index c5ffc38..fe5c023 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -35,11 +35,11 @@ type ListReleasesOptions struct { } // ListReleases list releases of a repository -func (c *Client) ListReleases(user, repo string, options ListReleasesOptions) ([]*Release, error) { - options.setDefaults() - releases := make([]*Release, 0, options.PageSize) +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?%s", user, repo, options.getURLQuery().Encode()), + 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 a7fbc44..e0bda79 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -62,17 +63,17 @@ type ListReposOptions struct { } // ListMyRepos lists all repositories for the authenticated user that has access to. -func (c *Client) ListMyRepos(options *ListReposOptions) ([]*Repository, error) { - options.setDefaults() - repos := make([]*Repository, 0, options.PageSize) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", options.getURLQuery().Encode()), 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, options ListReposOptions) ([]*Repository, error) { - options.setDefaults() - repos := make([]*Repository, 0, options.PageSize) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, options.getURLQuery().Encode()), 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 @@ -81,10 +82,76 @@ type ListOrgReposOptions struct { } // ListOrgRepos list all repositories of one organization by organization's name -func (c *Client) ListOrgRepos(org string, options ListOrgReposOptions) ([]*Repository, error) { - options.setDefaults() - repos := make([]*Repository, 0, options.PageSize) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", org, options.getURLQuery().Encode()), 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 { + // https://try.gitea.io/api/swagger#/repository/repoSearch + ListOptions + Keyword string + Topic bool + IncludeDesc bool + UID int64 + PriorityOwnerID int64 + StarredBy int64 + Private bool + Template bool + Mode string + Exclusive bool + Sort string +} + +// QueryEncode turns options into querystring argument +func (opt *SearchRepoOptions) QueryEncode() string { + query := opt.getURLQuery() + if opt.Keyword != "" { + query.Add("q", opt.Keyword) + } + + query.Add("topic", fmt.Sprintf("%t", opt.Topic)) + query.Add("includeDesc", fmt.Sprintf("%t", opt.IncludeDesc)) + + if opt.UID > 0 { + query.Add("uid", fmt.Sprintf("%d", opt.UID)) + } + + if opt.PriorityOwnerID > 0 { + query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PriorityOwnerID)) + } + + if opt.StarredBy > 0 { + query.Add("starredBy", fmt.Sprintf("%d", opt.StarredBy)) + } + + query.Add("private", fmt.Sprintf("%t", opt.Private)) + query.Add("template", fmt.Sprintf("%t", opt.Template)) + + if opt.Mode != "" { + query.Add("mode", opt.Mode) + } + + query.Add("exclusive", fmt.Sprintf("%t", opt.Exclusive)) + + if opt.Sort != "" { + query.Add("sort", opt.Sort) + } + + return query.Encode() +} + +// SearchRepos searches for repositories matching the given filters +func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) { + opt.setDefaults() + repos := make([]*Repository, 0, opt.PageSize) + + link, _ := url.Parse("/repos/search") + link.RawQuery = opt.QueryEncode() + + return repos, c.getParsedResponse("GET", link.String(), nil, nil, &repos) } // CreateRepoOption options when creating repository diff --git a/gitea/repo_branch.go b/gitea/repo_branch.go index 38316f3..adfc6c2 100644 --- a/gitea/repo_branch.go +++ b/gitea/repo_branch.go @@ -55,10 +55,10 @@ type ListRepoBranchesOptions struct { } // ListRepoBranches list all the branches of one repository -func (c *Client) ListRepoBranches(user, repo string, options ListRepoBranchesOptions) ([]*Branch, error) { - options.setDefaults() - branches := make([]*Branch, 0, options.PageSize) - return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, options.getURLQuery().Encode()), 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 af348fa..33c9067 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -16,9 +16,9 @@ type ListCollaboratorsOptions struct { } // ListCollaborators list a repository's collaborators -func (c *Client) ListCollaborators(user, repo string, options ListCollaboratorsOptions) ([]*User, error) { - options.setDefaults() - collaborators := make([]*User, 0, options.PageSize) +func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, error) { + opt.setDefaults() + collaborators := make([]*User, 0, opt.PageSize) err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/collaborators", user, repo), nil, nil, &collaborators) diff --git a/gitea/repo_key.go b/gitea/repo_key.go index db01795..96a6d93 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -30,10 +30,10 @@ type ListDeployKeysOptions struct { } // ListDeployKeys list all the deploy keys of one repository -func (c *Client) ListDeployKeys(user, repo string, options ListDeployKeysOptions) ([]*DeployKey, error) { - options.setDefaults() - keys := make([]*DeployKey, 0, options.PageSize) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys?%s", user, repo, options.getURLQuery().Encode()), 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 98b9a55..b64d90b 100644 --- a/gitea/repo_tag.go +++ b/gitea/repo_tag.go @@ -23,8 +23,8 @@ type ListRepoTagsOptions struct { } // ListRepoTags list all the branches of one repository -func (c *Client) ListRepoTags(user, repo string, options ListRepoTagsOptions) ([]*Tag, error) { - options.setDefaults() - tags := make([]*Tag, 0, options.PageSize) - return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, options.getURLQuery().Encode()), nil, nil, &tags) +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 43b115b..4e482f2 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -21,9 +21,9 @@ type TopicsList struct { } // ListRepoTopics list all repository's topics -func (c *Client) ListRepoTopics(user, repo string, options ListRepoTopics) (*TopicsList, error) { +func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopics) (*TopicsList, error) { var list TopicsList - return &list, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, options.getURLQuery().Encode()), 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 5aae94f..89724b9 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -70,10 +70,10 @@ type ListStatusesOptions struct { // ListStatuses returns all statuses for a given Commit // // GET /repos/:owner/:repo/commits/:ref/statuses -func (c *Client) ListStatuses(owner, repo, sha string, options ListStatusesOptions) ([]*Status, error) { - options.setDefaults() - statuses := make([]*Status, 0, options.PageSize) - return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, options.getURLQuery().Encode()), nil, nil, &statuses) +func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOptions) ([]*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 4d52164..d4a3c98 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -32,10 +32,10 @@ type ListAccessTokens struct { } // ListAccessTokens lista all the access tokens of user -func (c *Client) ListAccessTokens(user, pass string, options ListAccessTokens) ([]*AccessToken, error) { - options.setDefaults() - tokens := make([]*AccessToken, 0, options.PageSize) - return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, options.getURLQuery().Encode()), +func (c *Client) ListAccessTokens(user, pass string, opt ListAccessTokens) ([]*AccessToken, error) { + opt.setDefaults() + tokens := make([]*AccessToken, 0, opt.PageSize) + return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, opt.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 8ce6e6b..a122658 100644 --- a/gitea/user_email.go +++ b/gitea/user_email.go @@ -23,10 +23,10 @@ type ListEmailsOptions struct { } // ListEmails all the email addresses of user -func (c *Client) ListEmails(options ListEmailsOptions) ([]*Email, error) { - options.setDefaults() - emails := make([]*Email, 0, options.PageSize) - return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", options.getURLQuery().Encode()), 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 3b11c12..4cdcd1a 100644 --- a/gitea/user_follow.go +++ b/gitea/user_follow.go @@ -12,17 +12,17 @@ type ListFollowersOptions struct { } // ListMyFollowers list all the followers of current user -func (c *Client) ListMyFollowers(options *ListFollowersOptions) ([]*User, error) { - options.setDefaults() - users := make([]*User, 0, options.PageSize) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", options.getURLQuery().Encode()), 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, options ListFollowersOptions) ([]*User, error) { - options.setDefaults() - users := make([]*User, 0, options.PageSize) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, options.getURLQuery().Encode()), 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 @@ -31,17 +31,17 @@ type ListFollowingOptions struct { } // ListMyFollowing list all the users current user followed -func (c *Client) ListMyFollowing(options ListFollowingOptions) ([]*User, error) { - options.setDefaults() - users := make([]*User, 0, options.PageSize) - return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", options.getURLQuery().Encode()), 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, options *ListFollowingOptions) ([]*User, error) { - options.setDefaults() - users := make([]*User, 0, options.PageSize) - return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", user, options.getURLQuery().Encode()), 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 1891d79..411521e 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -39,17 +39,17 @@ type ListGPGKeys struct { } // ListGPGKeys list all the GPG keys of the user -func (c *Client) ListGPGKeys(user string, options ListGPGKeys) ([]*GPGKey, error) { - options.setDefaults() - keys := make([]*GPGKey, 0, options.PageSize) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, options.getURLQuery().Encode()), nil, nil, &keys) +func (c *Client) ListGPGKeys(user string, opt ListGPGKeys) ([]*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(options *ListGPGKeys) ([]*GPGKey, error) { - options.setDefaults() - keys := make([]*GPGKey, 0, options.PageSize) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", options.getURLQuery().Encode()), nil, nil, &keys) +func (c *Client) ListMyGPGKeys(opt *ListGPGKeys) ([]*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 c9181c3..6c71487 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -30,17 +30,17 @@ type ListPublicKeysOptions struct { } // ListPublicKeys list all the public keys of the user -func (c *Client) ListPublicKeys(user string, options ListPublicKeysOptions) ([]*PublicKey, error) { - options.setDefaults() - keys := make([]*PublicKey, 0, options.PageSize) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, options.getURLQuery().Encode()), 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(options ListPublicKeysOptions) ([]*PublicKey, error) { - options.setDefaults() - keys := make([]*PublicKey, 0, options.PageSize) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", options.getURLQuery().Encode()), 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 -- 2.40.1 From 9f2b41b3e68ddcfca4ed8712e74711ae4600dbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 20:52:28 +0000 Subject: [PATCH 11/27] refactored ListLabels; removed JSON tags from ListPullRequests --- gitea/issue_label.go | 12 ++++-------- gitea/pull.go | 6 +++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/gitea/issue_label.go b/gitea/issue_label.go index 4b5b0cd..af4b0a8 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -20,13 +20,13 @@ type Label struct { URL string `json:"url"` } -// ListRepoLabelsOptions options for listing repository's labels -type ListRepoLabelsOptions struct { +// ListLabelsOptions options for listing repository's labels +type ListLabelsOptions struct { ListOptions } // ListRepoLabels list labels of one repository -func (c *Client) ListRepoLabels(owner, repo string, opt ListRepoLabelsOptions) ([]*Label, error) { +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) @@ -81,13 +81,9 @@ func (c *Client) DeleteLabel(owner, repo string, id int64) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil) return err } -// ListIssueLabelsOptions options for listing issues' labels -type ListIssueLabelsOptions struct { - ListOptions -} // GetIssueLabels get labels of one issue via issue id -func (c *Client) GetIssueLabels(owner, repo string, index int64, opts ListIssueLabelsOptions) ([]*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?%s", owner, repo, index, opts.getURLQuery().Encode()), nil, nil, &labels) } diff --git a/gitea/pull.go b/gitea/pull.go index e3c01a0..0cc4ab9 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -61,10 +61,10 @@ type PullRequest struct { type ListPullRequestsOptions struct { ListOptions // open, closed, all - State string `json:"state"` + State string // oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority - Sort string `json:"sort"` - Milestone int64 `json:"milestone"` + Sort string + Milestone int64 } // QueryEncode turns options into querystring argument -- 2.40.1 From da61989e675ced9e9c3d1c276243b8e7ce705157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 21:38:47 +0000 Subject: [PATCH 12/27] added SearchRepo API Call --- gitea/repo.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ gitea/repo_test.go | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/gitea/repo.go b/gitea/repo.go index 4556f17..ecb90e4 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -74,6 +75,75 @@ func (c *Client) ListOrgRepos(org string) ([]*Repository, error) { return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos) } +// SearchRepoOptions options for searching repositories +type SearchRepoOptions struct { + // https://try.gitea.io/api/swagger#/repository/repoSearch + Keyword string + Topic bool + IncludeDesc bool + UID int64 + PriorityOwnerID int64 + StarredBy int64 + Private bool + Template bool + Mode string + Exclusive bool + Sort string +} + +// QueryEncode turns options into querystring argument +func (opt *SearchRepoOptions) QueryEncode() string { + query := make(url.Values) + if opt.Keyword != "" { + query.Add("q", opt.Keyword) + } + + query.Add("topic", fmt.Sprintf("%t", opt.Topic)) + query.Add("includeDesc", fmt.Sprintf("%t", opt.IncludeDesc)) + + if opt.UID > 0 { + query.Add("uid", fmt.Sprintf("%d", opt.UID)) + } + + if opt.PriorityOwnerID > 0 { + query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PriorityOwnerID)) + } + + if opt.StarredBy > 0 { + query.Add("starredBy", fmt.Sprintf("%d", opt.StarredBy)) + } + + query.Add("private", fmt.Sprintf("%t", opt.Private)) + query.Add("template", fmt.Sprintf("%t", opt.Template)) + + if opt.Mode != "" { + query.Add("mode", opt.Mode) + } + + query.Add("exclusive", fmt.Sprintf("%t", opt.Exclusive)) + + if opt.Sort != "" { + query.Add("sort", opt.Sort) + } + + return query.Encode() +} + +type searchRepoResponse struct { + Repos []*Repository `json:"data"` +} + +// SearchRepos searches for repositories matching the given filters +func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) { + resp := new(searchRepoResponse) + + link, _ := url.Parse("/repos/search") + link.RawQuery = opt.QueryEncode() + + err := c.getParsedResponse("GET", link.String(), nil, nil, &resp) + return resp.Repos, err +} + // CreateRepoOption options when creating repository type CreateRepoOption struct { // Name of the repository to create diff --git a/gitea/repo_test.go b/gitea/repo_test.go index f46e735..a1db3dd 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -31,6 +31,55 @@ func TestCreateRepo(t *testing.T) { assert.NoError(t, err) } +func TestSearchRepo(t *testing.T) { + log.Println("== TestSearchRepo ==") + c := newTestClient() + + repo, err := createTestRepo(t, "RepoSearch1", c) + assert.NoError(t, err) + assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")) + assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2")) + + repo, err = createTestRepo(t, "RepoSearch2", c) + assert.NoError(t, err) + assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")) + + repos, err := c.SearchRepos(SearchRepoOptions{ + Keyword: "Search1", + IncludeDesc: true, + }) + assert.NoError(t, err) + assert.NotNil(t, repos) + assert.Len(t, repos, 1) + + repos, err = c.SearchRepos(SearchRepoOptions{ + Keyword: "Search", + IncludeDesc: true, + }) + assert.NoError(t, err) + assert.NotNil(t, repos) + assert.Len(t, repos, 2) + + repos, err = c.SearchRepos(SearchRepoOptions{ + Keyword: "TestTopic1", + Topic: true, + }) + assert.NoError(t, err) + assert.NotNil(t, repos) + assert.Len(t, repos, 2) + + repos, err = c.SearchRepos(SearchRepoOptions{ + Keyword: "TestTopic2", + Topic: true, + }) + assert.NoError(t, err) + assert.NotNil(t, repos) + assert.Len(t, repos, 1) + + err = c.DeleteRepo(repo.Owner.UserName, repo.Name) + assert.NoError(t, err) +} + func TestDeleteRepo(t *testing.T) { log.Println("== TestDeleteRepo ==") c := newTestClient() -- 2.40.1 From 72a200b957d39f3f5f3f7e179397c7bd0d2b6411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 21:42:20 +0000 Subject: [PATCH 13/27] removed search repo code; fixed ListGPGKeysOptions struct name --- gitea/repo.go | 67 -------------------------------------------- gitea/user_gpgkey.go | 8 +++--- 2 files changed, 4 insertions(+), 71 deletions(-) diff --git a/gitea/repo.go b/gitea/repo.go index e0bda79..2fecdc3 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/url" "time" ) @@ -88,72 +87,6 @@ func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repositor 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 { - // https://try.gitea.io/api/swagger#/repository/repoSearch - ListOptions - Keyword string - Topic bool - IncludeDesc bool - UID int64 - PriorityOwnerID int64 - StarredBy int64 - Private bool - Template bool - Mode string - Exclusive bool - Sort string -} - -// QueryEncode turns options into querystring argument -func (opt *SearchRepoOptions) QueryEncode() string { - query := opt.getURLQuery() - if opt.Keyword != "" { - query.Add("q", opt.Keyword) - } - - query.Add("topic", fmt.Sprintf("%t", opt.Topic)) - query.Add("includeDesc", fmt.Sprintf("%t", opt.IncludeDesc)) - - if opt.UID > 0 { - query.Add("uid", fmt.Sprintf("%d", opt.UID)) - } - - if opt.PriorityOwnerID > 0 { - query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PriorityOwnerID)) - } - - if opt.StarredBy > 0 { - query.Add("starredBy", fmt.Sprintf("%d", opt.StarredBy)) - } - - query.Add("private", fmt.Sprintf("%t", opt.Private)) - query.Add("template", fmt.Sprintf("%t", opt.Template)) - - if opt.Mode != "" { - query.Add("mode", opt.Mode) - } - - query.Add("exclusive", fmt.Sprintf("%t", opt.Exclusive)) - - if opt.Sort != "" { - query.Add("sort", opt.Sort) - } - - return query.Encode() -} - -// SearchRepos searches for repositories matching the given filters -func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) { - opt.setDefaults() - repos := make([]*Repository, 0, opt.PageSize) - - link, _ := url.Parse("/repos/search") - link.RawQuery = opt.QueryEncode() - - return repos, c.getParsedResponse("GET", link.String(), nil, nil, &repos) -} - // CreateRepoOption options when creating repository type CreateRepoOption struct { // Name of the repository to create diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index 411521e..026cb1a 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -33,20 +33,20 @@ type GPGKeyEmail struct { Verified bool `json:"verified"` } -// ListGPGKeys options for listing a user's GPGKeys -type ListGPGKeys struct { +// 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, opt ListGPGKeys) ([]*GPGKey, error) { +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(opt *ListGPGKeys) ([]*GPGKey, error) { +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) -- 2.40.1 From c4211a73e20bc9ebca010aa3b0212a0e8a8bbcf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 21:55:45 +0000 Subject: [PATCH 14/27] added list options to SearchRepoOptions --- gitea/repo.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gitea/repo.go b/gitea/repo.go index ea8ce33..243b06e 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -91,6 +91,7 @@ func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repositor // SearchRepoOptions options for searching repositories type SearchRepoOptions struct { // https://try.gitea.io/api/swagger#/repository/repoSearch + ListOptions Keyword string Topic bool IncludeDesc bool @@ -106,7 +107,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) } @@ -148,6 +149,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") -- 2.40.1 From 2b6d94189774d67ccbf30ff52278f5fd1ec4583d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 21:57:27 +0000 Subject: [PATCH 15/27] ListRepoTopics setDefaults; fixed ListCollaborators --- gitea/repo_collaborator.go | 2 +- gitea/repo_topics.go | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gitea/repo_collaborator.go b/gitea/repo_collaborator.go index 33c9067..c8453b3 100644 --- a/gitea/repo_collaborator.go +++ b/gitea/repo_collaborator.go @@ -20,7 +20,7 @@ func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptio opt.setDefaults() collaborators := make([]*User, 0, opt.PageSize) err := c.getParsedResponse("GET", - fmt.Sprintf("/repos/%s/%s/collaborators", user, repo), + fmt.Sprintf("/repos/%s/%s/collaborators?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &collaborators) return collaborators, err } diff --git a/gitea/repo_topics.go b/gitea/repo_topics.go index 4e482f2..65e72a0 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -10,8 +10,8 @@ import ( "fmt" ) -// ListRepoTopics options for listing repo's topics -type ListRepoTopics struct { +// ListRepoTopicsOptions options for listing repo's topics +type ListRepoTopicsOptions struct { ListOptions } @@ -20,8 +20,10 @@ type TopicsList struct { Topics []string `json:"topics"` } -// ListRepoTopics list all repository's topics -func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopics) (*TopicsList, error) { +// ListRepoTopicsOptions list all repository's topics +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?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &list) } -- 2.40.1 From b42d1495d15f13eaae232d3fed289154a61b60e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Fri, 31 Jan 2020 22:15:53 +0000 Subject: [PATCH 16/27] fixed lint error --- gitea/repo_topics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/repo_topics.go b/gitea/repo_topics.go index 65e72a0..8facdba 100644 --- a/gitea/repo_topics.go +++ b/gitea/repo_topics.go @@ -20,7 +20,7 @@ type TopicsList struct { Topics []string `json:"topics"` } -// ListRepoTopicsOptions list all repository's topics +// ListRepoTopics list all repository's topics func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) (*TopicsList, error) { opt.setDefaults() -- 2.40.1 From 51b18b927e3c44f4b6bd5ca69f77e916f3967f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Sun, 2 Feb 2020 18:05:26 +0000 Subject: [PATCH 17/27] fixed unit tests --- gitea/issue_comment_test.go | 4 ++-- gitea/user_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gitea/issue_comment_test.go b/gitea/issue_comment_test.go index 6a63d9a..e2a17be 100644 --- a/gitea/issue_comment_test.go +++ b/gitea/issue_comment_test.go @@ -51,12 +51,12 @@ func TestIssueComment(t *testing.T) { assert.NoError(t, c.AdminDeleteUser(tUser3.UserName)) // ListRepoIssueComments - comments, err := c.ListRepoIssueComments(user.UserName, repo.Name) + comments, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListRepoIssueCommentsOptions{}) assert.NoError(t, err) assert.Len(t, comments, 7) // ListIssueComments - comments, err = c.ListIssueComments(user.UserName, repo.Name, 2) + comments, err = c.ListIssueComments(user.UserName, repo.Name, 2, ListIssueCommentsOptions{}) assert.NoError(t, err) assert.Len(t, comments, 3) diff --git a/gitea/user_test.go b/gitea/user_test.go index c5bd926..87f8ec4 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, ListAccessTokens{}) 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, ListAccessTokens{}) 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, ListAccessTokens{}) assert.Len(t, result, 1) } -- 2.40.1 From 5ffa9f872f6aa01f5c537a74a7b31e46a5d6fb3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Sun, 2 Feb 2020 19:46:59 +0000 Subject: [PATCH 18/27] use ListIssueCommentsOptions instead of ListRepoIssueCommentsOptions --- gitea/issue_comment.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 29d0a2e..b627207 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -37,13 +37,8 @@ func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssu return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, &comments) } -// ListRepoIssueCommentsOptions options for listing repository's issue's comments -type ListRepoIssueCommentsOptions struct { - ListOptions -} - // ListRepoIssueComments list comments for a given repo. -func (c *Client) ListRepoIssueComments(owner, repo string, opt ListRepoIssueCommentsOptions) ([]*Comment, error) { +func (c *Client) ListRepoIssueComments(owner, repo string, opt ListIssueCommentsOptions) ([]*Comment, error) { opt.setDefaults() comments := make([]*Comment, 0, opt.PageSize) return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &comments) -- 2.40.1 From 609142576abab36dd3b89d0498be6d2bb8b7f9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Sun, 2 Feb 2020 19:52:08 +0000 Subject: [PATCH 19/27] fixed unit test --- gitea/issue_comment_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/issue_comment_test.go b/gitea/issue_comment_test.go index e2a17be..385a1d3 100644 --- a/gitea/issue_comment_test.go +++ b/gitea/issue_comment_test.go @@ -51,7 +51,7 @@ func TestIssueComment(t *testing.T) { assert.NoError(t, c.AdminDeleteUser(tUser3.UserName)) // ListRepoIssueComments - comments, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListRepoIssueCommentsOptions{}) + comments, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListIssueCommentsOptions{}) assert.NoError(t, err) assert.Len(t, comments, 7) -- 2.40.1 From 47c7c25f061f103e703d6c1e2f218462d4e8d084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Mon, 3 Feb 2020 14:24:13 +0000 Subject: [PATCH 22/27] opt setDefaults on issue_comment --- gitea/issue_comment.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index d609900..cc8cf9f 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -47,6 +47,7 @@ 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, opt.PageSize) @@ -55,6 +56,7 @@ func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssu // 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, opt.PageSize) -- 2.40.1 From 4bb7a8eb374fd09b7667570b260fa4c3df89ac6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Tue, 4 Feb 2020 16:00:46 +0000 Subject: [PATCH 23/27] added sleep in order to increase CI stability --- gitea/issue_test.go | 2 ++ 1 file changed, 2 insertions(+) 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) } -- 2.40.1 From 75f40d10ae798444196c55e9e8dbce10dbe00fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Tue, 4 Feb 2020 16:02:52 +0000 Subject: [PATCH 24/27] reverted makefile changes --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 500d77a..a9e9f8b 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ test: test-instance: rm -r ${WORK_DIR}/test 2> /dev/null; \ mkdir -p ${WORK_DIR}/test/conf/ ${WORK_DIR}/test/data/ - wget "https://dl.gitea.io/gitea/master/gitea-master-darwin-10.6-amd64" -O ${WORK_DIR}/test/gitea-master; \ + wget "https://dl.gitea.io/gitea/master/gitea-master-linux-amd64" -O ${WORK_DIR}/test/gitea-master; \ chmod +x ${WORK_DIR}/test/gitea-master; \ echo "[security]" > ${WORK_DIR}/test/conf/app.ini; \ echo "INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4" >> ${WORK_DIR}/test/conf/app.ini; \ -- 2.40.1 From f66ed15508904254e8a7727d7f85af551fc6c131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Tue, 4 Feb 2020 16:27:05 +0000 Subject: [PATCH 25/27] ListMyRepos now receives a value instead of a pointer --- gitea/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/repo.go b/gitea/repo.go index 633e6c9..309702e 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -63,7 +63,7 @@ type ListReposOptions struct { } // ListMyRepos lists all repositories for the authenticated user that has access to. -func (c *Client) ListMyRepos(opt *ListReposOptions) ([]*Repository, error) { +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) -- 2.40.1 From e9558f36d958ab56932040804765938094e3c82f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Tue, 4 Feb 2020 16:42:31 +0000 Subject: [PATCH 26/27] small improvements --- gitea/status.go | 6 +++--- gitea/user_app.go | 8 ++++---- gitea/user_test.go | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gitea/status.go b/gitea/status.go index 8986550..f072cf8 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -59,13 +59,13 @@ 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) } -// ListStatusesOptions options for listing a repository's commit's statuses -type ListStatusesOptions struct { +// ListStatusesOption options for listing a repository's commit's statuses +type ListStatusesOption struct { ListOptions } // ListStatuses returns all statuses for a given Commit -func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOptions) ([]*Status, error) { +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) diff --git a/gitea/user_app.go b/gitea/user_app.go index 4877d02..ba3de6a 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -26,13 +26,13 @@ type AccessToken struct { TokenLastEight string `json:"token_last_eight"` } -// ListAccessTokens options for listing a users's access tokens -type ListAccessTokens struct { +// ListAccessTokensOptions options for listing a users's access tokens +type ListAccessTokensOptions struct { ListOptions } -// ListAccessTokens lista all the access tokens of user -func (c *Client) ListAccessTokens(user, pass string, opt ListAccessTokens) ([]*AccessToken, error) { +// ListAccessTokensOptions lists all the access tokens of user +func (c *Client) ListAccessTokens(user, pass string, opt ListAccessTokensOptions) ([]*AccessToken, error) { opt.setDefaults() tokens := make([]*AccessToken, 0, opt.PageSize) return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, opt.getURLQuery().Encode()), diff --git a/gitea/user_test.go b/gitea/user_test.go index 958a64b..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, ListAccessTokens{}) + 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, ListAccessTokens{}) + 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, ListAccessTokens{}) + result, _ = c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{}) assert.Len(t, result, 1) } -- 2.40.1 From ed84b633339e3a19aef99c34c35b43682994d580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hila=CC=81rio=20Coelho?= Date: Tue, 4 Feb 2020 21:44:55 +0000 Subject: [PATCH 27/27] fix golint issue --- gitea/user_app.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gitea/user_app.go b/gitea/user_app.go index ba3de6a..bbb3e13 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -31,11 +31,11 @@ type ListAccessTokensOptions struct { ListOptions } -// ListAccessTokensOptions lists all the access tokens of user -func (c *Client) ListAccessTokens(user, pass string, opt ListAccessTokensOptions) ([]*AccessToken, error) { - opt.setDefaults() - tokens := make([]*AccessToken, 0, opt.PageSize) - return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, opt.getURLQuery().Encode()), +// 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) } -- 2.40.1