Add support for caching the Slack user profiles locally
This commit is contained in:
parent
47256d7dcc
commit
000977a2c1
|
@ -9,9 +9,12 @@
|
|||
// PombeirP
|
||||
|
||||
const MemCache = require('mem-cache')
|
||||
const fs = require('fs')
|
||||
const { WebClient } = require('@slack/client')
|
||||
|
||||
const token = process.env.SLACK_USER_TOKEN || ''
|
||||
const cachePath = './slack-cache.json'
|
||||
var allowLoadFromCache = true
|
||||
|
||||
module.exports = (robot) => new GitHubSlackIdMapper(robot)
|
||||
|
||||
|
@ -49,6 +52,24 @@ class GitHubSlackIdMapper {
|
|||
}
|
||||
|
||||
async function internalBuild (robot, cache) {
|
||||
if (allowLoadFromCache) {
|
||||
try {
|
||||
const json = fs.readFileSync(cachePath)
|
||||
if (json) {
|
||||
const cacheFromFile = JSON.parse(json)
|
||||
for (const kvp of cacheFromFile) {
|
||||
cache.set(kvp.k, kvp.v)
|
||||
}
|
||||
robot.log.info(`Read Slack user cache from file (${cache.length} entries)`)
|
||||
allowLoadFromCache = false
|
||||
return
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore
|
||||
robot.log.info('Could not find Slack user cache')
|
||||
}
|
||||
}
|
||||
|
||||
robot.log.info('Populating Slack user ID cache...')
|
||||
|
||||
try {
|
||||
|
@ -117,6 +138,13 @@ async function internalBuild (robot, cache) {
|
|||
if (usersMissingGitHubInfo) {
|
||||
robot.log.warn(`The following ${usersMissingGitHubInfo.length} Slack users have no GitHub info in their profiles: ${usersMissingGitHubInfo.map(s => '@' + s).join(', ')}`)
|
||||
}
|
||||
|
||||
// Write cache out to JSON file for faster startup next time
|
||||
const c = []
|
||||
for (const key of cache.keys) {
|
||||
c.push({ k: key, v: cache.get(key) })
|
||||
}
|
||||
fs.writeFileSync(cachePath, JSON.stringify(c, null, 2))
|
||||
} catch (e) {
|
||||
robot.log.error(`Error while populating Slack user ID cache: ${e}`)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue