From ad3ebecb6271037283b74686adbb49767f020988 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Thu, 28 Feb 2019 17:15:15 -0700 Subject: [PATCH] feat: make comment page count configurable and fetch twice as many pages as before --- src/lib/get_comments.mjs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/lib/get_comments.mjs b/src/lib/get_comments.mjs index c08ec01..3e1441f 100644 --- a/src/lib/get_comments.mjs +++ b/src/lib/get_comments.mjs @@ -5,20 +5,30 @@ function truncate(str, len = 26) { return `${str.slice(0, len)}...`; } -export default async function getEvents(repo, { body, actor } = {}) { - const events = await repo.events.fetch(); +const getCommentEvents = async (repo, lastEvents) => { + // just fetch and return the results + if (lastEvents == null) return repo.events.fetch(); - const items = await (async () => { - // process 2 pages of events - if (events.nextPage) { - const moreEvents = await events.nextPage.fetch(); - return [...events.items, ...moreEvents.items]; - } + // if there is no next page, just return the passed in object + if (!lastEvents.nextPage) return lastEvents; - return events.items; - })(); + // fetch next items and append previous ones + const newEvents = await lastEvents.nextPage.fetch(); + newEvents.items = [...lastEvents.items, ...newEvents.items]; + return newEvents; +}; - return items +export default async function getEvents(repo, { body, actor, pages = 4 } = {}) { + // create a task array that we can reduce into a promise chain + const chain = []; + while (chain.length < pages) chain.push('page'); + + const events = await chain.reduce( + acc => acc.then(res => getCommentEvents(repo, res)), + Promise.resolve() + ); + + return events.items .map(comment => { let skip = false; const isComment = comment.type === 'IssueCommentEvent';