status-github-bot/bot_scripts/notify-reviewers-via-slack.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

// 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'
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
await notifyReviewer(context, robot, getSlackIdFromGitHubId)
})
}
async function notifyReviewer (context, robot, getSlackIdFromGitHubId) {
const { payload } = context
const reviewer = payload.requested_reviewer
const userID = getSlackIdFromGitHubId(reviewer.login)
if (!userID) {
robot.log.warn('Could not find Slack ID for GitHub user', reviewer.login)
return
}
try {
const resp = await robot.slackWeb.im.open(userID)
const dmChannelID = resp.channel.id
const msg = `New Pull Request awaiting your review: ${payload.pull_request.html_url}`
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
robot.slackWeb.chat.postMessage(dmChannelID, msg, {unfurl_links: true, as_user: slackHelper.BotUserName})
} catch (error) {
robot.log.error('Could not open DM channel for review request notification', error)
}
}