|
| 1 | +const { isPrivateDiscordMessage } = require('./utils/channels.js') |
| 2 | +const { getBusinessDatesCount } = require('./utils/dates.js') |
| 3 | +const { prHelpers } = require('./utils/prs.js') |
| 4 | +const CronJob = require('cron').CronJob |
| 5 | +require('dotenv').config() |
| 6 | + |
| 7 | +const processPRs = async robot => { |
| 8 | + const message = await getMessage(robot); |
| 9 | + if (message === "") { |
| 10 | + console.log("No PRs to remind about") |
| 11 | + return; |
| 12 | + } |
| 13 | + await fetch('http://clique-service', { |
| 14 | + method: 'POST', |
| 15 | + headers: { |
| 16 | + 'Accept': 'application/json', |
| 17 | + 'Content-Type': 'application/json' |
| 18 | + }, |
| 19 | + body: JSON.stringify({message}) |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +const getMessage = async robot => { |
| 24 | + const threshold = 2 // number of days since interaction |
| 25 | + const github = require('githubot')(robot) |
| 26 | + const repo = github.qualified_repo("colonyCDapp") |
| 27 | + const BASE_URL = `https://api.github.com/repos/${repo}` |
| 28 | + |
| 29 | + |
| 30 | + const { getPRs, getReviews, getComments, getPREvents, getPRCommits, getTeamMembers } = prHelpers(robot) |
| 31 | + const teamMembers = await getTeamMembers('joincolony', 'pagepro') |
| 32 | + const pageproLogins = teamMembers.map(m => m.login).filter(l => ["rdig","arrenv"].indexOf(l) === -1); |
| 33 | + const prs = await getPRs(BASE_URL) |
| 34 | + |
| 35 | + const over = []; |
| 36 | + |
| 37 | + for (const pr of prs) { |
| 38 | + // console.log(pr); |
| 39 | + if (pr.draft) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + if (pr.labels.filter(label => label.name === 'on-hold').length > 0) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + // This is only intended to alert about PRs that have _NOT_ been made by Pagepro team members |
| 46 | + if (pageproLogins.includes(pr.user.login)) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + const reviews = await getReviews(pr); |
| 50 | + const comments = await getComments(pr); |
| 51 | + const events = await getPREvents(pr); |
| 52 | + const commits = await getPRCommits(pr); |
| 53 | + |
| 54 | + const labellingEvents = events.filter(event => event.event === 'labeled' || event.event === 'unlabeled'); |
| 55 | + |
| 56 | + const last_review_timestamp = reviews.length > 0 ? reviews[reviews.length - 1].submitted_at : pr.created_at; |
| 57 | + const last_comment_timestamp = comments.length > 0 ? comments[comments.length - 1].created_at : pr.created_at; |
| 58 | + const last_label_timestamp = labellingEvents.length > 0 ? labellingEvents[labellingEvents.length - 1].created_at : pr.created_at; |
| 59 | + const last_commit_author_timestamp = commits.length > 0 ? commits[commits.length - 1].commit.author.date : pr.created_at; |
| 60 | + const last_event_timestamp = Math.max(new Date(last_review_timestamp), new Date(last_comment_timestamp), new Date(last_label_timestamp), new Date(last_commit_author_timestamp)); |
| 61 | + |
| 62 | + const days = getBusinessDatesCount(new Date(last_event_timestamp), new Date()); |
| 63 | + if (days >= threshold) { |
| 64 | + over.push(pr); |
| 65 | + } |
| 66 | + } |
| 67 | + let response = ""; |
| 68 | + |
| 69 | + if (over.length > 0) { |
| 70 | + response += `**These internal PRs didn't have any action taken on them yesterday:**\n` |
| 71 | + over.forEach(pr => { |
| 72 | + response += `**PR #${pr.number}:** ${pr.title} <${pr['html_url']}>\n` |
| 73 | + }) |
| 74 | + } |
| 75 | + return response; |
| 76 | +} |
| 77 | + |
| 78 | +const setupCronJob = robot => { |
| 79 | + const job = new CronJob({ |
| 80 | + // Every weekday 08:30h London |
| 81 | + cronTime: '00 30 08 * * 1-5', |
| 82 | + onTick: () => { |
| 83 | + processPRs(robot) |
| 84 | + }, |
| 85 | + start: false, |
| 86 | + timeZone: 'Europe/London' |
| 87 | + }) |
| 88 | + job.start() |
| 89 | +} |
| 90 | +module.exports = function(robot) { |
| 91 | + setupCronJob(robot) |
| 92 | + |
| 93 | + robot.hear(/!notPageproReminder/, async res => { |
| 94 | + if (!isPrivateDiscordMessage(robot.client, res)) return |
| 95 | + res.send("Received !notPageproReminder command - please wait while I query the Github API"); |
| 96 | + const message = await getMessage(robot); |
| 97 | + res.send(message); |
| 98 | + }); |
| 99 | +} |
0 commit comments