Files
shields/services/sourcegraph/sourcegraph.service.js
Paul Melnikow 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

56 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { BaseJsonService } = require('..')
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'
}
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',
pattern: ':repo',
namedParams: {
repo: 'github.com/gorilla/mux',
},
staticPreview: this.render({ projectsCount: '9.9k projects' }),
},
]
}
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() })
}
}