diff --git a/bot_scripts/bounty-awaiting-approval-slack-ping.js b/bot_scripts/bounty-awaiting-approval-slack-ping.js index 10da27e..2a2c686 100644 --- a/bot_scripts/bounty-awaiting-approval-slack-ping.js +++ b/bot_scripts/bounty-awaiting-approval-slack-ping.js @@ -14,26 +14,26 @@ const Slack = require('probot-slack-status') const defaultConfig = require('../lib/config') const slackHelper = require('../lib/slack') -module.exports = (robot, getSlackUsernameFromGitHubId) => { +module.exports = (robot, getSlackMentionFromGitHubId) => { robot.log('Connected to bounty-awaiting-approval-slack-ping') Slack(robot, (slack) => { robot.log.trace('Connected to Slack') - registerForNewBounties(robot, slack, getSlackUsernameFromGitHubId) + registerForNewBounties(robot, slack, getSlackMentionFromGitHubId) }) } -function registerForNewBounties (robot, slackClient, getSlackUsernameFromGitHubId) { +function registerForNewBounties (robot, slackClient, getSlackMentionFromGitHubId) { robot.on('issues.labeled', async context => { // Make sure we don't listen to our own messages if (context.isBot) return null - await notifyCollaborators(context, robot, slackClient, getSlackUsernameFromGitHubId) + await notifyCollaborators(context, robot, slackClient, getSlackMentionFromGitHubId) }) } -async function notifyCollaborators (context, robot, slackClient, getSlackUsernameFromGitHubId) { +async function notifyCollaborators (context, robot, slackClient, getSlackMentionFromGitHubId) { const { github, payload } = context const ownerName = payload.repository.owner.login const repoName = payload.repository.name @@ -47,7 +47,7 @@ async function notifyCollaborators (context, robot, slackClient, getSlackUsernam robot.log(`bountyAwaitingApprovalSlackPing - issue #${payload.issue.number} on ${ownerName}/${repoName} was labeled as a bounty awaiting approval. Pinging slack...`) - const slackCollaborators = await getSlackCollaborators(ownerName, repoName, github, robot, getSlackUsernameFromGitHubId) + const slackCollaborators = await getSlackCollaborators(ownerName, repoName, github, robot, getSlackMentionFromGitHubId) // Send message to Slack slackHelper.sendMessage( @@ -59,13 +59,12 @@ async function notifyCollaborators (context, robot, slackClient, getSlackUsernam ) } -// Get the Slack usernames of the collaborators of this repo. -// Collaborators should add a mapping of their GitHub to Slack usernames in .github/collaborators.yml -async function getSlackCollaborators (owner, repo, github, robot, getSlackUsernameFromGitHubId) { +// Get the Slack IDs of the collaborators of this repo. +async function getSlackCollaborators (owner, repo, github, robot, getSlackMentionFromGitHubId) { // Grab a list of collaborators to this repo, as an array of GitHub login usernames let collaborators = await github.repos.getCollaborators({owner, repo}) collaborators = collaborators.data.map(collaboratorObject => collaboratorObject.login) // Create an array of Slack usernames from GitHub usernames - return collaborators.map(getSlackUsernameFromGitHubId).filter(u => u) + return collaborators.map(getSlackMentionFromGitHubId).filter(id => id) } diff --git a/index.js b/index.js index 743e9a7..f56b414 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ const MemCache = require('mem-cache') +const slackGitHubCache = new MemCache({ timeoutDisabled: true }) const SlackGitHubCacheBuilder = require('./lib/retrieve-slack-github-users') module.exports = async (robot) => { console.log('Yay, the app was loaded!') - const slackGitHubCache = new MemCache({ timeoutDisabled: true }) const slackCachePromise = SlackGitHubCacheBuilder.build(robot, slackGitHubCache) require('./bot_scripts/assign-new-pr-to-review')(robot) @@ -16,7 +16,7 @@ module.exports = async (robot) => { robot.log.info('Slack user ID cache populated, loading remainder of scripts') // Add scripts which require using the Slack/GitHub cache after this comment - require('./bot_scripts/bounty-awaiting-approval-slack-ping')(robot, SlackGitHubCacheBuilder.getSlackUsernameFromGitHubId) + require('./bot_scripts/bounty-awaiting-approval-slack-ping')(robot, getSlackMentionFromGitHubId) // For more information on building apps: // https://probot.github.io/docs/ @@ -24,3 +24,11 @@ module.exports = async (robot) => { // To get your app running against GitHub, see: // https://probot.github.io/docs/development/ } + +function getSlackMentionFromGitHubId (gitHubId) { + const id = SlackGitHubCacheBuilder.getSlackIdFromGitHubId(gitHubId, slackGitHubCache) + if (!id) { + return null + } + return `<@${id}>` +}