Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
/ pr-police Public archive

Commit

Permalink
feat: first version of the slackbot
Browse files Browse the repository at this point in the history
This commit adds the basic features:

- Connect to slack
- Check periodically for PRs
- Post a message to all specified slack channels listing the PRs
  • Loading branch information
rogeriopvl committed Apr 27, 2017
0 parents commit ce7b1a9
Show file tree
Hide file tree
Showing 6 changed files with 1,791 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: node_js
node_js:
- stable
- 6
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./lib/server')()
53 changes: 53 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Slackbot = require('slackbots')
const pullhub = require('pullhub')
const debounce = require('lodash.debounce')

module.exports = function server () {
const requiredEnvs = ['SLACK_TOKEN', 'GH_TOKEN', 'SLACK_CHANNELS', 'GH_REPOS']

if (!requiredEnvs.every((k) => !!process.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 bot = new Slackbot({
token: process.env.SLACK_TOKEN,
name: process.env.SLACK_BOT_NAME || 'PR-Police'
})

bot.on('start', () => {
checkForPRs()
debounce(checkForPRs, checkInterval)
})

function checkForPRs () {
pullhub(repos, labels)
.then((prs) => {
const message = buildMessage(prs)
return notify(message)
})
.catch((err) => {
console.error(err)
})
}

function buildMessage (data) {
const message = data.map((item) => {
return `#${item.number} - ${item.title} | ${item.html_url}`
})

return message.join('\n')
}

function notify (message) {
channels.map((channel) => {
bot.postMessageToChannel(channel, message)
})
}
}
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "pr-police",
"version": "1.0.0",
"description": "Slackbot that warns about open pull requests with given labels",
"main": "index.js",
"scripts": {
"pretest": "standard",
"test": "tape test/*.js | faucet",
"start": "node index.js"
},
"keywords": [
"slackbot",
"pull",
"request",
"github",
"labels"
],
"author": "rogeriopvl",
"license": "MIT",
"dependencies": {
"lodash.debounce": "^4.0.8",
"pullhub": "^1.0.1",
"slackbots": "^1.0.2"
},
"devDependencies": {
"faucet": "^0.0.1",
"proxyquire": "^1.7.11",
"sinon": "^2.1.0",
"standard": "^10.0.2",
"tape": "^4.6.3"
}
}
Loading

0 comments on commit ce7b1a9

Please sign in to comment.