Last active
May 23, 2017 07:52
-
-
Save avnersorek/e5c1da093ae04e1e6df4 to your computer and use it in GitHub Desktop.
NodeJS script used to check GitHub for open pull requests - for several / multiple repositories - and send a message to Slack. We have this running every 30 minutes so the bot nags us about pull requests we are not attending to.
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
"use strict"; | |
var request = require('superagent'); | |
var Q = require('q'); | |
var config = { | |
github : { | |
// https://help.github.com/articles/creating-an-access-token-for-command-line-use/ | |
user : GITHUB_USERNAME, | |
password : GITHUB_TOKEN | |
}, | |
slack : { | |
// https://api.slack.com/incoming-webhooks | |
webhookUrl : SLACK_INCOMING_HOOK_URL | |
}, | |
reposToCheck : [ | |
{ "owner" : OWNER_NAME1, "repo" : REPO_NAME1 }, | |
{ "owner" : OWNER_NAME2, "repo" : REPO_NAME2 } | |
] | |
}; | |
console.log('Checking for pending pull requests...'); | |
var promises = config.reposToCheck.map(function(repoToCheck) { | |
var deferred = Q.defer(); | |
var url = "https://api.github.com/repos/" + | |
repoToCheck.owner + "/" + | |
repoToCheck.repo + "/pulls"; | |
request | |
.get(url) | |
.auth(config.github.user, config.github.password) | |
.end(function(err, res){ | |
if (err) return deferred.reject(err); | |
var prs = res.body.map(function(pr) { | |
return { | |
link : pr.html_url, | |
title : pr.title, | |
user : pr.user.login, | |
repo : repoToCheck.repo | |
}; | |
}); | |
deferred.resolve(prs); | |
}); | |
return deferred.promise; | |
}); | |
Q.all(promises).then(function(results) { | |
var allPrs = []; | |
results.forEach(function(repoResult) { allPrs = allPrs.concat(repoResult) }); | |
if (allPrs.length > 0) { | |
var text = "These pull requests are still pending : \n"; | |
allPrs.forEach(function(pr){ | |
text+= "<" + pr.link + "|" + pr.title + "> by " + pr.user + " in " + pr.repo + "\n"; | |
}); | |
var slackPostData = { | |
"username" : "pending-pull-requests-bot", | |
"text" : text | |
}; | |
request | |
.post(config.slack.webhookUrl) | |
.send(slackPostData) | |
.end(); | |
} | |
}) | |
.fail(console.log) | |
.done(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment