Last active
December 17, 2016 22:38
-
-
Save gabmontes/a596399287259d86ccd5eec881be4567 to your computer and use it in GitHub Desktop.
Slack bot that listens for GitHub's PR URLs and finds out if the PR was merged or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const SlackBot = require('slackbots') | |
| const GitHubApi = require('github') | |
| const github = new GitHubApi({ | |
| version: '3.0.0', | |
| protocol: 'https' | |
| }) | |
| const bot = new SlackBot({ | |
| token: 'xoxb-000000000000-aaaaaaaaaaaaaaaaaaaaaaaa', | |
| name: '@github' | |
| }) | |
| bot.on('start', function () { | |
| console.log('Bot started') | |
| }) | |
| bot.on('message', function ({ type, text, channel }) { | |
| if (type !== 'message') { | |
| return | |
| } | |
| const regexp = /(<https:\/\/github.com\/([\w-_]+)\/([\w-_]+)\/pull\/(\d+)>)/gm | |
| let result | |
| while (result = regexp.exec(text)) { // eslint-disable-line no-cond-assign | |
| const [fullMatch, match, owner, repo, number] = result // eslint-disable-line no-unused-vars | |
| github.pullRequests.get({ owner, repo, number }).then(function ({ merged, mergeable }) { | |
| let message = `It looks like ${owner}/${repo}/pull/${number}` | |
| if (merged) { | |
| message += ' is already merged!' | |
| } else if (mergeable) { | |
| message += ' is ready to merge!' | |
| } else { | |
| message += ' cannot be to merged...' | |
| } | |
| bot.postMessage(channel, message).then(function () { | |
| console.log('PR data sent') | |
| }).catch(function (err) { | |
| console.error(`Could not post message: ${err.message}`) | |
| }) | |
| }).catch(function (err) { | |
| console.error(`Could not get data for PR: ${err.message}`) | |
| }) | |
| } | |
| }) | |
| bot.on('error', function (err) { | |
| console.error(`Bot error: ${err.message}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment