refactor [bountysource] service (#2492)

This commit is contained in:
chris48s
2018-12-10 21:21:39 +00:00
committed by GitHub
parent b74fe4ed6f
commit 9189438a91
2 changed files with 38 additions and 72 deletions

View File

@@ -1,16 +1,36 @@
'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const BaseJsonService = require('../base-json')
const Joi = require('joi')
const { nonNegativeInteger } = require('../validators')
const { metric } = require('../../lib/text-formatters')
const schema = Joi.object({ activity_total: nonNegativeInteger })
module.exports = class Bountysource extends BaseJsonService {
async fetch({ team }) {
const url = `https://api.bountysource.com/teams/${team}`
return this._requestJson({
schema,
url,
options: {
headers: { Accept: 'application/vnd.bountysource+json; version=2' },
},
})
}
module.exports = class Bountysource extends LegacyService {
static get category() {
return 'funding'
}
static get defaultBadgeData() {
return { label: 'bounties' }
}
static get route() {
return {
base: 'bountysource',
base: 'bountysource/team',
pattern: ':team/activity',
}
}
@@ -18,47 +38,21 @@ module.exports = class Bountysource extends LegacyService {
return [
{
title: 'Bountysource',
previewUrl: 'team/mozilla-core/activity',
namedParams: { team: 'mozilla-core' },
staticExample: this.render({ total: 53000 }),
},
]
}
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/bountysource\/team\/([^/]+)\/activity\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const team = match[1] // eg, `mozilla-core`.
const format = match[2]
const url = `https://api.bountysource.com/teams/${team}`
const options = {
headers: { Accept: 'application/vnd.bountysource+json; version=2' },
}
const badgeData = getBadgeData('bounties', data)
request(url, options, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
if (res.statusCode !== 200) {
throw Error('Bad response.')
}
const parsedData = JSON.parse(buffer)
const activity = parsedData.activity_total
badgeData.colorscheme = 'brightgreen'
badgeData.text[1] = activity
sendBadge(format, badgeData)
} catch (e) {
if (res.statusCode === 404) {
badgeData.text[1] = 'not found'
} else {
badgeData.text[1] = 'invalid'
}
sendBadge(format, badgeData)
}
})
})
)
static render({ total }) {
return {
message: metric(total),
color: 'brightgreen',
}
}
async handle({ team }) {
const json = await this.fetch({ team })
return this.constructor.render({ total: json.activity_total })
}
}

View File

@@ -2,7 +2,7 @@
const Joi = require('joi')
const ServiceTester = require('../service-tester')
const { invalidJSON } = require('../response-fixtures')
const { isMetric } = require('../test-validators')
const t = (module.exports = new ServiceTester({
id: 'bountysource',
@@ -14,9 +14,7 @@ t.create('bounties (valid)')
.expectJSONTypes(
Joi.object().keys({
name: 'bounties',
value: Joi.number()
.integer()
.positive(),
value: isMetric,
})
)
@@ -26,29 +24,3 @@ t.create('bounties (invalid team)')
name: 'bounties',
value: 'not found',
})
t.create('bounties (connection error)')
.get('/team/mozilla-core/activity.json')
.networkOff()
.expectJSON({ name: 'bounties', value: 'inaccessible' })
t.create('bounties (unexpected response)')
.get('/team/mozilla-core/activity.json')
.intercept(nock =>
nock('https://api.bountysource.com')
.get('/teams/mozilla-core')
.reply(invalidJSON)
)
.expectJSON({ name: 'bounties', value: 'invalid' })
t.create('bounties (error response)')
.get('/team/mozilla-core/activity.json')
.intercept(nock =>
nock('https://api.bountysource.com')
.get('/teams/mozilla-core')
.reply(500, '{"error":"oh noes!!"}')
)
.expectJSON({
name: 'bounties',
value: 'invalid',
})