From 3d55c24225590cca84d02abbc1343cddd53c75ff Mon Sep 17 00:00:00 2001 From: rogeriopvl Date: Thu, 27 Apr 2017 19:53:01 +0100 Subject: [PATCH] feat: add support for slack private groups the package slackbots distinguishes channels from private groups in its API, so this commit refactors the code to support both channels and groups and use the correct api method for each. --- app.json | 4 ++++ lib/server.js | 22 ++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app.json b/app.json index 208186c..383d056 100644 --- a/app.json +++ b/app.json @@ -25,6 +25,10 @@ "description": "Slack channels to post the announcements, comma separated", "value": "" }, + "SLACK_GROUPS": { + "description": "Slack private groups to post the announcements, comma separated", + "value": "" + }, "CHECK_INTERVAL": { "description": "Time interval to check for new pull requests, in milliseconds", "value": "3600000" diff --git a/lib/server.js b/lib/server.js index b1a2de2..7110b49 100644 --- a/lib/server.js +++ b/lib/server.js @@ -3,22 +3,24 @@ const pullhub = require('pullhub') const debounce = require('lodash.debounce') module.exports = function server () { - const requiredEnvs = ['SLACK_TOKEN', 'GH_TOKEN', 'SLACK_CHANNELS', 'GH_REPOS'] + const env = process.env + const requiredEnvs = ['SLACK_TOKEN', 'GH_TOKEN', 'GH_REPOS'] - if (!requiredEnvs.every((k) => !!process.env[k])) { + if (!requiredEnvs.every((k) => !!env[k])) { throw ( new Error('Missing one of this required ENV vars: ' + requiredEnvs.join(',')) ) } - const channels = process.env.SLACK_CHANNELS.split(',') || [] - const repos = process.env.GH_REPOS.split(',') || [] - const labels = process.env.GH_LABELS - const checkInterval = process.env.CHECK_INTERVAL || 3600000 // 1 hour default + const channels = env.SLACK_CHANNELS ? env.SLACK_CHANNELS.split(',') : [] + const groups = env.SLACK_GROUPS ? env.SLACK_GROUPS.split(',') : [] + const repos = env.GH_REPOS ? env.GH_REPOS.split(',') : [] + const labels = env.GH_LABELS + const checkInterval = env.CHECK_INTERVAL || 3600000 // 1 hour default const bot = new Slackbot({ - token: process.env.SLACK_TOKEN, - name: process.env.SLACK_BOT_NAME || 'PR-Police' + token: env.SLACK_TOKEN, + name: env.SLACK_BOT_NAME || 'PR-Police' }) bot.on('start', () => { @@ -49,5 +51,9 @@ module.exports = function server () { channels.map((channel) => { bot.postMessageToChannel(channel, message) }) + + groups.map((group) => { + bot.postMessageToGroup(group, message) + }) } }