Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ListIssues GitHub API #139

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ type Client interface {
CreateComment(
context.Context, string, string, int, string,
) (*github.IssueComment, *github.Response, error)
ListIssues(
context.Context, string, string, *github.IssueListByRepoOptions,
) ([]*github.Issue, *github.Response, error)
}

// NewIssueOptions is a struct of optional fields for new issues
Expand Down Expand Up @@ -339,7 +342,7 @@ func (g *githubClient) ListBranches(
) ([]*github.Branch, *github.Response, error) {
branches, response, err := g.Repositories.ListBranches(ctx, owner, repo, opt)
if err != nil {
return nil, nil, fmt.Errorf("fetching brnaches from repo: %w", err)
return nil, nil, fmt.Errorf("fetching branches from repo: %w", err)
}

return branches, response, nil
Expand Down Expand Up @@ -478,6 +481,17 @@ func (g *githubClient) CreateComment(
}
}

func (g *githubClient) ListIssues(
ctx context.Context, owner, repo string, opts *github.IssueListByRepoOptions,
) ([]*github.Issue, *github.Response, error) {
issues, response, err := g.Issues.ListByRepo(ctx, owner, repo, opts)
if err != nil {
return nil, nil, fmt.Errorf("fetching issues from repo: %w", err)
}

return issues, 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 @@ -1047,3 +1061,41 @@ func (g *githubClient) AddLabels(
}
}
}

// IssueState is the enum for all available issue states.
type IssueState string

const (
// IssueStateAll can be used to list all issues.
IssueStateAll IssueState = "all"

// IssueStateOpen can be used to list only open issues.
IssueStateOpen IssueState = "open"

// IssueStateClosed can be used to list only closed issues.
IssueStateClosed IssueState = "closed"
)

// ListIssues gets the issues from a GitHub repository.
// State filters issues based on their state. Possible values are: open,
// closed, all. Default is "open".
func (g *GitHub) ListIssues(owner, repo string, state IssueState) ([]*github.Issue, error) {
options := &github.IssueListByRepoOptions{
State: string(state),
ListOptions: github.ListOptions{PerPage: g.Options().GetItemsPerPage()},
}
issues := []*github.Issue{}
for {
more, r, err := g.Client().ListIssues(context.Background(), owner, repo, options)
if err != nil {
return issues, fmt.Errorf("getting issues from client: %w", err)
}
issues = append(issues, more...)
if r.NextPage == 0 {
break
}
options.Page = r.NextPage
}

return issues, nil
}
27 changes: 27 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,30 @@ func TestAddLabels(t *testing.T) {
}
}
}

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

issue0 := "My title 1"
issue1 := "Create XYZ"
issue2 := "foo-bar"

issues := []*gogithub.Issue{
{Title: &issue0},
{Title: &issue1},
{Title: &issue2},
}

client.ListIssuesReturns(issues, &gogithub.Response{NextPage: 0}, nil)

// When
result, err := sut.ListIssues("kubernetes", "kubernotia", github.IssueStateOpen)

// Then
require.Nil(t, err)
require.Len(t, result, 3)
require.Equal(t, result[0].GetTitle(), issue0)
require.Equal(t, result[1].GetTitle(), issue1)
require.Equal(t, result[2].GetTitle(), issue2)
}
90 changes: 90 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 @@ -49,6 +49,7 @@ const (
gitHubAPIListReleaseAssets gitHubAPI = "ListReleaseAssets"
gitHubAPICreateComment gitHubAPI = "CreateComment"
gitHubAPIListMilestones gitHubAPI = "ListMilestones"
gitHubAPIListIssues gitHubAPI = "ListIssues"
)

type apiRecord struct {
Expand Down Expand Up @@ -311,6 +312,19 @@ func (c *githubNotesRecordClient) CreateComment(ctx context.Context, owner, repo
return issueComment, resp, nil
}

func (c *githubNotesRecordClient) ListIssues(
ctx context.Context, owner, repo string, opts *github.IssueListByRepoOptions,
) ([]*github.Issue, *github.Response, error) {
issues, resp, err := c.client.ListIssues(ctx, owner, repo, opts)
if err != nil {
return nil, nil, err
}
if err := c.recordAPICall(gitHubAPIListIssues, issues, resp); err != nil {
return nil, nil, err
}
return issues, 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 @@ -321,3 +321,18 @@ func (c *githubNotesReplayClient) CreateComment(ctx context.Context, owner, repo
}
return result, record.response(), nil
}

func (c *githubNotesReplayClient) ListIssues(
ctx context.Context, owner, repo string, opts *github.IssueListByRepoOptions,
) ([]*github.Issue, *github.Response, error) {
data, err := c.readRecordedData(gitHubAPIListIssues)
if err != nil {
return nil, nil, err
}
issues := make([]*github.Issue, 0)
record := apiRecord{Result: issues}
if err := json.Unmarshal(data, &record); err != nil {
return nil, nil, err
}
return issues, record.response(), nil
}