diff --git a/gitea/issue.go b/gitea/issue.go index f5b2512..464ac80 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -56,13 +56,6 @@ type ListIssueOption struct { // QueryEncode turns options into querystring argument func (opt *ListIssueOption) QueryEncode() string { query := make(url.Values) - if opt.Page > 0 { - query.Add("page", fmt.Sprintf("%d", opt.Page)) - } - if len(opt.State) > 0 { - query.Add("state", opt.State) - } - if opt.Page > 0 { query.Add("page", fmt.Sprintf("%d", opt.Page)) } diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 8527079..151dae0 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -25,16 +26,38 @@ type Comment struct { Updated time.Time `json:"updated_at"` } +// ListIssueCommentOptions list comment options +type ListIssueCommentOptions struct { + Since time.Time + Before time.Time +} + +// QueryEncode turns options into querystring argument +func (opt *ListIssueCommentOptions) QueryEncode() string { + query := make(url.Values) + if !opt.Since.IsZero() { + query.Add("since", opt.Since.Format(time.RFC3339)) + } + if !opt.Before.IsZero() { + query.Add("before", opt.Before.Format(time.RFC3339)) + } + return query.Encode() +} + // ListIssueComments list comments on an issue. -func (c *Client) ListIssueComments(owner, repo string, index int64) ([]*Comment, error) { +func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssueCommentOptions) ([]*Comment, error) { + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index)) + link.RawQuery = opt.QueryEncode() comments := make([]*Comment, 0, 10) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), nil, nil, &comments) + return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments) } // ListRepoIssueComments list comments for a given repo. -func (c *Client) ListRepoIssueComments(owner, repo string) ([]*Comment, error) { +func (c *Client) ListRepoIssueComments(owner, repo string, opt ListIssueCommentOptions) ([]*Comment, error) { + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo)) + link.RawQuery = opt.QueryEncode() comments := make([]*Comment, 0, 10) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo), nil, nil, &comments) + return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments) } // GetIssueComment get a comment for a given repo by id. diff --git a/gitea/issue_comment_test.go b/gitea/issue_comment_test.go index 6a63d9a..e8731c0 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, ListIssueCommentOptions{}) 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, ListIssueCommentOptions{}) assert.NoError(t, err) assert.Len(t, comments, 3)