* 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
50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
import Joi from 'joi'
|
|
import { renderLicenseBadge } from '../licenses.js'
|
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
|
import { documentation, httpErrorsFor } from './github-helpers.js'
|
|
|
|
const schema = Joi.object({
|
|
// Some repos do not have a license, in which case GitHub returns `{ license: null }`.
|
|
license: Joi.object({ spdx_id: Joi.string().required() }).allow(null),
|
|
}).required()
|
|
|
|
export default class GithubLicense extends GithubAuthV3Service {
|
|
static category = 'license'
|
|
static route = { base: 'github/license', pattern: ':user/:repo' }
|
|
static examples = [
|
|
{
|
|
title: 'GitHub',
|
|
namedParams: { user: 'mashape', repo: 'apistatus' },
|
|
staticPreview: {
|
|
label: 'license',
|
|
message: 'MIT',
|
|
color: 'green',
|
|
},
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'license' }
|
|
|
|
static render({ license }) {
|
|
if (license === 'NOASSERTION') {
|
|
return { message: 'not identifiable by github' }
|
|
} else if (license) {
|
|
return renderLicenseBadge({ license })
|
|
} else {
|
|
return { message: 'not specified' }
|
|
}
|
|
}
|
|
|
|
async handle({ user, repo }) {
|
|
const { license: licenseObject } = await this._requestJson({
|
|
schema,
|
|
url: `/repos/${user}/${repo}`,
|
|
httpErrors: httpErrorsFor('repo not found'),
|
|
})
|
|
|
|
const license = licenseObject ? licenseObject.spdx_id : undefined
|
|
return this.constructor.render({ license })
|
|
}
|
|
}
|