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

chore: code cleanup #4

Merged
merged 1 commit into from
Aug 15, 2023
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
9 changes: 8 additions & 1 deletion pagination/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagination

import (
"context"

"github.com/google/go-github/v53/github"
)

Expand Down Expand Up @@ -57,16 +58,18 @@ type PaginatorOpts struct {
}

func Paginator[T any](ctx context.Context, listFunc ListFunc[T], processFunc ProcessFunc[T], rateLimitFunc RateLimitFunc, Opts *PaginatorOpts) ([]T, error) {
var allItems []T

opts := listOpts(Opts)
var allItems []T

for {
items, resp, err := listFunc.List(ctx, opts)
if err != nil {
return allItems, err
}

allItems = append(allItems, items...)

for _, item := range items {
if err = processFunc.Process(ctx, item); err != nil {
return allItems, err
Expand All @@ -78,21 +81,25 @@ func Paginator[T any](ctx context.Context, listFunc ListFunc[T], processFunc Pro
if err != nil {
return allItems, err
}

if !shouldContinue {
break
}

if resp.NextPage == 0 {
break
}

opts.Page = resp.NextPage
}

return allItems, nil
}

func listOpts(opts *PaginatorOpts) *github.ListOptions {
if opts == nil || opts.ListOptions == nil {
return &github.ListOptions{PerPage: 100, Page: 1}
}

return opts.ListOptions
}
17 changes: 15 additions & 2 deletions pagination/paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (l *listFunc) List(ctx context.Context, opt *github.ListOptions) ([]*github
Visibility: "public",
ListOptions: *opt,
}

t, r, err := l.client.Repositories.List(ctx, "xorima", &rOpts)

return t, r, err
}

Expand All @@ -77,18 +79,22 @@ func Test_Paginator(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

pFunc := &processFunc{client: client}
rFunc := &rateLimitFunc{}
lFunc := &listFunc{client: client}
opts := PaginatorOpts{ListOptions: &github.ListOptions{Page: 1, PerPage: 10}}

resp, err := Paginator[*github.Repository](context.Background(), lFunc, pFunc, rFunc, &opts)
assert.NoError(t, err)
assert.Len(t, resp, 59)
})

t.Run("should return when ratelimter returns a false response", func(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator-with-opts")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

want := 2
pFunc := &processFunc{client: client}
rFunc := &rateLimitReturnNowFunc{}
Expand All @@ -98,12 +104,13 @@ func Test_Paginator(t *testing.T) {
resp, err := Paginator[*github.Repository](context.Background(), lFunc, pFunc, rFunc, &opts)
assert.NoError(t, err)
assert.Len(t, resp, want)

})

t.Run("should use default opts if none provided", func(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator-default-opts")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

pFunc := &processFunc{client: client}
rFunc := &rateLimitFunc{}
lFunc := &listFunc{client: client}
Expand All @@ -117,6 +124,7 @@ func Test_Paginator(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator-list")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

pFunc := &processFunc{client: client}
rFunc := &rateLimitFunc{}
lFunc := &listErrorFunc{client: client}
Expand All @@ -130,17 +138,20 @@ func Test_Paginator(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator-rate-limit")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

pFunc := &processFunc{client: client}
rFunc := &rateLimitErrorFunc{}
lFunc := &listFunc{client: client}

_, err := Paginator[*github.Repository](context.Background(), lFunc, pFunc, rFunc, nil)
assert.Error(t, err)
})

t.Run("should return any error encountered by the process function", func(t *testing.T) {
client, r := newVcrGithubClient("fixtures/paginator-process")
//nolint:errcheck // this is used as a cleanup
defer r.Stop()

pFunc := &processErrorFunc{client: client}
rFunc := &rateLimitFunc{}
lFunc := &listFunc{client: client}
Expand All @@ -163,6 +174,7 @@ func newVcrGithubClient(vcrPath string) (*github.Client, *recorder.Recorder) {

// Start our recorder
opts := recorder.Options{RealTransport: tr, CassetteName: vcrPath, Mode: recorder.ModeReplayWithNewEpisodes}

r, err := recorder.NewWithOptions(&opts)
if err != nil {
panic(err)
Expand All @@ -179,12 +191,13 @@ func newVcrGithubClient(vcrPath string) (*github.Client, *recorder.Recorder) {

return nil
}

r.AddHook(hook, recorder.AfterCaptureHook)

// custom http.client
httpClient := &http.Client{
Transport: r,
}
return github.NewClient(httpClient), r

return github.NewClient(httpClient), r
}