Fix call to getSlackUsernameFromGitHubId

This commit is contained in:
Pedro Pombeiro 2018-02-06 12:17:58 +01:00
parent 8244dfae14
commit 75646a2005
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
2 changed files with 19 additions and 12 deletions

View File

@ -14,26 +14,26 @@ const Slack = require('probot-slack-status')
const defaultConfig = require('../lib/config') const defaultConfig = require('../lib/config')
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')
module.exports = (robot, getSlackUsernameFromGitHubId) => { module.exports = (robot, getSlackMentionFromGitHubId) => {
robot.log('Connected to bounty-awaiting-approval-slack-ping') robot.log('Connected to bounty-awaiting-approval-slack-ping')
Slack(robot, (slack) => { Slack(robot, (slack) => {
robot.log.trace('Connected to 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 => { robot.on('issues.labeled', async context => {
// Make sure we don't listen to our own messages // Make sure we don't listen to our own messages
if (context.isBot) return null 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 { github, payload } = context
const ownerName = payload.repository.owner.login const ownerName = payload.repository.owner.login
const repoName = payload.repository.name 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...`) 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 // Send message to Slack
slackHelper.sendMessage( slackHelper.sendMessage(
@ -59,13 +59,12 @@ async function notifyCollaborators (context, robot, slackClient, getSlackUsernam
) )
} }
// Get the Slack usernames of the collaborators of this repo. // Get the Slack IDs 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, getSlackMentionFromGitHubId) {
async function getSlackCollaborators (owner, repo, github, robot, getSlackUsernameFromGitHubId) {
// Grab a list of collaborators to this repo, as an array of GitHub login usernames // Grab a list of collaborators to this repo, as an array of GitHub login usernames
let collaborators = await github.repos.getCollaborators({owner, repo}) let collaborators = await github.repos.getCollaborators({owner, repo})
collaborators = collaborators.data.map(collaboratorObject => collaboratorObject.login) collaborators = collaborators.data.map(collaboratorObject => collaboratorObject.login)
// Create an array of Slack usernames from GitHub usernames // Create an array of Slack usernames from GitHub usernames
return collaborators.map(getSlackUsernameFromGitHubId).filter(u => u) return collaborators.map(getSlackMentionFromGitHubId).filter(id => id)
} }

View File

@ -1,10 +1,10 @@
const MemCache = require('mem-cache') const MemCache = require('mem-cache')
const slackGitHubCache = new MemCache({ timeoutDisabled: true })
const SlackGitHubCacheBuilder = require('./lib/retrieve-slack-github-users') const SlackGitHubCacheBuilder = require('./lib/retrieve-slack-github-users')
module.exports = async (robot) => { module.exports = async (robot) => {
console.log('Yay, the app was loaded!') console.log('Yay, the app was loaded!')
const slackGitHubCache = new MemCache({ timeoutDisabled: true })
const slackCachePromise = SlackGitHubCacheBuilder.build(robot, slackGitHubCache) const slackCachePromise = SlackGitHubCacheBuilder.build(robot, slackGitHubCache)
require('./bot_scripts/assign-new-pr-to-review')(robot) 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') robot.log.info('Slack user ID cache populated, loading remainder of scripts')
// Add scripts which require using the Slack/GitHub cache after this comment // 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: // For more information on building apps:
// https://probot.github.io/docs/ // https://probot.github.io/docs/
@ -24,3 +24,11 @@ module.exports = async (robot) => {
// To get your app running against GitHub, see: // To get your app running against GitHub, see:
// https://probot.github.io/docs/development/ // https://probot.github.io/docs/development/
} }
function getSlackMentionFromGitHubId (gitHubId) {
const id = SlackGitHubCacheBuilder.getSlackIdFromGitHubId(gitHubId, slackGitHubCache)
if (!id) {
return null
}
return `<@${id}>`
}