Files
shields/services/github/github-checks-status.service.js
chris48s 14892e3943 Implement a pattern for dealing with upstream APIs which are slow on the first hit; affects [endpoint] (#9233)
* 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
2023-06-13 21:08:43 +01:00

71 lines
1.7 KiB
JavaScript

import Joi from 'joi'
import { isBuildStatus, renderBuildStatusBadge } from '../build-status.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, httpErrorsFor } from './github-helpers.js'
const schema = Joi.object({
state: isBuildStatus,
}).required()
export default class GithubChecksStatus extends GithubAuthV3Service {
static category = 'build'
static route = {
base: 'github/checks-status',
pattern: ':user/:repo/:ref',
}
static examples = [
{
title: 'GitHub branch checks state',
namedParams: {
user: 'badges',
repo: 'shields',
ref: 'master',
},
staticPreview: renderBuildStatusBadge({
status: 'success',
}),
keywords: ['status'],
documentation,
},
{
title: 'GitHub commit checks state',
namedParams: {
user: 'badges',
repo: 'shields',
ref: '91b108d4b7359b2f8794a4614c11cb1157dc9fff',
},
staticPreview: renderBuildStatusBadge({
status: 'success',
}),
keywords: ['status'],
documentation,
},
{
title: 'GitHub tag checks state',
namedParams: {
user: 'badges',
repo: 'shields',
ref: '3.3.0',
},
staticPreview: renderBuildStatusBadge({
status: 'success',
}),
keywords: ['status'],
documentation,
},
]
static defaultBadgeData = { label: 'checks' }
async handle({ user, repo, ref }) {
const { state } = await this._requestJson({
url: `/repos/${user}/${repo}/commits/${ref}/status`,
httpErrors: httpErrorsFor('ref or repo not found'),
schema,
})
return renderBuildStatusBadge({ status: state })
}
}