Files
gh-action-bot/src/lib/get_comments.mjs
joe fleming ad3ebecb62 feat: make comment page count configurable
and fetch twice as many pages as before
2019-02-28 17:15:15 -07:00

68 lines
2.2 KiB
JavaScript

import logger from './logger.mjs';
function truncate(str, len = 26) {
if (str.length <= len) return str;
return `${str.slice(0, len)}...`;
}
const getCommentEvents = async (repo, lastEvents) => {
// just fetch and return the results
if (lastEvents == null) return repo.events.fetch();
// if there is no next page, just return the passed in object
if (!lastEvents.nextPage) return lastEvents;
// fetch next items and append previous ones
const newEvents = await lastEvents.nextPage.fetch();
newEvents.items = [...lastEvents.items, ...newEvents.items];
return newEvents;
};
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';
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 && `(${truncate(comment.payload.comment.body)})`
);
}
return false;
}
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);
}