From 369d01bcb92876d17bc84065d9d545d4dc9678ba Mon Sep 17 00:00:00 2001 From: spycrab Date: Tue, 5 Jun 2018 00:59:55 +0200 Subject: [PATCH] Implement some GitHub related improvements (#28) --- env.json | 12 ++++++++++-- src/commands/status.js | 31 +++++++++++++++++++++++++++++++ src/triggers/github.js | 8 +++++--- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/commands/status.js diff --git a/env.json b/env.json index f1814db..73c79de 100644 --- a/env.json +++ b/env.json @@ -31,6 +31,14 @@ }, "DATA_CUSTOM_RESPONSES": { "required": false, - "description": "Whether or not to load responses.js from the data directory." + "description": "Whether or not to load responses.js from the data directory." + }, + "GITHUB_REPOSITORY": { + "required": false, + "description": "The github repository that should be tracked (format is user/repository)" + }, + "GITHUB_OLD_THRESHOLD": { + "required": false, + "description": "Issues below this treshold should be ignored" } - } +} diff --git a/src/commands/status.js b/src/commands/status.js new file mode 100644 index 0000000..95818fd --- /dev/null +++ b/src/commands/status.js @@ -0,0 +1,31 @@ +const request = require('request'); +const logger = require('../logging.js'); + +exports.roles = ['Admins', 'Moderators', 'Developers']; +exports.command = function (message) { + let pr = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, ''); + + let repo = process.env.GITHUB_REPOSITORY || "citra-emu/citra"; + let url = `https://api.github.com/repos/${repo}/pulls/${pr}`; + + request({ url: url, headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)'}}, function (error, response, body) { + if (!error) { + var pr = JSON.parse(body); + request({ url: pr.statuses_url, headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)'}}, function(error, response, body) + { + var statuses = JSON.parse(body); + + if (statuses.length == 0) return; + + // Travis CI will give you multiple, identical target URLs so we might as well just check the first one... + var status = statuses[0]; + status.target_url = status.target_url.substr(0, status.target_url.indexOf('?')); + message.channel.sendMessage(`${status.context}: ${status.target_url}: **${status.state}**`); + }); + } + else + { + message.channel.sendMessage('No such PR.'); + } + }); +}; diff --git a/src/triggers/github.js b/src/triggers/github.js index 80387d1..9db019c 100644 --- a/src/triggers/github.js +++ b/src/triggers/github.js @@ -10,6 +10,8 @@ exports.execute = function (message) { let matcher = new RegExp(regex); let match = matcher.exec(message.content); let matched = []; + let threshold = process.env.GITHUB_OLD_THRESHOLD || 2000; + let repo = process.env.GITHUB_REPOSITORY || "citra-emu/citra"; while (match != null) { if (matched.indexOf(match[1]) === -1) { @@ -23,12 +25,12 @@ exports.execute = function (message) { // This usually happens when someone messes up pinging another person or // in general conversation. // ex: You're #1! - if (match[1] <= 2000) { return; } - + if (match[1] < threshold) { return; } + // Map domain path to type let map = {'pull': 'Pull Request', 'issues': 'Issue'}; - let url = `https://github.com/citra-emu/citra/pull/${match[1]}`; + let url = `https://github.com/${repo}/pull/${match[1]}`; request(url, function (error, response, body) { if (!error && response.statusCode === 200) {