Skip to content

Commit

Permalink
Merge pull request #148 from justaugustus/gh-comments
Browse files Browse the repository at this point in the history
github: Add `ListComments` method
  • Loading branch information
k8s-ci-robot committed Jan 4, 2023
2 parents 1e6a900 + 7e24459 commit c885fdb
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
70 changes: 70 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/google/go-github/v48/github"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -146,6 +147,9 @@ type Client interface {
ListIssues(
context.Context, string, string, *github.IssueListByRepoOptions,
) ([]*github.Issue, *github.Response, error)
ListComments(
context.Context, string, string, int, *github.IssueListCommentsOptions,
) ([]*github.IssueComment, *github.Response, error)
}

// NewIssueOptions is a struct of optional fields for new issues
Expand Down Expand Up @@ -492,6 +496,20 @@ func (g *githubClient) ListIssues(
return issues, response, nil
}

func (g *githubClient) ListComments(
ctx context.Context,
owner, repo string,
number int,
opts *github.IssueListCommentsOptions,
) ([]*github.IssueComment, *github.Response, error) {
comments, response, err := g.Issues.ListComments(ctx, owner, repo, number, opts)
if err != nil {
return nil, nil, fmt.Errorf("fetching comments from issue: %w", err)
}

return comments, response, nil
}

// SetClient can be used to manually set the internal GitHub client
func (g *GitHub) SetClient(client Client) {
g.client = client
Expand Down Expand Up @@ -1099,3 +1117,55 @@ func (g *GitHub) ListIssues(owner, repo string, state IssueState) ([]*github.Iss

return issues, nil
}

// Sort specifies how to sort comments. Possible values are: created, updated.
type Sort string

// SortDirection in which to sort comments. Possible values are: asc, desc.
type SortDirection string

const (
SortCreated Sort = "created"
SortUpdated Sort = "updated"

SortDirectionAscending SortDirection = "asc"
SortDirectionDescending SortDirection = "desc"
)

// ListComments lists all comments on the specified issue. Specifying an issue
// number of 0 will return all comments on all issues for the repository.
//
// GitHub API docs: https://docs.github.com/en/rest/issues/comments#list-issue-comments
// GitHub API docs: https://docs.github.com/en/rest/issues/comments#list-issue-comments-for-a-repository
func (g *GitHub) ListComments(
owner, repo string,
issueNumber int,
sort Sort,
direction SortDirection,
since *time.Time,
) ([]*github.IssueComment, error) {
options := &github.IssueListCommentsOptions{
Sort: github.String(string(sort)),
Direction: github.String(string(direction)),
ListOptions: github.ListOptions{PerPage: g.Options().GetItemsPerPage()},
}

if since != nil {
options.Since = since
}

comments := []*github.IssueComment{}
for {
more, r, err := g.Client().ListComments(context.Background(), owner, repo, issueNumber, options)
if err != nil {
return comments, fmt.Errorf("getting comments from client: %w", err)
}
comments = append(comments, more...)
if r.NextPage == 0 {
break
}
options.Page = r.NextPage
}

return comments, nil
}
37 changes: 37 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"testing"
"time"

gogithub "github.com/google/go-github/v48/github"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -614,3 +615,39 @@ func TestListIssues(t *testing.T) {
require.Equal(t, result[1].GetTitle(), issue1)
require.Equal(t, result[2].GetTitle(), issue2)
}

func TestListComments(t *testing.T) {
// Given
sut, client := newSUT()

comment0 := "comment 0"
comment1 := "comment 1"
comment2 := "comment 2"

comments := []*gogithub.IssueComment{
{Body: &comment0},
{Body: &comment1},
{Body: &comment2},
}

client.ListCommentsReturns(comments, &gogithub.Response{NextPage: 0}, nil)

since := time.Now()

// When
result, err := sut.ListComments(
"fake-owner",
"fake-repo",
1,
github.SortCreated,
github.SortDirectionAscending,
&since,
)

// Then
require.Nil(t, err)
require.Len(t, result, 3)
require.Equal(t, result[0].GetBody(), comment0)
require.Equal(t, result[1].GetBody(), comment1)
require.Equal(t, result[2].GetBody(), comment2)
}
92 changes: 92 additions & 0 deletions github/githubfakes/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions github/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
gitHubAPICreateComment gitHubAPI = "CreateComment"
gitHubAPIListMilestones gitHubAPI = "ListMilestones"
gitHubAPIListIssues gitHubAPI = "ListIssues"
gitHubAPIListComments gitHubAPI = "ListComments"
)

type apiRecord struct {
Expand Down Expand Up @@ -325,6 +326,19 @@ func (c *githubNotesRecordClient) ListIssues(
return issues, resp, nil
}

func (c *githubNotesRecordClient) ListComments(
ctx context.Context, owner, repo string, number int, opts *github.IssueListCommentsOptions,
) ([]*github.IssueComment, *github.Response, error) {
comments, resp, err := c.client.ListComments(ctx, owner, repo, number, opts)
if err != nil {
return nil, nil, err
}
if err := c.recordAPICall(gitHubAPIListComments, comments, resp); err != nil {
return nil, nil, err
}
return comments, resp, nil
}

// recordAPICall records a single GitHub API call into a JSON file by ensuring
// naming conventions
func (c *githubNotesRecordClient) recordAPICall(
Expand Down
15 changes: 15 additions & 0 deletions github/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,18 @@ func (c *githubNotesReplayClient) ListIssues(
}
return issues, record.response(), nil
}

func (c *githubNotesReplayClient) ListComments(
ctx context.Context, owner, repo string, number int, opts *github.IssueListCommentsOptions,
) ([]*github.IssueComment, *github.Response, error) {
data, err := c.readRecordedData(gitHubAPIListComments)
if err != nil {
return nil, nil, err
}
comments := make([]*github.IssueComment, 0)
record := apiRecord{Result: comments}
if err := json.Unmarshal(data, &record); err != nil {
return nil, nil, err
}
return comments, record.response(), nil
}

0 comments on commit c885fdb

Please sign in to comment.