Skip to content

Commit

Permalink
refactor: make listPRs use paginate-generator pacakge
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Jun 21, 2021
1 parent 5bae7c1 commit f0f167c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"prettier": "@shelf/prettier-config",
"dependencies": {
"@octokit/rest": "18.6.0",
"lodash": "4.17.21"
"lodash": "4.17.21",
"paginate-generator": "1.2.7"
},
"devDependencies": {
"@babel/cli": "7.14.5",
Expand Down
66 changes: 25 additions & 41 deletions src/prs/list-prs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods';
import {all, paginate} from 'paginate-generator';
import {getClient} from '../rest-client';

interface ListOpenPRsParams {
Expand All @@ -20,52 +21,35 @@ export async function listClosedPRs(params: ListOpenPRsParams): ReturnType<typeo
return listPRs({...params, prStatus: 'closed'});
}

// TODO: Refactor to use https://github.com/holvonix-open/paginate-generator
async function listPRs(
params: ListPRsParams
): Promise<
RestEndpointMethodTypes['search']['issuesAndPullRequests']['response']['data']['items']
> {
const prs: unknown[] = [];
let page = 1;
let totalCount = 0;

do {
const data = await search({
page,
q: `is:${params.prStatus} is:pr archived:false user:${params.owner} ${
params.searchText || ''
}`.trim(),
});

totalCount = data.total_count;
prs.push(...data.items);

page++;
} while (prs.length < totalCount);
const prs: unknown[] = await all(
paginate(async (token?: number) => {
if ((token || 1) >= 11) {
return {
page: [],
};
}

const gh = getClient();

const {data} = await gh.search.issuesAndPullRequests({
per_page: 100,
page: token || 1,
q: `is:${params.prStatus} is:pr archived:false user:${params.owner} ${
params.searchText || ''
}`,
});

return {
next: data.items.length >= 100 ? (token || 1) + 1 : undefined,
page: data.items,
};
})
);

return prs as RestEndpointMethodTypes['search']['issuesAndPullRequests']['response']['data']['items'];
}

async function search({
q,
page,
}: {
q: string;
page: number;
}): Promise<RestEndpointMethodTypes['search']['issuesAndPullRequests']['response']['data']> {
// Because HttpError: Only the first 1000 search results are available
if (page >= 11) {
return {incomplete_results: false, items: [], total_count: 0};
}

const gh = getClient();

const {data} = await gh.search.issuesAndPullRequests({
per_page: 100,
page,
q,
});

return data;
}

0 comments on commit f0f167c

Please sign in to comment.