mirror of
https://github.com/status-im/status-github-bot.git
synced 2025-03-03 22:30:38 +00:00
greet-new-contributor: Fix Slack users not getting notified
This commit is contained in:
parent
9b83f591ce
commit
2c1d67e829
@ -79,8 +79,8 @@ async function greetNewContributor (context, robot) {
|
||||
|
||||
const slackRecipients = welcomeBotConfig['slack-recipients']
|
||||
if (slackRecipients) {
|
||||
for (const userID of slackRecipients) {
|
||||
await notifySlackRecipient(robot, userID, payload, repoInfo)
|
||||
for (const slackUsername of slackRecipients) {
|
||||
await notifySlackRecipient(robot, slackUsername, payload, repoInfo)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@ -96,18 +96,20 @@ async function greetNewContributor (context, robot) {
|
||||
}
|
||||
}
|
||||
|
||||
async function notifySlackRecipient (robot, userID, payload, repoInfo) {
|
||||
async function notifySlackRecipient (robot, slackUsername, payload, repoInfo) {
|
||||
try {
|
||||
const slackProfileCache = robot['slackProfileCache']
|
||||
const userID = await slackProfileCache.getSlackIdFromSlackUsername(slackUsername)
|
||||
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.log.info(`Notifying ${slackUsername} 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)
|
||||
robot.log.warn(`Could not open DM channel to ${slackUsername} for new user's first PM notification`, error)
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,9 @@ const { WebClient } = require('@slack/client')
|
||||
|
||||
const token = process.env.SLACK_USER_TOKEN || ''
|
||||
const cacheMemcachedKey = 'slack-profile-cache-json'
|
||||
const slackIdCacheKeyPrefix = 'Slack-'
|
||||
const slackUsernameCacheKeyPrefix = 'SlackUN-'
|
||||
const gitHubIdCacheKeyPrefix = 'GitHub-'
|
||||
var allowLoadFromCache = true
|
||||
|
||||
module.exports = (robot) => new GitHubSlackIdMapper(robot)
|
||||
@ -37,6 +40,12 @@ class GitHubSlackIdMapper {
|
||||
return null
|
||||
}
|
||||
|
||||
async getSlackIdFromSlackUsername (slackUsername) {
|
||||
await this.buildPromise
|
||||
const id = this.cache.get(getSlackUsername2IdCacheKeyName(slackUsername))
|
||||
return id
|
||||
}
|
||||
|
||||
async getGitHubHandleFromSlackId (slackUserId) {
|
||||
await this.buildPromise
|
||||
const profile = this.cache.get(getSlackId2ProfileCacheKeyName(slackUserId))
|
||||
@ -78,11 +87,15 @@ async function internalBuild (robot, cache) {
|
||||
if (json.value) {
|
||||
const cacheFromFile = JSON.parse(json.value)
|
||||
for (const kvp of cacheFromFile) {
|
||||
if (kvp.k.startsWith('Slack-') && !kvp.v.hasOwnProperty('pubkey')) {
|
||||
if (kvp.k.startsWith(slackIdCacheKeyPrefix) && !kvp.v.hasOwnProperty('pubkey')) {
|
||||
cache.clean()
|
||||
break
|
||||
}
|
||||
cache.set(kvp.k, kvp.v)
|
||||
if (kvp.k.startsWith(slackIdCacheKeyPrefix)) {
|
||||
const profile = kvp.v
|
||||
cache.set(getSlackUsername2IdCacheKeyName(profile.name), kvp.k.substring(slackIdCacheKeyPrefix.length))
|
||||
}
|
||||
}
|
||||
allowLoadFromCache = false
|
||||
if (cache.length > 0) {
|
||||
@ -155,6 +168,7 @@ async function internalBuild (robot, cache) {
|
||||
robot.log.debug(`@${username} (${user.id}) -> ${JSON.stringify(data)}`)
|
||||
|
||||
cache.set(getSlackId2ProfileCacheKeyName(user.id), data)
|
||||
cache.set(getSlackUsername2IdCacheKeyName(username), user.id)
|
||||
if (gitHubUsername) {
|
||||
cache.set(getGitHub2SlackIdCacheKeyName(gitHubUsername), user.id)
|
||||
}
|
||||
@ -219,11 +233,15 @@ function findProfileLabelId (profile, labelName) {
|
||||
}
|
||||
|
||||
function getSlackId2ProfileCacheKeyName (slackUserId) {
|
||||
return `Slack-${slackUserId}`
|
||||
return slackIdCacheKeyPrefix.concat(slackUserId)
|
||||
}
|
||||
|
||||
function getSlackUsername2IdCacheKeyName (slackUsername) {
|
||||
return slackUsernameCacheKeyPrefix.concat(slackUsername)
|
||||
}
|
||||
|
||||
function getGitHub2SlackIdCacheKeyName (gitHubUsername) {
|
||||
return `GitHub-${gitHubUsername}`
|
||||
return gitHubIdCacheKeyPrefix.concat(gitHubUsername)
|
||||
}
|
||||
|
||||
function timeout (ms) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user