Files
shields/services/sourcegraph/sourcegraph.service.js
Christian Haag fba860fbe9 Refactor service classes [snyk sonar sourceforge sourcegraph spack] (#5632)
* Refactor service classes snyk-sonar-sourceforge-sourcegraph-spack

* Amend end-line issue on pull-request #5632

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-10-03 16:14:47 +00:00

47 lines
1.1 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 category = 'other'
static route = {
base: 'sourcegraph/rrc',
pattern: ':repo(.*?)',
}
static examples = [
{
title: 'Sourcegraph for Repo Reference Count',
pattern: ':repo',
namedParams: {
repo: 'github.com/gorilla/mux',
},
staticPreview: this.render({ projectsCount: '9.9k projects' }),
},
]
static defaultBadgeData = { color: 'brightgreen', label: 'used by' }
static render({ projectsCount }) {
return {
message: projectsCount,
}
}
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() })
}
}