2018-02-12 18:43:12 +00:00
|
|
|
// Description:
|
|
|
|
// Script that listens for new Review Requests and notifies reviewers on Slack
|
|
|
|
//
|
|
|
|
// Dependencies:
|
|
|
|
// @slack/client: ""
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Martin Klepsch (martinklepsch)
|
|
|
|
|
2018-02-13 16:06:55 +00:00
|
|
|
const slackHelper = require('../lib/slack')
|
|
|
|
|
2018-02-13 15:42:00 +00:00
|
|
|
const botName = 'notify-reviewers-via-slack'
|
2018-02-12 18:43:12 +00:00
|
|
|
|
|
|
|
module.exports = (robot, getSlackIdFromGitHubId) => {
|
|
|
|
robot.log(`${botName} - Starting...`)
|
|
|
|
registerForNewReviewRequests(robot, getSlackIdFromGitHubId)
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerForNewReviewRequests (robot, getSlackIdFromGitHubId) {
|
|
|
|
robot.on('pull_request.review_requested', async context => {
|
|
|
|
// Make sure we don't listen to our own messages
|
|
|
|
if (context.isBot) return null
|
|
|
|
|
2018-02-13 18:22:29 +00:00
|
|
|
await notifyReviewer(context, robot, getSlackIdFromGitHubId)
|
2018-02-12 18:43:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:22:29 +00:00
|
|
|
async function notifyReviewer (context, robot, getSlackIdFromGitHubId) {
|
2018-02-12 18:43:12 +00:00
|
|
|
const { payload } = context
|
2018-02-13 18:22:29 +00:00
|
|
|
const reviewer = payload.requested_reviewer
|
|
|
|
const userID = getSlackIdFromGitHubId(reviewer.login)
|
2018-02-12 18:43:12 +00:00
|
|
|
|
2018-02-13 18:22:29 +00:00
|
|
|
if (!userID) {
|
|
|
|
robot.log.warn('Could not find Slack ID for GitHub user', reviewer.login)
|
|
|
|
return
|
|
|
|
}
|
2018-02-12 18:43:12 +00:00
|
|
|
|
2018-02-14 11:39:00 +00:00
|
|
|
try {
|
|
|
|
const resp = await robot.slackWeb.im.open(userID)
|
|
|
|
|
2018-02-13 18:22:29 +00:00
|
|
|
const dmChannelID = resp.channel.id
|
|
|
|
const msg = `New Pull Request awaiting your review: ${payload.pull_request.html_url}`
|
2018-02-12 18:43:12 +00:00
|
|
|
|
2018-02-13 18:22:29 +00:00
|
|
|
robot.log.info(`${botName} - Opened DM Channel ${dmChannelID}`)
|
|
|
|
robot.log.info(`Notifying ${userID} about review request in ${payload.pull_request.url}`)
|
2018-02-13 16:06:55 +00:00
|
|
|
|
2018-02-14 11:37:49 +00:00
|
|
|
robot.slackWeb.chat.postMessage(dmChannelID, msg, {unfurl_links: true, as_user: slackHelper.BotUserName})
|
2018-02-14 11:39:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
robot.log.error('Could not open DM channel for review request notification', error)
|
|
|
|
}
|
2018-02-12 18:43:12 +00:00
|
|
|
}
|