Extend Slack profile cache to support retrieving more data from profile

This commit is contained in:
Pedro Pombeiro 2018-03-28 18:02:12 +02:00
parent 2869c23aa2
commit 47256d7dcc
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
4 changed files with 25 additions and 19 deletions

View File

@ -61,7 +61,7 @@ async function notifyCollaborators (context, robot) {
// Mention the project board owner as well, if configured
const bountyProjectBoardOwner = bountyProjectBoardConfig['owner']
if (bountyProjectBoardOwner) {
const slackUserMention = robot.gitHubIdMapper.getSlackMentionFromGitHubId(bountyProjectBoardOwner)
const slackUserMention = robot.slackProfileCache.getSlackMentionFromGitHubId(bountyProjectBoardOwner)
if (slackUserMention) {
slackCollaborators.push(slackUserMention)
}
@ -102,7 +102,7 @@ async function getSlackCollaborators (repoInfo, github, robot, gitHubTeamConfig)
const gitHubUsers = teamMembers.map(u => u.login)
const slackUsers = new HashSet()
for (const gitHubUser of gitHubUsers) {
const id = await robot.gitHubIdMapper.getSlackMentionFromGitHubId(gitHubUser)
const id = await robot.slackProfileCache.getSlackMentionFromGitHubId(gitHubUser)
if (id) {
slackUsers.add(id)
}

View File

@ -28,7 +28,7 @@ function registerForNewReviewRequests (robot) {
async function notifyReviewer (context, robot) {
const { payload } = context
const reviewer = payload.requested_reviewer
const userID = await robot.gitHubIdMapper.getSlackIdFromGitHubId(reviewer.login)
const userID = await robot.slackProfileCache.getSlackIdFromGitHubId(reviewer.login)
if (!userID) {
robot.log.warn('Could not find Slack ID for GitHub user', reviewer.login)

View File

@ -13,7 +13,7 @@ module.exports = async (robot) => {
await setupSlack(robot)
robot['gitHubIdMapper'] = require('./lib/github-id-mapper')(robot)
robot['slackProfileCache'] = require('./lib/slack-profile-cache')(robot)
require('./bot_scripts/assign-new-pr-to-review')(robot)
require('./bot_scripts/assign-approved-pr-to-test')(robot)

View File

@ -25,9 +25,13 @@ class GitHubSlackIdMapper {
setInterval(() => internalBuild(this.robot, this.cache), 24 * 60 * 60 * 1000)
}
async getGitHubIdFromSlackId (slackUserId, cache) {
async getGitHubHandleFromSlackId (slackUserId) {
await this.buildPromise
return cache.get(getSlackId2GitHubIdCacheKeyName(slackUserId))
const profile = this.cache.get(getSlackId2ProfileCacheKeyName(slackUserId))
if (profile) {
return profile.github_handle
}
return null
}
async getSlackIdFromGitHubId (gitHubId) {
@ -68,27 +72,29 @@ async function internalBuild (robot, cache) {
if (!gitHubFieldId) {
// Find the field ID for the field with the 'Github ID' label
gitHubFieldId = findGitHubLabelId(profile)
gitHubFieldId = findProfileLabelId(profile, 'Github ID')
}
if (!gitHubFieldId) {
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} (${user.id}) -> ${gitHubUsername}`)
cache.set(getSlackId2GitHubIdCacheKeyName(user.id), gitHubUsername)
cache.set(getGitHub2SlackIdCacheKeyName(gitHubUsername), user.id)
const gitHubUsername = gitHubFieldId && profile.fields && profile.fields[gitHubFieldId] ? profile.fields[gitHubFieldId].value : null
if (gitHubUsername) {
usersContainingGitHubInfo = usersContainingGitHubInfo.concat(username)
} else {
robot.log.warn(`@${username} (${user.id}) has no GitHub ID set`)
usersMissingGitHubInfo = usersMissingGitHubInfo.concat(username)
}
const data = { github_handle: gitHubUsername }
robot.log.debug(`@${username} (${user.id}) -> ${JSON.stringify(data)}`)
cache.set(getSlackId2ProfileCacheKeyName(user.id), data)
if (gitHubUsername) {
cache.set(getGitHub2SlackIdCacheKeyName(gitHubUsername), user.id)
}
++i
await sleep(1500)
} catch (e) {
@ -116,11 +122,11 @@ async function internalBuild (robot, cache) {
}
}
function findGitHubLabelId (profile) {
function findProfileLabelId (profile, labelName) {
if (profile.fields) {
for (const fieldId in profile.fields) {
const field = profile.fields[fieldId]
if (field.label === 'Github ID') {
if (field.label === labelName) {
return fieldId
}
}
@ -129,7 +135,7 @@ function findGitHubLabelId (profile) {
return null
}
function getSlackId2GitHubIdCacheKeyName (slackUserId) {
function getSlackId2ProfileCacheKeyName (slackUserId) {
return `Slack-${slackUserId}`
}