Add ListIssueCommentOptions for optional param #243

Merged
lunny merged 7 commits from 6543/go-sdk:impruve-ListXIssueComments into master 2020-02-03 03:18:19 +00:00
3 changed files with 29 additions and 13 deletions

View File

@ -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))
}

View File

@ -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.

View File

@ -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)