use fetch/render pattern in [apm appveyor cdnjs clojars gem] (#1901)

* remove tests we don't need
* use fetch/render pattern in [apm appveyor cdnjs clojars gem]
This commit is contained in:
chris48s
2018-08-12 18:27:47 +01:00
committed by GitHub
parent 67193ce09b
commit 0f534cefc0
12 changed files with 110 additions and 128 deletions

View File

@@ -18,7 +18,7 @@ const apmSchema = Joi.object({
})
class BaseAPMService extends BaseJsonService {
async fetch(repo) {
async fetch({ repo }) {
return this._requestJson({
schema: apmSchema,
url: `https://atom.io/api/packages/${repo}`,
@@ -41,13 +41,15 @@ class BaseAPMService extends BaseJsonService {
}
class APMDownloads extends BaseAPMService {
async handle({ repo }) {
const json = await this.fetch(repo)
const downloads = json.downloads
static render({ downloads }) {
return { message: metric(downloads), color: 'green' }
}
async handle({ repo }) {
const json = await this.fetch({ repo })
return this.constructor.render({ downloads: json.downloads })
}
static get category() {
return 'downloads'
}
@@ -66,15 +68,19 @@ class APMDownloads extends BaseAPMService {
}
class APMVersion extends BaseAPMService {
static render({ version }) {
return { message: addv(version), color: versionColor(version) }
}
async handle({ repo }) {
const json = await this.fetch(repo)
const json = await this.fetch({ repo })
const version = json.releases.latest
if (!version)
throw new InvalidResponse({
underlyingError: new Error('version is invalid'),
})
return { message: addv(version), color: versionColor(version) }
return this.constructor.render({ version })
}
static get category() {
@@ -91,15 +97,19 @@ class APMVersion extends BaseAPMService {
}
class APMLicense extends BaseAPMService {
static render({ license }) {
return { message: license, color: 'blue' }
}
async handle({ repo }) {
const json = await this.fetch(repo)
const json = await this.fetch({ repo })
const license = json.metadata.license
if (!license)
throw new InvalidResponse({
underlyingError: new Error('licence is invalid'),
})
return { message: license, color: 'blue' }
return this.constructor.render({ license })
}
static get defaultBadgeData() {

View File

@@ -33,11 +33,6 @@ t.create('License | Package not found')
.get('/l/notapackage.json')
.expectJSON({ name: 'license', value: 'package not found' })
t.create('Connection error')
.get('/v/vim-mode.json')
.networkOff()
.expectJSON({ name: 'apm', value: 'inaccessible' })
t.create('Invalid version')
.get('/dm/vim-mode.json')
.intercept(nock =>

View File

@@ -10,19 +10,19 @@ const appVeyorSchema = Joi.object({
}).required()
module.exports = class AppVeyor extends BaseJsonService {
async handle({ repo, branch }) {
async fetch({ repo, branch }) {
let url = `https://ci.appveyor.com/api/projects/${repo}`
if (branch != null) {
url += `/branch/${branch}`
}
const {
build: { status },
} = await this._requestJson({
return this._requestJson({
schema: appVeyorSchema,
url,
notFoundMessage: 'project not found or access denied',
})
}
static render({ status }) {
if (status === 'success') {
return { message: 'passing', color: 'brightgreen' }
} else if (status !== 'running' && status !== 'queued') {
@@ -32,6 +32,13 @@ module.exports = class AppVeyor extends BaseJsonService {
}
}
async handle({ repo, branch }) {
const {
build: { status },
} = await this.fetch({ repo, branch })
return this.constructor.render({ status })
}
// Metadata
static get category() {
return 'build'

View File

@@ -12,12 +12,23 @@ const cdnjsSchema = Joi.object({
}).required()
module.exports = class Cdnjs extends BaseJsonService {
async handle({ library }) {
async fetch({ library }) {
const url = `https://api.cdnjs.com/libraries/${library}?fields=version`
const json = await this._requestJson({
return this._requestJson({
url,
schema: cdnjsSchema,
})
}
static render({ version }) {
return {
message: versionText(version),
color: versionColor(version),
}
}
async handle({ library }) {
const json = await this.fetch({ library })
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
@@ -25,10 +36,7 @@ module.exports = class Cdnjs extends BaseJsonService {
throw new NotFound()
}
return {
message: versionText(json.version),
color: versionColor(json.version),
}
return this.constructor.render({ version: json.version })
}
// Metadata

View File

@@ -19,17 +19,3 @@ t.create('cdnjs (valid)')
t.create('cdnjs (not found)')
.get('/v/not-a-library.json')
.expectJSON({ name: 'cdnjs', value: 'not found' })
t.create('cdnjs (connection error)')
.get('/v/jquery.json')
.networkOff()
.expectJSON({ name: 'cdnjs', value: 'inaccessible' })
t.create('cdnjs (error response)')
.get('/v/jquery.json')
.intercept(nock =>
nock('https://api.cdnjs.com')
.get('/libraries/jquery?fields=version')
.reply(500, '{"error":"oh noes!!"}')
)
.expectJSON({ name: 'cdnjs', value: 'invalid' })

View File

@@ -11,12 +11,23 @@ const clojarsSchema = Joi.object({
}).required()
module.exports = class Clojars extends BaseJsonService {
async handle({ clojar }) {
async fetch({ clojar }) {
const url = `https://clojars.org/${clojar}/latest-version.json`
const json = await this._requestJson({
return this._requestJson({
url,
schema: clojarsSchema,
})
}
static render({ clojar, version }) {
return {
message: '[' + clojar + ' "' + version + '"]',
color: versionColor(version),
}
}
async handle({ clojar }) {
const json = await this.fetch({ clojar })
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from clojars is:
@@ -24,10 +35,7 @@ module.exports = class Clojars extends BaseJsonService {
throw new NotFound()
}
return {
message: '[' + clojar + ' "' + json.version + '"]',
color: versionColor(json.version),
}
return this.constructor.render({ clojar, version: json.version })
}
// Metadata

View File

@@ -18,17 +18,3 @@ t.create('clojars (valid)')
t.create('clojars (not found)')
.get('/v/not-a-package.json')
.expectJSON({ name: 'clojars', value: 'not found' })
t.create('clojars (connection error)')
.get('/v/jquery.json')
.networkOff()
.expectJSON({ name: 'clojars', value: 'inaccessible' })
t.create('clojars (error response)')
.get('/v/prismic.json')
.intercept(nock =>
nock('https://clojars.org')
.get('/prismic/latest-version.json')
.reply(500, '{"error":"oh noes!!"}')
)
.expectJSON({ name: 'clojars', value: 'invalid' })

View File

@@ -29,7 +29,7 @@ const versionsSchema = Joi.array()
.required()
module.exports = class GemDownloads extends BaseJsonService {
async fetch(repo, info) {
async fetch({ repo, info }) {
const endpoint = info === 'dv' ? 'versions/' : 'gems/'
const schema = info === 'dv' ? versionsSchema : gemsSchema
const url = `https://rubygems.org/api/v1/${endpoint}${repo}.json`
@@ -39,6 +39,14 @@ module.exports = class GemDownloads extends BaseJsonService {
})
}
static render({ label, downloads }) {
return {
label: label,
message: metric(downloads),
color: downloadCountColor(downloads),
}
}
_getLabel(version, info) {
if (version) {
return 'downloads@' + version
@@ -58,13 +66,13 @@ module.exports = class GemDownloads extends BaseJsonService {
splitRubygem.length > 1 ? splitRubygem[splitRubygem.length - 1] : null
version = version === 'stable' ? version : semver.valid(version)
const label = this._getLabel(version, info)
const json = await this.fetch(repo, info)
const json = await this.fetch({ repo, info })
let downloads
if (info === 'dt') {
downloads = metric(json.downloads)
downloads = json.downloads
} else if (info === 'dtv') {
downloads = metric(json.version_downloads)
downloads = json.version_downloads
} else if (info === 'dv') {
let versionData
if (version !== null && version === 'stable') {
@@ -80,13 +88,13 @@ module.exports = class GemDownloads extends BaseJsonService {
versionData = json.filter(function(ver) {
return ver.number === stableVersion
})[0]
downloads = metric(versionData.downloads_count)
downloads = versionData.downloads_count
} else if (version !== null) {
versionData = json.filter(function(ver) {
return ver.number === version
})[0]
downloads = metric(versionData.downloads_count)
downloads = versionData.downloads_count
} else {
throw new InvalidResponse({
underlyingError: new Error('version is null'),
@@ -98,11 +106,7 @@ module.exports = class GemDownloads extends BaseJsonService {
})
}
return {
label: label,
message: downloads,
color: downloadCountColor(downloads),
}
return this.constructor.render({ label, downloads })
}
// Metadata

View File

@@ -8,20 +8,26 @@ const { floorCount: floorCountColor } = require('../../lib/color-formatters')
const ownerSchema = Joi.array().required()
module.exports = class GemOwner extends BaseJsonService {
async handle({ user }) {
async fetch({ user }) {
const url = `https://rubygems.org/api/v1/owners/${user}/gems.json`
const json = await this._requestJson({
return this._requestJson({
url,
schema: ownerSchema,
})
const count = json.length
}
static render({ count }) {
return {
message: count,
color: floorCountColor(count, 10, 50, 100),
}
}
async handle({ user }) {
const json = await this.fetch({ user })
return this.constructor.render({ count: json.length })
}
// Metadata
static get defaultBadgeData() {
return { label: 'gems' }

View File

@@ -25,42 +25,36 @@ const dailySchema = Joi.array()
.required()
module.exports = class GemRank extends BaseJsonService {
_getApiUrl(repo, totalRank, dailyRank) {
let endpoint
if (totalRank) {
endpoint = '/total_ranking.json'
} else if (dailyRank) {
endpoint = '/daily_ranking.json'
}
return `http://bestgems.org/api/v1/gems/${repo}${endpoint}`
}
async handle({ info, repo }) {
async fetch({ info, repo }) {
const totalRank = info === 'rt'
const dailyRank = info === 'rd'
const endpoint = totalRank ? '/total_ranking.json' : '/daily_ranking.json'
const url = `http://bestgems.org/api/v1/gems/${repo}${endpoint}`
const schema = totalRank ? totalSchema : dailySchema
const url = this._getApiUrl(repo, totalRank, dailyRank)
const json = await this._requestJson({
return this._requestJson({
url,
schema,
})
}
let rank
if (totalRank) {
rank = json[0].total_ranking
} else if (dailyRank) {
rank = json[0].daily_ranking
}
const count = Math.floor(100000 / rank)
let message = ordinalNumber(rank)
message += totalRank ? '' : ' daily'
static render({ message, count }) {
return {
message: message,
color: floorCountColor(count, 10, 50, 100),
}
}
async handle({ info, repo }) {
const json = await this.fetch({ info, repo })
const totalRank = info === 'rt'
const rank = totalRank ? json[0].total_ranking : json[0].daily_ranking
const count = Math.floor(100000 / rank)
let message = ordinalNumber(rank)
message += totalRank ? '' : ' daily'
return this.constructor.render({ message, count })
}
// Metadata
static get defaultBadgeData() {
return { label: 'rank' }

View File

@@ -14,18 +14,26 @@ const versionSchema = Joi.object({
}).required()
module.exports = class GemVersion extends BaseJsonService {
async handle({ repo }) {
async fetch({ repo }) {
const url = `https://rubygems.org/api/v1/gems/${repo}.json`
const { version } = await this._requestJson({
return this._requestJson({
url,
schema: versionSchema,
})
}
static render({ version }) {
return {
message: versionText(version),
color: versionColor(version),
}
}
async handle({ repo }) {
const { version } = await this.fetch({ repo })
return this.constructor.render({ version })
}
// Metadata
static get defaultBadgeData() {
return { label: 'gem' }

View File

@@ -30,11 +30,6 @@ t.create('version (not found)')
.get('/v/not-a-package.json')
.expectJSON({ name: 'gem', value: 'not found' })
t.create('version (connection error)')
.get('/v/formatador.json')
.networkOff()
.expectJSON({ name: 'gem', value: 'inaccessible' })
// downloads endpoints
// total downloads
@@ -51,11 +46,6 @@ t.create('total downloads (not found)')
.get('/dt/not-a-package.json')
.expectJSON({ name: 'downloads', value: 'not found' })
t.create('total downloads (connection error)')
.get('/dt/rails.json')
.networkOff()
.expectJSON({ name: 'downloads', value: 'inaccessible' })
// version downloads
t.create('version downloads (valid, stable version)')
.get('/dv/rails/stable.json')
@@ -87,11 +77,6 @@ t.create('version downloads (valid package, version not specified)')
.get('/dv/rails.json')
.expectJSON({ name: 'downloads', value: 'invalid' })
t.create('version downloads (connection error)')
.get('/dv/rails/4.1.0.json')
.networkOff()
.expectJSON({ name: 'downloads', value: 'inaccessible' })
// latest version downloads
t.create('latest version downloads (valid)')
.get('/dtv/rails.json')
@@ -106,11 +91,6 @@ t.create('latest version downloads (not found)')
.get('/dtv/not-a-package.json')
.expectJSON({ name: 'downloads', value: 'not found' })
t.create('latest version downloads (connection error)')
.get('/dtv/rails.json')
.networkOff()
.expectJSON({ name: 'downloads', value: 'inaccessible' })
// users endpoint
t.create('users (valid)')
@@ -126,11 +106,6 @@ t.create('users (not found)')
.get('/u/not-a-package.json')
.expectJSON({ name: 'gems', value: 'not found' })
t.create('users (connection error)')
.get('/u/raphink.json')
.networkOff()
.expectJSON({ name: 'gems', value: 'inaccessible' })
// rank endpoint
t.create('total rank (valid)')
@@ -154,8 +129,3 @@ t.create('daily rank (valid)')
t.create('rank (not found)')
.get('/rt/not-a-package.json')
.expectJSON({ name: 'rank', value: 'not found' })
t.create('rank (connection error)')
.get('/rt/rspec-puppet-facts.json')
.networkOff()
.expectJSON({ name: 'rank', value: 'inaccessible' })