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'
const https = require('https')
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}', '')
}
const octokit = require('@octokit/rest')()
// Returns all the bounty labelNames of a given issue (Github API v3)
const getLabels = function (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))
async function getLabels (req) {
const labelsPayload = await octokit.issues.getIssueLabels({
owner: req.body.repository.owner.login,
repo: req.body.repository.name,
number: req.body.issue.number
})
return labelsPayload.data.map(labelObj => labelObj.name)
}
module.exports = {

View File

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

View File

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

View File

@ -49,7 +49,7 @@ const TOKEN_CONTRACTS = {
module.exports = {
// Debug mode for testing the bot
debug: true,
debug: true,
// URL where the bot is listening (e.g. '/funding')
urlEndpoint: '/',
@ -85,5 +85,5 @@ module.exports = {
githubUsername: 'status-open-bounty',
// Activate real transactions
realTransaction: false
realTransaction: false
}

View File

@ -89,5 +89,5 @@ async function processRequest (req) {
const port = process.env.PORT || 8181
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": "",
"license": "ISC",
"dependencies": {
"@octokit/rest": "^15.2.5",
"body-parser": "^1.17.2",
"cors": "^2.8.1",
"ethers": "^3.0",