Add [Repology] repositories badge (#4529)

* Add [Repology] repositories badge

Partly addresses #929

* Replace JSON API with SVG scrape

* Fix failing tests

* Replace Joi.number with nonNegativeInteger

* Replace integer with string in failing test

* Update title to specify that the badge is for num of repositories

* Remove repology-repositories.spec.js

* Remove keywords

* Use the metric text formatter

* Remove mocked svg test

* Remove the "in" preposition
This commit is contained in:
Matan Kushner
2020-01-14 23:33:09 -05:00
committed by Caleb Cartwright
parent b1c069c826
commit 95a54e8d35
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
'use strict'
const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { BaseSvgScrapingService } = require('..')
const schema = Joi.object({
message: nonNegativeInteger,
}).required()
module.exports = class RepologyRepositories extends BaseSvgScrapingService {
static get category() {
return 'platform-support'
}
static get route() {
return {
base: 'repology/repositories',
pattern: ':projectName',
}
}
static get examples() {
return [
{
title: 'Repology - Repositories',
namedParams: { projectName: 'starship' },
staticPreview: this.render({ repositoryCount: '18' }),
},
]
}
static get defaultBadgeData() {
return {
label: 'repositories',
color: 'blue',
}
}
static render({ repositoryCount }) {
return {
message: metric(repositoryCount),
}
}
async handle({ projectName }) {
const { message: repositoryCount } = await this._requestSvg({
schema,
url: `https://repology.org/badge/tiny-repos/${projectName}.svg`,
})
return this.constructor.render({ repositoryCount })
}
}

View File

@@ -0,0 +1,18 @@
'use strict'
const t = (module.exports = require('../tester').createServiceTester())
const { nonNegativeInteger } = require('../validators')
t.create('Existing project')
.get('/starship.json')
.expectBadge({
label: 'repositories',
message: nonNegativeInteger,
})
t.create('Non-existent project')
.get('/invalidprojectthatshouldnotexist.json')
.expectBadge({
label: 'repositories',
message: '0',
})