Merged WIP

This commit is contained in:
Pol.Alvarez@BSC 2018-01-22 17:08:23 +01:00
commit f14bdf05a6
5 changed files with 74 additions and 36 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules
npm-debug.log
.ssh
*.log

37
bot/github.js Normal file
View File

@ -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))
});
}

View File

@ -21,14 +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'))
@ -45,15 +37,16 @@ const getAddress = function(req) {
}
const getLabel = function(req) {
let labelNames = req.body.issue.labels.map(labelObj => labelObj.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) {
@ -76,8 +69,8 @@ const getGasPrice = function(req) {
// Logging functions
const logFunding = function(txId, from, to, amount, gasPrice){
logger.info("\nSuccesfully funded bounty with transaction ", txId);
const logTransaction = function(txId, from, to, amount, gasPrice){
logger.info("\n[OK] Succesfully funded bounty with transaction ", txId);
logger.info(" * From: ", from);
logger.info(" * To: ", to);
logger.info(" * Amount: ", amount);
@ -89,9 +82,9 @@ const log = function(msg) {
logger.info(msg);
}
const error = function(requestBody, errorMessage) {
const error = function(requestInfo, errorMessage) {
logger.error("[ERROR] Request processing failed: ", errorMessage);
logger.error("[ERROR] Request body: ", requestBody);
logger.error("[ERROR] Request body: ", requestInfo);
}
@ -100,6 +93,8 @@ module.exports = {
needsFunding: needsFunding,
getAddress: getAddress,
getAmount: getAmount,
getGasPrice: getGasPrice,
log: log
getGasPrice: prices.getGasPrice,
log: log,
logTransaction: logTransaction,
error: error
}

View File

@ -1,3 +1,15 @@
const BOUNTY_LABELS = {
'bounty-xs': 1,
'bounty-s': 10,
'bounty-m': 100,
'bounty-l': 1000,
'bounty-xl': 10000
}
module.exports = {
debug: true,
urlEndpoint: '/autobounty/fund',
@ -7,12 +19,4 @@ module.exports = {
token: 'SNT',
gasLimit: 92000,
priceHour: 35,
bountyLabelsHours: {
'bounty-xs': 1,
'bounty-s': 10,
'bounty-m': 100,
'bounty-l': 1000,
'bounty-xl': 10000,
'bounty-xx': 100000
};
}

View File

@ -25,6 +25,7 @@ app.use(helmet());
app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
// TODO decide how long the delay to start everything should be
if (!req.body || !req.body.action) {
bot.error(req, 'Wrong format');
return res.sendStatus(400);
}
@ -42,13 +43,13 @@ app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
Promise.all([amountPromise, gasPricePromise])
.then(function(amount, gasPrice){
sendTransaction(eth, from, to, amount, gasPrice)
sendTransaction(eth, from, to, amount, gasPrice);
})
.catch(function(error) {
bot.error(req.body, error);
});
}
});
const sendTransaction = function(eth, from, to, amount, gasPrice){
@ -68,7 +69,7 @@ const sendTransaction = function(eth, from, to, amount, gasPrice){
return res.status(500).json(err)
}
else {
bot.logFunding(txID, from, to, amount, gasPrice);
bot.logTransaction(txID, from, to, amount, gasPrice);
res.json({ txID })
}
});
@ -77,7 +78,7 @@ const sendTransaction = function(eth, from, to, amount, gasPrice){
return res.sendStatus(200);
} else {
let txId = -1;
bot.logFunding(txID, from, to, amount, gasPrice);
bot.logTransaction(txID, from, to, amount, gasPrice);
}
}