feat: make comment page count configurable
and fetch twice as many pages as before
This commit is contained in:
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user