From 45364c385a4a1d7b05067a0043079c8378b96228 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Wed, 15 Oct 2014 21:55:30 -0700 Subject: [PATCH] Handle pagination when fetching the organization repositories. --- app/repos.go | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/app/repos.go b/app/repos.go index 98f6d45..399a3ca 100644 --- a/app/repos.go +++ b/app/repos.go @@ -216,17 +216,20 @@ type OrgRepos struct { } func getRepos(c appengine.Context, githubClient *github.Client, account *Account, user *github.User) (*Repos, error) { - // The username parameter must be left blank so that we can get all of the - // repositories the user has access to, not just ones that they own. clientUserRepos := make([]github.Repository, 0) page := 1 for { - pageClientUserRepos, response, err := githubClient.Repositories.List("", &github.RepositoryListOptions{ - ListOptions: github.ListOptions{ - Page: page, - PerPage: 100, - }, - }) + pageClientUserRepos, response, err := githubClient.Repositories.List( + // The username parameter must be left blank so that we can get all + // of the repositories the user has access to, not just ones that + // they own. + "", + &github.RepositoryListOptions{ + ListOptions: github.ListOptions{ + Page: page, + PerPage: 100, + }, + }) if err != nil { return nil, err } @@ -264,7 +267,13 @@ func getRepos(c appengine.Context, githubClient *github.Client, account *Account } } - orgs, _, err := githubClient.Organizations.List("", nil) + orgs, _, err := githubClient.Organizations.List( + "", + &github.ListOptions{ + // Don't bother with pagination for the organization list, the user + // is unlikely to have that many. + PerPage: 100, + }) if err != nil { return nil, err } @@ -272,11 +281,26 @@ func getRepos(c appengine.Context, githubClient *github.Client, account *Account repos.OrgRepos = make([]*OrgRepos, 0, len(orgs)) for i := range orgs { org := &orgs[i] - clientOrgRepos, _, err := githubClient.Repositories.ListByOrg(*org.Login, nil) - if err != nil { - return nil, err + clientOrgRepos := make([]github.Repository, 0) + page := 1 + for { + pageClientOrgRepos, response, err := githubClient.Repositories.ListByOrg( + *org.Login, + &github.RepositoryListByOrgOptions{ + ListOptions: github.ListOptions{ + Page: page, + PerPage: 100, + }, + }) + if err != nil { + return nil, err + } + clientOrgRepos = append(clientOrgRepos, pageClientOrgRepos...) + if response.NextPage == 0 { + break + } + page = response.NextPage } - orgRepos := make([]*Repo, 0, len(clientOrgRepos)) allRepoCount += len(clientOrgRepos) for j := range clientOrgRepos {