* allow serviceData to override cacheSeconds with a longer value * prevent [endpoint] json cacheSeconds property exceeding service default * allow ShieldsRuntimeError to specify a cacheSeconds property By default error responses use the cacheLength of the service class throwing the error. This allows error to tell the handling layer the maxAge that should be set on the error badge response. * add customExceptions param This 1. allows us to specify custom properties to pass to the exception constructor if we throw any of the standard got errors e.g: `ETIMEDOUT`, `ECONNRESET`, etc 2. uses a custom `cacheSeconds` property (if set on the exception) to set the response maxAge * customExceptions --> systemErrors * errorMessages --> httpErrors
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
import Joi from 'joi'
|
|
import parseLinkHeader from 'parse-link-header'
|
|
import { renderContributorBadge } from '../contributor-count.js'
|
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
|
import { documentation, httpErrorsFor } from './github-helpers.js'
|
|
|
|
// All we do is check its length.
|
|
const schema = Joi.array().items(Joi.object())
|
|
|
|
export default class GithubContributors extends GithubAuthV3Service {
|
|
static category = 'activity'
|
|
static route = {
|
|
base: 'github',
|
|
pattern: ':variant(contributors|contributors-anon)/:user/:repo',
|
|
}
|
|
|
|
static examples = [
|
|
{
|
|
title: 'GitHub contributors',
|
|
namedParams: {
|
|
variant: 'contributors',
|
|
user: 'cdnjs',
|
|
repo: 'cdnjs',
|
|
},
|
|
staticPreview: this.render({ contributorCount: 397 }),
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'contributors' }
|
|
|
|
static render({ contributorCount }) {
|
|
return renderContributorBadge({ contributorCount })
|
|
}
|
|
|
|
async handle({ variant, user, repo }) {
|
|
const isAnon = variant === 'contributors-anon'
|
|
|
|
const { res, buffer } = await this._request({
|
|
url: `/repos/${user}/${repo}/contributors`,
|
|
options: { searchParams: { page: '1', per_page: '1', anon: isAnon } },
|
|
httpErrors: httpErrorsFor('repo not found'),
|
|
})
|
|
|
|
const parsed = parseLinkHeader(res.headers.link)
|
|
let contributorCount
|
|
if (parsed === null) {
|
|
const json = this._parseJson(buffer)
|
|
const contributorInfo = this.constructor._validate(json, schema)
|
|
contributorCount = contributorInfo.length
|
|
} else {
|
|
contributorCount = +parsed.last.page
|
|
}
|
|
|
|
return this.constructor.render({ contributorCount })
|
|
}
|
|
}
|