From 868cefb46f87eabcac66f5bc4ccb1b6419241eba Mon Sep 17 00:00:00 2001 From: x1unix Date: Sun, 2 Jul 2023 01:06:32 +0200 Subject: [PATCH] fix: support repos pagination --- internal/services/preferences/github.go | 42 +++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/internal/services/preferences/github.go b/internal/services/preferences/github.go index b0165e5..d57067f 100644 --- a/internal/services/preferences/github.go +++ b/internal/services/preferences/github.go @@ -20,6 +20,8 @@ var ( ErrHookNotExists = errors.New("hook not exists") ) +const repoPageSize = 60 + type Hooks map[string]int64 type HookStore interface { @@ -121,12 +123,7 @@ func (svc GitHubService) getOAuthToken(ctx context.Context, uid auth.UserID) (*o // GetUntrackedRepositories returns a list of available untracked repositories. func (svc GitHubService) GetUntrackedRepositories(ctx context.Context, uid auth.UserID) ([]string, error) { - client, err := svc.getClient(ctx, uid) - if err != nil { - return nil, err - } - - repos, _, err := client.Repositories.List(ctx, "", &github.RepositoryListOptions{}) + repos, err := svc.fetchUserRepos(ctx, uid) if err != nil { //github.ErrorResponse return nil, err @@ -147,6 +144,39 @@ func (svc GitHubService) GetUntrackedRepositories(ctx context.Context, uid auth. ), nil } +func (svc GitHubService) fetchUserRepos(ctx context.Context, uid auth.UserID) ([]*github.Repository, error) { + client, err := svc.getClient(ctx, uid) + if err != nil { + return nil, err + } + + opts := &github.RepositoryListOptions{ + Affiliation: "owner", + Sort: "created", + Direction: "desc", + ListOptions: github.ListOptions{ + PerPage: repoPageSize, + }, + } + + results := make([]*github.Repository, 0, repoPageSize) + for { + repos, resp, err := client.Repositories.List(ctx, "", opts) + if err != nil { + return nil, err + } + + results = append(results, repos...) + if resp.NextPage == 0 { + break + } + + opts.Page = resp.NextPage + } + + return results, nil +} + func (svc GitHubService) GetTrackedRepositories(ctx context.Context, uid auth.UserID) ([]string, error) { return svc.hookStore.GetHookRepositories(ctx, uid) }