* 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
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
import Joi from 'joi'
|
|
import { optionalUrl, nonNegativeInteger } from '../validators.js'
|
|
import { renderContributorBadge } from '../contributor-count.js'
|
|
import { documentation, httpErrorsFor } from './gitlab-helper.js'
|
|
import GitLabBase from './gitlab-base.js'
|
|
|
|
const schema = Joi.object({ 'x-total': nonNegativeInteger }).required()
|
|
|
|
const queryParamSchema = Joi.object({
|
|
gitlab_url: optionalUrl,
|
|
}).required()
|
|
|
|
export default class GitlabContributors extends GitLabBase {
|
|
static category = 'activity'
|
|
static route = {
|
|
base: 'gitlab/contributors',
|
|
pattern: ':project+',
|
|
queryParamSchema,
|
|
}
|
|
|
|
static examples = [
|
|
{
|
|
title: 'GitLab contributors',
|
|
namedParams: {
|
|
project: 'gitlab-org/gitlab',
|
|
},
|
|
staticPreview: this.render({ contributorCount: 418 }),
|
|
documentation,
|
|
},
|
|
{
|
|
title: 'GitLab (self-managed) contributors',
|
|
queryParams: { gitlab_url: 'https://jihulab.com' },
|
|
namedParams: {
|
|
project: 'gitlab-cn/gitlab',
|
|
},
|
|
staticPreview: this.render({ contributorCount: 415 }),
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'contributors' }
|
|
|
|
static render({ contributorCount }) {
|
|
return renderContributorBadge({ contributorCount })
|
|
}
|
|
|
|
async handle({ project }, { gitlab_url: baseUrl = 'https://gitlab.com' }) {
|
|
// https://docs.gitlab.com/ee/api/repositories.html#contributors
|
|
const { res } = await this._request(
|
|
this.authHelper.withBearerAuthHeader({
|
|
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(
|
|
project
|
|
)}/repository/contributors`,
|
|
options: { searchParams: { page: '1', per_page: '1' } },
|
|
httpErrors: httpErrorsFor('project not found'),
|
|
})
|
|
)
|
|
const data = this.constructor._validate(res.headers, schema)
|
|
// The total number of contributors is in the `x-total` field in the headers.
|
|
// https://docs.gitlab.com/ee/api/index.html#other-pagination-headers
|
|
const contributorCount = data['x-total']
|
|
return this.constructor.render({ contributorCount })
|
|
}
|
|
}
|