2018-01-22 15:49:04 +00:00
|
|
|
'use strict'
|
|
|
|
|
2018-03-19 21:37:38 +00:00
|
|
|
const https = require('https')
|
|
|
|
const config = require('../config')
|
2018-03-19 22:02:24 +00:00
|
|
|
const bot = require('../bot')
|
2018-01-22 15:49:04 +00:00
|
|
|
|
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
|
2018-03-19 22:02:24 +00:00
|
|
|
function getLabelsURL (req) {
|
|
|
|
// Make the URL generic removing the name of the label
|
2018-03-20 08:54:50 +00:00
|
|
|
return req.body.issue.labels_url.replace('{/name}', '')
|
2018-01-24 22:37:39 +00:00
|
|
|
}
|
2018-01-22 15:49:04 +00:00
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
// Returns all the bounty labelNames of a given issue (Github API v3)
|
2018-03-19 22:02:24 +00:00
|
|
|
function getLabels (req) {
|
|
|
|
const path = getLabelsURL(req).replace('https://api.github.com', '')
|
|
|
|
const options = {
|
|
|
|
hostname: 'api.github.com',
|
|
|
|
path: path,
|
|
|
|
headers: { 'User-Agent': config.githubUsername }
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const request = https.get(options, (response) => {
|
|
|
|
// handle http errors
|
|
|
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
|
|
|
bot.error(response, `Failed to load page, status code: ${response.statusCode}`)
|
|
|
|
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', () => {
|
|
|
|
const labels = JSON.parse(body.join('')).map(labelObj => labelObj.name)
|
|
|
|
resolve(labels)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// handle connection errors of the request
|
|
|
|
request.on('error', (err) => reject(err))
|
|
|
|
})
|
2018-01-22 15:49:04 +00:00
|
|
|
}
|
2018-01-22 20:21:31 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-03-19 22:02:24 +00:00
|
|
|
getLabels: getLabels
|
2018-01-22 20:21:31 +00:00
|
|
|
}
|