From 2dcc3b5d037f7f23a609583b176405dd1ba0900b Mon Sep 17 00:00:00 2001 From: bendtherules Date: Wed, 6 Jun 2018 12:54:19 +0530 Subject: [PATCH] Support pagination in getContributors + Rename ResponseWithPaginationAndMeta to ResponseWithDataArrayAndMeta --- src/gh.ts | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/gh.ts b/src/gh.ts index 82c4305..6a8fce2 100644 --- a/src/gh.ts +++ b/src/gh.ts @@ -18,7 +18,7 @@ interface ResponseWithMetaLink { }; } -interface ResponseWithPaginationAndMeta extends ResponseWithDataArray, ResponseWithMetaLink { +interface ResponseWithDataArrayAndMeta extends ResponseWithDataArray, ResponseWithMetaLink { } @@ -44,9 +44,8 @@ interface ResponseFromCompareCommits { }; } -interface ResponseFromGetContributors { +interface ResponseFromGetContributors extends ResponseWithMetaLink { data: OwnerFromGetContributors[] | undefined; - meta: {}; } interface RepoFromGetUserRepo { @@ -120,14 +119,14 @@ interface ObjectWithPerPage { } async function paginate( - method: (args: TFirstParam) => Promise>, + method: (args: TFirstParam) => Promise>, args: TFirstParam ): Promise> { // Set per_page args.per_page = 100; - let response: ResponseWithPaginationAndMeta = await method(args); + let response: ResponseWithDataArrayAndMeta = await method(args); // Concat all data let { data } = response; @@ -255,30 +254,43 @@ async function fetchNoneOfForkBranchesIsAhead( async function fetchUserIsNotContributor( repoName: string ): Promise { - const responseFromGetContributors: ResponseFromGetContributors = ((await octokit.repos.getContributors( - { - owner: username, - repo: repoName, - anon: '0', - per_page: 100, - page: 1, - } - )) as any) as ResponseFromGetContributors; + const params: rest.ReposGetContributorsParams = { + owner: username, + repo: repoName, + anon: '0', + }; - let contributors = responseFromGetContributors.data; + const responseFromGetContributors: ResponseWithDataArray = + await paginate( + async (tmpFirstParam: rest.ReposGetContributorsParams): Promise> => { + // Modify getContributors to return empty contributor data array instead of undefined for empty repos + const response = await (octokit.repos.getContributors(tmpFirstParam) as any as Promise); - if (contributors === undefined) { - contributors = []; - } + let dataNormalized = response.data; + if (dataNormalized === undefined) { + dataNormalized = []; + } - const foundContributor = contributors.find( + const responseNormalized: ResponseWithDataArrayAndMeta = { + data: dataNormalized, + meta: response.meta + }; + + return responseNormalized; + }, + params + ); + + const contributors = responseFromGetContributors.data; + + const matchingContributor = contributors.find( tmpContributor => tmpContributor.login === username ); return { // tslint:disable-next-line:object-literal-shorthand repoName: repoName, - unused: foundContributor === undefined, + unused: matchingContributor === undefined, }; }