Files
shields/services/github/github-milestone.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

82 lines
1.8 KiB
JavaScript

import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, httpErrorsFor } from './github-helpers.js'
const schema = Joi.array()
.items(
Joi.object({
state: Joi.string().required(),
})
)
.required()
export default class GithubMilestone extends GithubAuthV3Service {
static category = 'issue-tracking'
static route = {
base: 'github/milestones',
pattern: ':variant(open|closed|all)/:user/:repo',
}
static examples = [
{
title: 'GitHub milestones',
namedParams: {
user: 'badges',
repo: 'shields',
variant: 'open',
},
staticPreview: {
label: 'milestones',
message: '2',
color: 'red',
},
documentation,
},
]
static defaultBadgeData = {
label: 'milestones',
color: 'informational',
}
static render({ user, repo, variant, milestones }) {
const milestoneLength = milestones.length
let color
let qualifier = ''
switch (variant) {
case 'all':
color = 'blue'
break
case 'open':
color = 'red'
qualifier = 'active'
break
case 'closed':
color = 'green'
qualifier = 'completed'
break
}
return {
label: `${qualifier}${qualifier ? ' ' : ''}milestones`,
message: metric(milestoneLength),
color,
}
}
async fetch({ user, repo, variant }) {
return this._requestJson({
url: `/repos/${user}/${repo}/milestones?state=${variant}`,
schema,
httpErrors: httpErrorsFor('repo not found'),
})
}
async handle({ user, repo, variant }) {
const milestones = await this.fetch({ user, repo, variant })
return this.constructor.render({ user, repo, variant, milestones })
}
}