2018-01-22 15:49:04 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const https = require('https');
|
|
|
|
const config = require('../config');
|
|
|
|
|
2018-01-22 16:23:35 +00:00
|
|
|
// Returns the url for getting the labels of a request (Github API v3)
|
2018-01-22 15:49:04 +00:00
|
|
|
// req has req.issue.labels_url
|
|
|
|
const getLabelsURL = function(req) {
|
2018-01-22 20:21:31 +00:00
|
|
|
let url = req.body.issue.labels_url;
|
2018-01-22 15:49:04 +00:00
|
|
|
// Make the URL generic removing the name of the label
|
|
|
|
return url.replace('{/name}', '');
|
|
|
|
}
|
|
|
|
|
2018-01-22 16:23:35 +00:00
|
|
|
// Returns all labelNames of a given issue (Github API v3)
|
2018-01-22 15:49:04 +00:00
|
|
|
const getLabels = function(req) {
|
|
|
|
let url = getLabelsURL(req);
|
2018-01-22 20:21:31 +00:00
|
|
|
const options = {
|
|
|
|
hostname: 'api.github.com',
|
|
|
|
path: '/repos/jomsdev/my-github-bot/issues/6/labels',
|
|
|
|
headers: { 'User-Agent': 'kafkasl' }
|
|
|
|
};
|
|
|
|
console.log('Url in getLabels(): [' + url + ']');
|
2018-01-22 15:49:04 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-22 20:21:31 +00:00
|
|
|
const request = https.get(options, (response) => {
|
2018-01-22 15:49:04 +00:00
|
|
|
// handle http errors
|
|
|
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
2018-01-22 20:21:31 +00:00
|
|
|
bot.error(response, 'Failed to load page, status code: ' + response.statusCode);
|
2018-01-22 15:49:04 +00:00
|
|
|
reject(new Error('Failed to load page, status code: ' + response.statusCode));
|
|
|
|
}
|
2018-01-22 20:21:31 +00:00
|
|
|
console.log('Processing Promise');
|
2018-01-22 15:49:04 +00:00
|
|
|
// 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', () => {
|
|
|
|
|
2018-01-22 20:21:31 +00:00
|
|
|
let labels = JSON.parse(body.join('')).map(labelObj => labelObj.name);
|
|
|
|
let bountyLabel = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
|
|
|
console.log('Label: ' + bountyLabel);
|
|
|
|
resolve(bountyLabel);
|
2018-01-22 15:49:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// handle connection errors of the request
|
|
|
|
request.on('error', (err) => reject(err))
|
|
|
|
});
|
|
|
|
}
|
2018-01-22 20:21:31 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-01-22 22:43:58 +00:00
|
|
|
getLabels: getLabels
|
2018-01-22 20:21:31 +00:00
|
|
|
}
|