From 83d35b38ff1525b5bc88b2a6ab7dd88fe7ca947f Mon Sep 17 00:00:00 2001 From: bendtherules Date: Tue, 5 Jun 2018 18:30:00 +0530 Subject: [PATCH] Add generic pagination util + use in branch fetch --- src/gh.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/src/gh.ts b/src/gh.ts index bd473ba..8d05271 100644 --- a/src/gh.ts +++ b/src/gh.ts @@ -8,14 +8,27 @@ octokit.authenticate({ secret: '2228539a48032f0622d6c12a66f56253d0a30d60', }); +interface ResponseWithDataArray { + data: T[]; +} + +interface ResponseWithMetaLink { + meta: { + link: string + }; +} + +interface ResponseWithPaginationAndMeta extends ResponseWithDataArray, ResponseWithMetaLink { + +} + interface ResponseFromGetUserRepo { data: RepoFromGetUserRepo[]; meta: {}; } -interface ResponseFromGetBranches { +interface ResponseFromGetBranches extends ResponseWithMetaLink { data: BranchFromGetBranches[]; - meta: {}; } interface ResponseFromGetRepo { @@ -99,20 +112,51 @@ interface RepoNameWithParentRepo { interface RepoNameWithBranchesAndParent extends RepoNameWithBranches, - RepoNameWithParentRepo {} + RepoNameWithParentRepo { } const username = 'bendtherules'; +interface ObjectWithPerPage { + per_page?: number; +} + +async function paginate( + method: (args: TFirstParam) => ResponseWithPaginationAndMeta, + args: TFirstParam +): Promise> { + + // Set per_page + args.per_page = 100; + + let response: ResponseWithPaginationAndMeta = await method(args); + + // Concat all data + let { data } = response; + while (octokit.hasNextPage(response)) { + response = await octokit.getNextPage(response); + data = data.concat(response.data); + } + + return { + data + }; +} + async function fetchRepoNameWithBranches( repoName: string ): Promise { - const branchesResponse: ResponseFromGetBranches = await octokit.repos.getBranches( - { - owner: username, - repo: repoName, - per_page: 10, // change this - } - ); + const params: rest.ReposGetBranchesParams = { + owner: username, + repo: repoName, + }; + + const branchesResponse: ResponseWithDataArray = + await paginate( + (tmpFirstParam: rest.ReposGetBranchesParams): ResponseFromGetBranches => { + return octokit.repos.getBranches(tmpFirstParam) as any as ResponseFromGetBranches; + }, + params + ); return { repoName,