From 9c6038bfb4610eb06532ad12da3e0d61fceaee96 Mon Sep 17 00:00:00 2001 From: Jordi Montes Date: Mon, 22 Jan 2018 16:49:04 +0100 Subject: [PATCH 1/2] Github API calls --- bot/github.js | 37 +++++++++++++++++++++++++++++++++++++ config/default.js | 12 ++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 bot/github.js diff --git a/bot/github.js b/bot/github.js new file mode 100644 index 0000000..2a74382 --- /dev/null +++ b/bot/github.js @@ -0,0 +1,37 @@ +'use strict' + +const https = require('https'); +const config = require('../config'); + +// Returns the url for getting the labels of a request (Github v3) +// req has req.issue.labels_url +const getLabelsURL = function(req) { + let url = req.issue.labels_url; + // Make the URL generic removing the name of the label + return url.replace('{/name}', ''); +} + +const getLabels = function(req) { + let url = getLabelsURL(req); + return new Promise((resolve, reject) => { + const request = https.get(url, (response) => { + // handle http errors + if (response.statusCode < 200 || response.statusCode > 299) { + reject(new Error('Failed to load page, status code: ' + response.statusCode)); + } + // temporary data holder + const body = []; + // on every content chunk, push it to the data array + response.on('data', (chunk) => body.push(chunk)); + // we are done, resolve promise with those joined chunks + response.on('end', () => { + let labels = JSON.parse(body.join('')).map(lableObj => lableObj.name); + let bountyLabels = labels.filter(name => config.BOUNTY_LABELS.hasOwnProperty(name)); + + resolve(bountyLabels); + }); + }); + // handle connection errors of the request + request.on('error', (err) => reject(err)) + }); +} diff --git a/config/default.js b/config/default.js index b34d64e..53e55ae 100644 --- a/config/default.js +++ b/config/default.js @@ -1,3 +1,15 @@ + + +const BOUNTY_LABELS = { + 'bounty-xs': 1, + 'bounty-s': 10, + 'bounty-m': 100, + 'bounty-l': 1000, + 'bounty-xl': 10000 +} + + + module.exports = { urlEndpoint: "/autobounty/fund", signerPath: "https://ropsten.infura.io", From 9b84e2bd7fb849d560610645b8c75492ba4187e1 Mon Sep 17 00:00:00 2001 From: Jordi Montes Date: Mon, 22 Jan 2018 16:49:41 +0100 Subject: [PATCH 2/2] WIP: getAmount functionality --- bot/index.js | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/bot/index.js b/bot/index.js index 203c0f1..0935fea 100644 --- a/bot/index.js +++ b/bot/index.js @@ -21,15 +21,6 @@ const logger = winston.createLogger({ ] }); -const bountyLabels = { - 'bounty-xs': 1, - 'bounty-s': 10, - 'bounty-m': 100, - 'bounty-l': 1000, - 'bounty-xl': 10000, - 'bounty-xx': 100000 -}; - const needsFunding = function(req) { if (req.body.action !== 'created' || !req.body.hasOwnProperty('comment')) return false @@ -45,15 +36,16 @@ const getAddress = function(req) { } const getLabel = function(req) { - let labelNames = req.body.issue.labels.map(lableObj => lableObj.name); - - labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name)); - - if (labels.length == 1) - return labels[0]; - - //log error - return 0; + return github.getLabels(req) + .then(labels => { + if (labels.length === 1) { + resolve(labels[0]); + } else { + // TODO: Handle error + } + }).catch(err => { + // TODO: Handle error + }); } const getAmount = function(req) { @@ -69,15 +61,6 @@ const getAmount = function(req) { } -const getGasPrice = function(req) { - let gasPricePromise = prices.getGasPrice(); - - gasPricePromise - .then((gasPrice) => {return gasPrice}) - .catch((err) => {console.log("TODO-ERROR: Failed gas price request throw log error")}); - // Check how to handle errors when promises does not arrive -} - const log = function(msg) { logger.info(msg); } @@ -87,6 +70,6 @@ module.exports = { needsFunding: needsFunding, getAddress: getAddress, getAmount: getAmount, - getGasPrice: getGasPrice, + getGasPrice: prices.getGasPrice, log: log }