diff --git a/services/sourcegraph/sourcegraph.service.js b/services/sourcegraph/sourcegraph.service.js index a901dd2e68..b058dbd95c 100644 --- a/services/sourcegraph/sourcegraph.service.js +++ b/services/sourcegraph/sourcegraph.service.js @@ -1,9 +1,21 @@ 'use strict' -const LegacyService = require('../legacy-service') -const { makeBadgeData: getBadgeData } = require('../../lib/badge-data') +const Joi = require('joi') +const BaseJsonService = require('../base-json') -module.exports = class Sourcegraph extends LegacyService { +const projectsCountRegex = /^\s[0-9]*(\.[0-9]k)?\sprojects$/ +const schema = Joi.object({ + value: Joi.string() + .regex(projectsCountRegex) + .required(), +}).required() + +module.exports = class Sourcegraph extends BaseJsonService { + static render({ projectsCount }) { + return { + message: projectsCount, + } + } static get category() { return 'other' } @@ -11,43 +23,33 @@ module.exports = class Sourcegraph extends LegacyService { static get route() { return { base: 'sourcegraph/rrc', + pattern: ':repo(.*)', } } + static get defaultBadgeData() { + return { color: 'brightgreen', label: 'used by' } + } + static get examples() { return [ { title: 'Sourcegraph for Repo Reference Count', - previewUrl: 'github.com/gorilla/mux', + pattern: ':repo', + namedParams: { + repo: 'github.com/gorilla/mux', + }, + staticExample: this.render({ projectsCount: '9.9k projects' }), }, ] } - static registerLegacyRouteHandler({ camp, cache }) { - camp.route( - /^\/sourcegraph\/rrc\/([\s\S]+)\.(svg|png|gif|jpg|json)$/, - cache((data, match, sendBadge, request) => { - const repo = match[1] - const format = match[2] - const apiUrl = `https://sourcegraph.com/.api/repos/${repo}/-/shield` - const badgeData = getBadgeData('used by', data) - request(apiUrl, (err, res, buffer) => { - if (err != null) { - badgeData.text[1] = 'inaccessible' - sendBadge(format, badgeData) - return - } - try { - badgeData.colorscheme = 'brightgreen' - const data = JSON.parse(buffer) - badgeData.text[1] = data.value.trim() // The value generally comes with a leading space. - sendBadge(format, badgeData) - } catch (e) { - badgeData.text[1] = 'invalid' - sendBadge(format, badgeData) - } - }) - }) - ) + async handle({ repo }) { + const url = `https://sourcegraph.com/.api/repos/${repo}/-/shield` + const json = await this._requestJson({ + url, + schema, + }) + return this.constructor.render({ projectsCount: json.value.trim() }) } } diff --git a/services/sourcegraph/sourcegraph.tester.js b/services/sourcegraph/sourcegraph.tester.js index 626124869f..22807b00cd 100644 --- a/services/sourcegraph/sourcegraph.tester.js +++ b/services/sourcegraph/sourcegraph.tester.js @@ -1,18 +1,15 @@ 'use strict' const Joi = require('joi') -const ServiceTester = require('../service-tester') +const t = (module.exports = require('../create-service-tester')()) const { withRegex } = require('../test-validators') // Matches API responses such as "0 projects", "1 projects", "182 projects", "14.0k projects". // There may be other cases not covered by this regex, but hopefully the tested projects won't vary much. const projectsCount = withRegex(/^[0-9]*(\.[0-9]k)?\sprojects$/) -const t = new ServiceTester({ id: 'sourcegraph', title: 'Sourcegraph' }) -module.exports = t - t.create('project usage count') - .get('/rrc/github.com/theupdateframework/notary.json') + .get('/github.com/theupdateframework/notary.json') .expectJSONTypes( Joi.object().keys({ name: 'used by', @@ -21,8 +18,8 @@ t.create('project usage count') ) t.create('project without any available information') - .get('/rrc/github.com/PyvesB/EmptyRepo.json') + .get('/github.com/PyvesB/EmptyRepo.json') .expectJSON({ name: 'used by', - value: 'invalid', + value: '0 projects', })