diff --git a/bot/github.js b/bot/github.js index 0d5ccc7..2be9107 100644 --- a/bot/github.js +++ b/bot/github.js @@ -8,6 +8,8 @@ const config = require('../config'); const getLabelsURL = function (req) { let url = req.body.issue.labels_url; // Make the URL generic removing the name of the label + console.log(url); + console.log('clean url' + url.replace('{name}', '')); return url.replace('{/name}', ''); } @@ -18,15 +20,19 @@ const getLabelsMock = function (req) { // Returns all the bounty labelNames of a given issue (Github API v3) const getLabels = function (req) { + console.log('starting getLabels from github...'); if (config.debug) { return getLabelsMock(req); } else { - let path = getLabelsURL(req).remplace('api.github.com', ''); + console.log('getting path for asking labels...'); + let path = getLabelsURL(req).replace('https://api.github.com', ''); + console.log('path: ' + path); const options = { hostname: 'api.github.com', path: path, headers: { 'User-Agent': 'kafkasl' } }; + console.log('Github url: ' + options.hostname+path) return new Promise((resolve, reject) => { const request = https.get(options, (response) => { // handle http errors diff --git a/bot/index.js b/bot/index.js index aba7b44..9a0822b 100644 --- a/bot/index.js +++ b/bot/index.js @@ -10,6 +10,7 @@ const logger = winston.createLogger({ transports: [ new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }), new winston.transports.File({ filename: config.logPath + 'info.log', level: 'info' }), + new winston.transports.File({ filename: 'combined.log' }) ] }); @@ -19,10 +20,24 @@ const needsFunding = function (req) { return false } else if (req.body.comment.user.login !== config.githubUsername) { return false + } else if (!hasAddress(req)) { + console.log('does not have address'); + return false; } return true } +const hasAddress = function(req) { + let commentBody = req.body.comment.body; + console.log(commentBody); + console.log(commentBody.search('Contract address:')); + if (commentBody.search('Contract address:') === -1) { + return false; + } else { + return true; + } +} + const getAddress = function (req) { let commentBody = req.body.comment.body; return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60) @@ -49,12 +64,14 @@ const getLabel = function (req) { if (config.debug) { return getLabelMock(req); } + console.log('starting getLabel...'); return new Promise((resolve, reject) => { github.getLabels(req) .then(labels => { let bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name)); if (bountyLabels.length === 1) { - resolve(bountyLabels[0]); + console.log('getLabel promise finishes'); + resolve(bountyLabels[0]); } else { error(req.body, 'More than 1 label found: [' + labels.length + ']'); reject(new Error('More than 1 label found: ['+ labels.length + ']')); @@ -82,6 +99,7 @@ const getAmount = function (req) { let label = values[0]; let tokenPrice = values[1]; let amountToPayDollar = config.priceHour * config.bountyLabels[label]; + console.log('getAmount finishes'); resolve(amountToPayDollar / tokenPrice); }) .catch((err) => { diff --git a/bot/prices.js b/bot/prices.js index 66d2ce4..fd213c3 100644 --- a/bot/prices.js +++ b/bot/prices.js @@ -24,7 +24,8 @@ const getGasPrice = function() { // safeLowWait returns GWei (10^10 Wei). let jsonBody = JSON.parse(body.join('')); let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10); - resolve(gasPriceWei); + console.log('getGasPrice finishes') + resolve(gasPriceWei); }); }); // handle connection errors of the request diff --git a/config/default.js b/config/default.js index 908caad..372a671 100644 --- a/config/default.js +++ b/config/default.js @@ -32,5 +32,8 @@ module.exports = { bountyLabels: {}, // username for the bot which has to comment for starting the process (e.g. status-bounty-) - githubUsername: '' + githubUsername: '', + + // Activate real transactions + realTransaction: false } diff --git a/index.js b/index.js index d40b509..14256f1 100644 --- a/index.js +++ b/index.js @@ -28,19 +28,20 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) { } else if (!bot.needsFunding(req)) { return res.sendStatus(204); } - + console.log('new req to process:' + req.body); setTimeout(() => { processRequest(req) .then(() => { - return res.sendStatus(200); - }) + console.log('Well funded'); + }) .catch((err) => { bot.error('Error funding issue: ' + req.body.issue.url); bot.error('error: ' + err); bot.error('dump: ' + req); - return res.sendStatus(204); }); }, config.delayInMiliSeconds); + + return res.sendStatus(200); }); const processRequest = function (req) { @@ -51,30 +52,33 @@ const processRequest = function (req) { // Asynchronous requests for Gas Price and Amount const amountPromise = bot.getAmount(req); const gasPricePromise = bot.getGasPrice(); + console.log('processingRequest...'); + return new Promise((resolve, reject) => { + Promise.all([amountPromise, gasPricePromise]) + .then(function (results) { + let amount = results[0]; + let gasPrice = results[1]; + let transaction = sendTransaction(eth, from, to, amount, gasPrice); - Promise.all([amountPromise, gasPricePromise]) - .then(function (results) { - let amount = results[0]; - let gasPrice = results[1]; - let transaction = sendTransaction(eth, from, to, amount, gasPrice); + transaction + .then(function () { + resolve(); + }) + .catch(function (err) { + reject(err); + }); - transaction - .then(function () { - return res.sendStatus(200); - }) - .catch(function (err) { - reject(err); - }); - - }) - .catch(function (err) { - reject(err); + }) + .catch(function (err) { + reject(err); + }); }); } const sendTransaction = function (eth, from, to, amount, gasPrice) { + console.log('sending transaction...'); return new Promise((resolve, reject) => { - if (config.debug) { + if (!config.realTransaction) { let txID = -1; bot.logTransaction(txID, from, to, amount, gasPrice); resolve();