Introduce @octokit/rest npm package to replace handrolled logic

This commit is contained in:
Pedro Pombeiro 2018-03-20 13:50:51 +01:00
parent b975645815
commit a96c434c96
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
7 changed files with 2623 additions and 42 deletions

View File

@ -1,44 +1,16 @@
'use strict' 'use strict'
const https = require('https') const octokit = require('@octokit/rest')()
const config = require('../config')
const bot = require('../bot')
// Returns the url for getting the labels of a request (Github API v3)
// req has req.issue.labels_url
function getLabelsURL (req) {
// Make the URL generic removing the name of the label
return req.body.issue.labels_url.replace('{/name}', '')
}
// Returns all the bounty labelNames of a given issue (Github API v3) // Returns all the bounty labelNames of a given issue (Github API v3)
const getLabels = function (req) { async function getLabels (req) {
const path = getLabelsURL(req).replace('https://api.github.com', '') const labelsPayload = await octokit.issues.getIssueLabels({
const options = { owner: req.body.repository.owner.login,
hostname: 'api.github.com', repo: req.body.repository.name,
path: path, number: req.body.issue.number
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))
}) })
return labelsPayload.data.map(labelObj => labelObj.name)
} }
module.exports = { module.exports = {

View File

@ -53,7 +53,7 @@ async function getLabel (req) {
return bountyLabels[0] return bountyLabels[0]
} }
throw new Error('Error getting bounty labels') throw new Error(`Error getting bounty labels: ${JSON.stringify(labelNames)}`)
} }
async function getAmount (req) { async function getAmount (req) {

View File

@ -1,6 +1,6 @@
'use strict' 'use strict'
function getGasPrice () { async function getGasPrice () {
const url = 'https://ethgasstation.info/json/ethgasAPI.json' const url = 'https://ethgasstation.info/json/ethgasAPI.json'
// return new pending promise // return new pending promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -28,7 +28,7 @@ function getGasPrice () {
}) })
} }
function getTokenPrice (token) { async function getTokenPrice (token) {
if (token === 'STT') { if (token === 'STT') {
return 1 return 1
} }

View File

@ -89,5 +89,5 @@ async function processRequest (req) {
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181
app.listen(port, function () { app.listen(port, function () {
bot.info('Autobounty listening on port ' + port) bot.info(`Autobounty listening on port ${port}`)
}) })

2608
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@octokit/rest": "^15.2.5",
"body-parser": "^1.17.2", "body-parser": "^1.17.2",
"cors": "^2.8.1", "cors": "^2.8.1",
"ethers": "^3.0", "ethers": "^3.0",