Skip to content

Commit

Permalink
fix: support repos pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix committed Jul 1, 2023
1 parent 0eef3fc commit 868cefb
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions internal/services/preferences/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
ErrHookNotExists = errors.New("hook not exists")
)

const repoPageSize = 60

type Hooks map[string]int64

type HookStore interface {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 868cefb

Please sign in to comment.