From 9d25bbe9518aa49fe742404b926c0670377e2ff0 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Tue, 6 Feb 2018 13:47:21 +0100 Subject: [PATCH] Store Slack IDs instead of user names --- index.js | 10 +++++----- lib/retrieve-slack-github-users.js | 28 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index d094208..9437567 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ -let MemCache = require('mem-cache') -let SlackGitHubCacheBuilder = require('./lib/retrieve-slack-github-users') +const MemCache = require('mem-cache') +const SlackGitHubCacheBuilder = require('./lib/retrieve-slack-github-users') module.exports = async (robot) => { console.log('Yay, the app was loaded!') - let slackGitHubCache = new MemCache({ timeoutDisabled: true }) - let slackCachePromise = SlackGitHubCacheBuilder.build(robot, slackGitHubCache) + const slackGitHubCache = new MemCache({ timeoutDisabled: true }) + const slackCachePromise = SlackGitHubCacheBuilder.build(robot, slackGitHubCache) require('./bot_scripts/assign-new-pr-to-review')(robot) require('./bot_scripts/assign-approved-pr-to-test')(robot) @@ -13,7 +13,7 @@ module.exports = async (robot) => { require('./bot_scripts/greet-new-contributor')(robot) await slackCachePromise - robot.log.info('Slack username 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 diff --git a/lib/retrieve-slack-github-users.js b/lib/retrieve-slack-github-users.js index d13761d..75dede3 100644 --- a/lib/retrieve-slack-github-users.js +++ b/lib/retrieve-slack-github-users.js @@ -15,16 +15,16 @@ module.exports.build = async (robot, cache) => { await populateCache(robot, web, cache) } -module.exports.getGitHubIdFromSlackUsername = (slackUsername, cache) => { - return cache.get(getSlackCacheKeyName(slackUsername)) +module.exports.getGitHubIdFromSlackId = (slackUserId, cache) => { + return cache.get(getSlackId2GitHubIdCacheKeyName(slackUserId)) } -module.exports.getSlackUsernameFromGitHubId = (gitHubId, cache) => { - return cache.get(getGitHubCacheKeyName(gitHubId)) +module.exports.getSlackIdFromGitHubId = (gitHubId, cache) => { + return cache.get(getGitHub2SlackIdCacheKeyName(gitHubId)) } async function populateCache (robot, web, cache) { - robot.log.info('Populating Slack username cache...') + robot.log.info('Populating Slack user ID cache...') try { const usersList = await web.users.list() // TODO: This call should be paginated to avoid hitting limits (memory, API): https://api.slack.com/docs/pagination#cursors @@ -50,17 +50,17 @@ async function populateCache (robot, web, cache) { } if (!gitHubFieldId) { - robot.log.warn(`No GitHub ID field found in profile (@${username})!`) + robot.log.warn(`No GitHub ID field found in @${username} (${user.id}) profile!`) ++i continue } if (profile.fields && profile.fields[gitHubFieldId]) { const gitHubUsername = profile.fields[gitHubFieldId].value - robot.log.debug(`@${username} -> ${gitHubUsername}`) + robot.log.debug(`@${username} (${user.id}) -> ${gitHubUsername}`) - cache.set(getSlackCacheKeyName(username), gitHubUsername) - cache.set(getGitHubCacheKeyName(gitHubUsername), username) + cache.set(getSlackId2GitHubIdCacheKeyName(user.id), gitHubUsername) + cache.set(getGitHub2SlackIdCacheKeyName(gitHubUsername), user.id) usersContainingGitHubInfo = usersContainingGitHubInfo.concat(username) } else { robot.log.warn(`@${username} (${user.id}) has no GitHub ID set`) @@ -85,12 +85,12 @@ async function populateCache (robot, web, cache) { throw e } } - robot.log.info(`Populated Slack username cache with ${usersContainingGitHubInfo.length} users: ${usersContainingGitHubInfo.map(s => '@' + s).join(', ')}`) + robot.log.info(`Populated Slack user ID cache with ${usersContainingGitHubInfo.length} users: ${usersContainingGitHubInfo.map(s => '@' + s).join(', ')}`) if (usersMissingGitHubInfo) { robot.log.warn(`The following ${usersMissingGitHubInfo.length} Slack users have no GitHub info in their profiles: ${usersMissingGitHubInfo.map(s => '@' + s).join(', ')}`) } } catch (e) { - robot.log.error(`Error while populating Slack username cache: ${e}`) + robot.log.error(`Error while populating Slack user ID cache: ${e}`) } } @@ -107,11 +107,11 @@ function findGitHubLabelId (profile) { return null } -function getSlackCacheKeyName (slackUsername) { - return `Slack-${slackUsername}` +function getSlackId2GitHubIdCacheKeyName (slackUserId) { + return `Slack-${slackUserId}` } -function getGitHubCacheKeyName (gitHubUsername) { +function getGitHub2SlackIdCacheKeyName (gitHubUsername) { return `GitHub-${gitHubUsername}` }