From f224b4e50c390eabac80b19c267e7a5f02160cd5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Wed, 20 May 2020 06:48:06 +0000 Subject: [PATCH] ListIssues: add milestones filter (#327) use string.Join Code Format add test case add Milestones to ListIssueOption Co-authored-by: Lunny Xiao Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/327 Reviewed-by: Andrew Thornton Reviewed-by: Lunny Xiao --- gitea/issue.go | 26 ++++++++++++++------------ gitea/issue_test.go | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index 02479a6..0e57147 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -56,10 +56,11 @@ type Issue struct { // ListIssueOption list issue options type ListIssueOption struct { ListOptions - State StateType - Type IssueType - Labels []string - KeyWord string + State StateType + Type IssueType + Labels []string + Milestones []string + KeyWord string } // StateType issue state type @@ -89,24 +90,25 @@ const ( // QueryEncode turns options into querystring argument func (opt *ListIssueOption) QueryEncode() string { query := opt.getURLQuery() + if len(opt.State) > 0 { query.Add("state", string(opt.State)) } + if len(opt.Labels) > 0 { - var lq string - for _, l := range opt.Labels { - if len(lq) > 0 { - lq += "," - } - lq += l - } - query.Add("labels", lq) + query.Add("labels", strings.Join(opt.Labels, ",")) } + if len(opt.KeyWord) > 0 { query.Add("q", opt.KeyWord) } + query.Add("type", string(opt.Type)) + if len(opt.Milestones) > 0 { + query.Add("milestones", strings.Join(opt.Milestones, ",")) + } + return query.Encode() } diff --git a/gitea/issue_test.go b/gitea/issue_test.go index a707494..cc6b056 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -71,12 +71,12 @@ func listIssues(t *testing.T, c *Client) { log.Println("== TestListIssues ==") issues, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{ - Labels: []string{"Label2"}, + Labels: []string{"Label1", "Label2"}, KeyWord: "", State: "all", }) assert.NoError(t, err) - assert.Len(t, issues, 2) + assert.Len(t, issues, 1) issues, err = c.ListIssues(ListIssueOption{ Labels: []string{"Label2"}, @@ -86,6 +86,18 @@ func listIssues(t *testing.T, c *Client) { assert.NoError(t, err) assert.Len(t, issues, 1) + issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{ + Milestones: []string{"mile1"}, + State: "all", + }) + assert.NoError(t, err) + assert.Len(t, issues, 3) + for i := range issues { + if assert.NotNil(t, issues[i].Milestone) { + assert.EqualValues(t, "mile1", issues[i].Milestone.Title) + } + } + issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{}) assert.NoError(t, err) assert.Len(t, issues, 3)