From 524bda1e14870e4150a661e6b4587896f745ad43 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Tue, 4 Feb 2020 07:58:46 +0000 Subject: [PATCH] Use StateType (#265) use StateType Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/265 Reviewed-by: Lunny Xiao Reviewed-by: lafriks --- gitea/issue.go | 21 ++++++++++++++++----- gitea/issue_milestone.go | 12 ------------ gitea/issue_test.go | 4 +++- gitea/pull.go | 9 ++++----- gitea/pull_test.go | 2 +- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index 464ac80..8c7d9d0 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -46,13 +46,24 @@ type Issue struct { // ListIssueOption list issue options type ListIssueOption struct { - Page int - // open, closed, all - State string + Page int + State StateType Labels []string KeyWord string } +// StateType issue state type +type StateType string + +const ( + // StateOpen pr is opend + StateOpen StateType = "open" + // StateClosed pr is closed + StateClosed StateType = "closed" + // StateAll is all + StateAll StateType = "all" +) + // QueryEncode turns options into querystring argument func (opt *ListIssueOption) QueryEncode() string { query := make(url.Values) @@ -60,7 +71,7 @@ func (opt *ListIssueOption) QueryEncode() string { query.Add("page", fmt.Sprintf("%d", opt.Page)) } if len(opt.State) > 0 { - query.Add("state", opt.State) + query.Add("state", string(opt.State)) } if len(opt.Labels) > 0 { var lq string @@ -134,7 +145,7 @@ type EditIssueOption struct { Assignee *string `json:"assignee"` Assignees []string `json:"assignees"` Milestone *int64 `json:"milestone"` - State *string `json:"state"` + State *StateType `json:"state"` Deadline *time.Time `json:"due_date"` } diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index dfbe1cc..a174c19 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -12,18 +12,6 @@ import ( "time" ) -// StateType issue state type -type StateType string - -const ( - // StateOpen pr is opend - StateOpen StateType = "open" - // StateClosed pr is closed - StateClosed StateType = "closed" - // StateAll is all - StateAll StateType = "all" -) - // Milestone milestone is a collection of issues on one repository type Milestone struct { ID int64 `json:"id"` diff --git a/gitea/issue_test.go b/gitea/issue_test.go index 75654b0..f83a382 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -86,9 +86,11 @@ func editIssues(t *testing.T, c *Client) { assert.NoError(t, err) body := "123 test and go" + state := StateClosed issueNew, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{ Title: "Edited", Body: &body, + State: &state, }) assert.NoError(t, err) assert.EqualValues(t, issue.ID, issueNew.ID) @@ -117,5 +119,5 @@ func listIssues(t *testing.T, c *Client) { issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{}) assert.NoError(t, err) - assert.Len(t, issues, 4) + assert.Len(t, issues, 3) } diff --git a/gitea/pull.go b/gitea/pull.go index 274ecd5..1faecc7 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -59,9 +59,8 @@ type PullRequest struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - Page int `json:"page"` - // open, closed, all - State string `json:"state"` + Page int `json:"page"` + State StateType `json:"state"` // oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority Sort string `json:"sort"` Milestone int64 `json:"milestone"` @@ -78,7 +77,7 @@ func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOp query.Add("page", fmt.Sprintf("%d", opt.Page)) } if len(opt.State) > 0 { - query.Add("state", opt.State) + query.Add("state", string(opt.State)) } if len(opt.Sort) > 0 { query.Add("sort", opt.Sort) @@ -129,7 +128,7 @@ type EditPullRequestOption struct { Assignees []string `json:"assignees"` Milestone int64 `json:"milestone"` Labels []int64 `json:"labels"` - State *string `json:"state"` + State *StateType `json:"state"` Deadline *time.Time `json:"due_date"` } diff --git a/gitea/pull_test.go b/gitea/pull_test.go index 7c391eb..5fb85fd 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -26,7 +26,7 @@ func TestPull(t *testing.T) { // ListRepoPullRequests list PRs of one repository pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{ Page: 1, - State: "all", + State: StateAll, Sort: "leastupdate", }) assert.NoError(t, err)