greet-new-contributor: Add support to notify Slack users

This commit is contained in:
Pedro Pombeiro 2018-04-10 18:50:17 +02:00
parent 51ff592e08
commit 7d8189b515
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
1 changed files with 23 additions and 0 deletions

View File

@ -76,6 +76,13 @@ async function greetNewContributor (context, robot) {
// Send message to Slack // Send message to Slack
slackHelper.sendMessage(robot, config.slack.notification.room, `Greeted ${payload.pull_request.user.login} on his first PR in the ${repoInfo.repo} repo\n${payload.pull_request.html_url}`) slackHelper.sendMessage(robot, config.slack.notification.room, `Greeted ${payload.pull_request.user.login} on his first PR in the ${repoInfo.repo} repo\n${payload.pull_request.html_url}`)
const slackRecipients = welcomeBotConfig['slack-recipients']
if (slackRecipients) {
for (const userID of slackRecipients) {
await notifySlackRecipient(robot, userID, payload, repoInfo)
}
}
} catch (err) { } catch (err) {
if (err.code !== 404) { if (err.code !== 404) {
robot.log.error(`${botName} - Couldn't create comment on PR: ${err}`, repoInfo) robot.log.error(`${botName} - Couldn't create comment on PR: ${err}`, repoInfo)
@ -88,3 +95,19 @@ async function greetNewContributor (context, robot) {
robot.log.error(`${botName} - Couldn't fetch the user's github issues for repo: ${err}`, repoInfo) robot.log.error(`${botName} - Couldn't fetch the user's github issues for repo: ${err}`, repoInfo)
} }
} }
async function notifySlackRecipient (robot, userID, payload, repoInfo) {
try {
const resp = await robot.slackWeb.im.open(userID)
const dmChannelID = resp.channel.id
const msg = `Greeted ${payload.pull_request.user.login} on his first PR in the ${repoInfo.repo} repo\n${payload.pull_request.html_url}`
robot.log.info(`${botName} - Opened DM Channel ${dmChannelID}`)
robot.log.info(`Notifying ${userID} about user's first PM in ${payload.pull_request.url}`)
robot.slackWeb.chat.postMessage(dmChannelID, msg, {unfurl_links: true, as_user: slackHelper.BotUserName})
} catch (error) {
robot.log.warn('Could not open DM channel for new user\'s first PM notification', error)
}
}