Skip to content

Commit

Permalink
fix: Continue support for setting a Redis string key prefix via REDIS…
Browse files Browse the repository at this point in the history
…_URL like redis.sock?key-prefix and localhost:6379/key-prefix
  • Loading branch information
joeyguerra committed Jul 23, 2023
1 parent 0ac4ab6 commit c1719ba
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/redis-brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ module.exports = function (robot, redis = Redis) {
robot.logger.info('Turning off redis ready checks')
}

const info = new URL(redisUrl)
let info = null
let prefix = ''
try {
info = new URL(redisUrl)
prefix = (info.pathname ? info.pathname.replace('/', '') : undefined) || 'hubot'
} catch (err) {
if (err.code === 'ERR_INVALID_URL') {
const urlPath = redisUrl.replace(/rediss?:\/{2}\:?(.*@)?/, '')
info = new URL(`redis://${urlPath}`)
prefix = info.search?.replace('?', '') || 'hubot'
}
}

let redisOptions = {
url: redisUrl
}
Expand All @@ -53,8 +65,6 @@ module.exports = function (robot, redis = Redis) {
redisOptions = Object.assign(redisOptions || {}, { socket: redisSocket })
}

const prefix = (info.pathname ? info.pathname.replace('/', '') : undefined) || 'hubot'

const client = redis.createClient(redisOptions)

robot.brain.setAutoSave(false)
Expand Down
56 changes: 51 additions & 5 deletions test/redis-brain-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const shell = require('hubot/src/adapters/shell')
const Adapter = require('hubot/src/adapter')
const redisBrain = require('../src/redis-brain.js')
const EventEmitter = require('events')
const path = require('path')

const chai = require('chai')
const sinon = require('sinon')
Expand All @@ -18,25 +17,28 @@ const Robot = Hubot.Robot
chai.use(require('sinon-chai'))

class RedisMock extends EventEmitter {
constructor () {
constructor (delegate) {
super()
this.data = {}
this.delegate = delegate
}

async connect () {
this.emit('connect')
}

async get (key) {
if (this.delegate?.get) return this.delegate.get(key)
return this.data[key]
}

async set (key, value) {
if (this.delegate?.set) return this.delegate.set(key)
this.data[key] = value
}

quit () {

if (this.delegate?.quit) return this.delegate.quit()
}
}

Expand All @@ -52,11 +54,16 @@ describe('redis-brain', () => {
it('exports a function', () => {
expect(require('../index')).to.be.a('Function')
})

it('Hostname should never be empty', () => {
process.env.REDIS_URL = 'redis://'
const robot = new Robot(null, 'shell', false, 'hubot')
sinon.spy(robot.logger, 'info')
robot.loadFile(path.resolve('src/'), 'redis-brain.js')
redisBrain(robot, {
createClient: (options) => {
return new RedisMock()
}
})
robot.run()
expect(robot.logger.info).to.have.been.calledWith('hubot-redis-brain: Discovered redis from REDIS_URL environment variable: redis://')
robot.shutdown()
Expand Down Expand Up @@ -89,7 +96,6 @@ describe('redis-brain', () => {
process.env.REDIS_NO_CHECK = 'true'
redisBrain(robot, {
createClient: (options) => {
console.log(options.socket)
expect(options.url).to.equal(process.env.REDIS_URL)
expect(options.socket.tls).to.be.true
expect(options.no_ready_check).to.be.true
Expand All @@ -103,4 +109,44 @@ describe('redis-brain', () => {
delete process.env.REDIS_REJECT_UNAUTHORIZED
delete process.env.REDIS_NO_CHECK
})

it('Setting the prefix with redis://localhost:6379/prefix-for-redis-key', () => {
process.env.REDIS_URL = 'redis://localhost:6379/prefix-for-redis-key'
const robot = new Robot(null, 'shell', false, 'hubot')
const delegate = {
data: {},
async get (key) {
expect(key).to.equal('prefix-for-redis-key:storage')
robot.shutdown()
delete process.env.REDIS_URL
return this.data[key]
}
}
redisBrain(robot, {
createClient: (options) => {
return new RedisMock(delegate)
}
})
robot.run()
})

it('Setting the prefix in the query string redis://:password@/var/run/redis.sock?prefix-for-redis-key', () => {
process.env.REDIS_URL = 'redis://username:test@/var/run/redis.sock?prefix-for-redis-key'
const robot = new Robot(null, 'shell', false, 'hubot')
const delegate = {
data: {},
async get (key) {
expect(key).to.equal('prefix-for-redis-key:storage')
robot.shutdown()
delete process.env.REDIS_URL
return this.data[key]
}
}
redisBrain(robot, {
createClient: (options) => {
return new RedisMock(delegate)
}
})
robot.run()
})
})

0 comments on commit c1719ba

Please sign in to comment.