add bot script to notify PR reviewers via slack; fix #4

This commit is contained in:
Martin Klepsch 2018-02-12 19:43:12 +01:00 committed by Pedro Pombeiro
parent 27a12c7c59
commit 515e5d4539
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,49 @@
// Description:
// Script that listens for new Review Requests and notifies reviewers on Slack
//
// Dependencies:
// @slack/client: ""
//
// Author:
// Martin Klepsch (martinklepsch)
const { WebClient } = require('@slack/client')
const slackWeb = new WebClient(process.env.SLACK_BOT_TOKEN)
const botName = 'notify-reviwers-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 notifyReviewers(context, robot, getSlackIdFromGitHubId)
})
}
async function notifyReviewers (context, robot, getSlackIdFromGitHubId) {
const { payload } = context
for (var reviewer of payload.pull_request.requested_reviewers) {
const userID = getSlackIdFromGitHubId(reviewer.login)
if (userID === undefined) {
robot.log.error('Could not find Slack ID for GitHub user', reviewer.login)
} else {
slackWeb.im.open(userID).then((resp) => {
const dmChannelID = resp.channel.id
const octoboxNote = 'For more powerful management of GitHub notifications also check out https://octobox.io/'
const msg = `New Pull Request awaiting review: ${payload.pull_request.html_url}\n${octoboxNote}`
robot.log.info(`${botName} - Opened DM Channel ${dmChannelID}`)
robot.log.info(`Notifying ${userID} about review request in ${payload.pull_request.url}`)
slackWeb.chat.postMessage(dmChannelID, msg, {unfurl_links: true, as_user: 'probot'})
}).catch(error => robot.log.error('Could not open DM channel for review request notification', error))
}
}
}

View File

@ -18,6 +18,7 @@ module.exports = async (robot) => {
// Add scripts which require using the Slack/GitHub cache after this comment
require('./bot_scripts/bounty-awaiting-approval-slack-ping')(robot, getSlackMentionFromGitHubId)
require('./bot_scripts/notify-reviewers-via-slack')(robot, getSlackIdFromGitHubId)
// For more information on building apps:
// https://probot.github.io/docs/
@ -33,3 +34,7 @@ function getSlackMentionFromGitHubId (gitHubId) {
}
return `<@${id}>`
}
function getSlackIdFromGitHubId (gitHubId) {
return SlackGitHubCacheBuilder.getSlackIdFromGitHubId(gitHubId, slackGitHubCache)
}

View File

@ -10,6 +10,8 @@
module.exports.sendMessage = async (robot, slackClient, room, message) => {
// Send message to Slack
if (slackClient != null) {
// TODO BOUNTY migrate away from datastore:
// https://github.com/slackapi/node-slack-sdk/wiki/DataStore-v3.x-Migration-Guide
const channel = slackClient.dataStore.getChannelByName(room)
try {
if (process.env.DRY_RUN) {