* 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
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
import Joi from 'joi'
|
|
import { metric } from '../text-formatters.js'
|
|
import { nonNegativeInteger } from '../validators.js'
|
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
|
import { documentation, httpErrorsFor } from './github-helpers.js'
|
|
|
|
const schema = Joi.object({ total_commits: nonNegativeInteger }).required()
|
|
|
|
const queryParamSchema = Joi.object({
|
|
base: Joi.string().required(),
|
|
head: Joi.string().required(),
|
|
}).required()
|
|
|
|
export default class GithubCommitsDifference extends GithubAuthV3Service {
|
|
static category = 'activity'
|
|
static route = {
|
|
base: 'github/commits-difference',
|
|
pattern: ':user/:repo',
|
|
queryParamSchema,
|
|
}
|
|
|
|
static examples = [
|
|
{
|
|
title: 'GitHub commits difference between two branches/tags/commits',
|
|
namedParams: {
|
|
user: 'microsoft',
|
|
repo: 'vscode',
|
|
},
|
|
queryParams: {
|
|
base: '1.60.0',
|
|
head: '82f2db7',
|
|
},
|
|
staticPreview: this.render({
|
|
commitCount: 9227,
|
|
}),
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'commits difference', namedLogo: 'github' }
|
|
|
|
static render({ commitCount }) {
|
|
return {
|
|
message: metric(commitCount),
|
|
color: 'blue',
|
|
}
|
|
}
|
|
|
|
async handle({ user, repo }, { base, head }) {
|
|
const notFoundMessage = 'could not establish commit difference between refs'
|
|
const { total_commits: commitCount } = await this._requestJson({
|
|
schema,
|
|
url: `/repos/${user}/${repo}/compare/${base}...${head}`,
|
|
httpErrors: httpErrorsFor(notFoundMessage),
|
|
})
|
|
|
|
return this.constructor.render({ commitCount })
|
|
}
|
|
}
|