import logger from './logger.mjs'; export default async function getEvents(repo, { body, actor } = {}) { const events = await 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]; } return events.items; })(); return items .map(comment => { let skip = false; const isComment = comment.type === 'IssueCommentEvent'; const bodyMatch = isComment && (!body || body.test(comment.payload.comment.body)); const isActor = comment.actor.login === actor; // skip events that don't match the criteria if (!isComment) skip = true; if (comment.payload.action !== 'created') skip = true; if (!isActor) skip = true; if (isComment && !bodyMatch) skip = true; if (skip) { if (isActor) { logger.debug( `SKIP EVENT ${isComment ? `#${comment.payload.issue.number}` : comment.id}: ${ comment.type } ${comment.payload.action} by ${comment.actor.login}`, isComment && !bodyMatch && `(${comment.payload.comment.body.slice(0, 30)})` ); } return false; } logger.debug(`PROCESS #${comment.payload.issue.number}`); return { id: comment.id, number: comment.payload.issue.number, owner: comment.payload.issue.user.login, labels: comment.payload.issue.labels.map(label => label.name), comment_id: comment.payload.comment.id, comment_author: comment.payload.comment.user.login, comment_body: comment.payload.comment.body, }; }) .filter(Boolean); }