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

97 lines
2.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({
open_issues: nonNegativeInteger,
closed_issues: nonNegativeInteger,
title: Joi.string().required(),
}).required()
export default class GithubMilestoneDetail extends GithubAuthV3Service {
static category = 'issue-tracking'
static route = {
base: 'github/milestones',
pattern:
':variant(issues-closed|issues-open|issues-total|progress|progress-percent)/:user/:repo/:number([0-9]+)',
}
static examples = [
{
title: 'GitHub milestone',
namedParams: {
variant: 'issues-open',
user: 'badges',
repo: 'shields',
number: '1',
},
staticPreview: {
label: 'milestone issues',
message: '17/22',
color: 'blue',
},
documentation,
},
]
static defaultBadgeData = { label: 'milestones', color: 'informational' }
static render({ user, repo, variant, number, milestone }) {
let milestoneMetric
let color
let label = ''
switch (variant) {
case 'issues-open':
milestoneMetric = milestone.open_issues
color = 'red'
label = 'open issues'
break
case 'issues-closed':
milestoneMetric = milestone.closed_issues
color = 'green'
label = 'closed issues'
break
case 'issues-total':
milestoneMetric = milestone.open_issues + milestone.closed_issues
color = 'blue'
label = 'issues'
break
case 'progress':
milestoneMetric = `${milestone.closed_issues}/${
milestone.open_issues + milestone.closed_issues
}`
color = 'blue'
break
case 'progress-percent':
milestoneMetric = `${Math.floor(
(milestone.closed_issues /
(milestone.open_issues + milestone.closed_issues)) *
100
)}%`
color = 'blue'
}
return {
label: `${milestone.title}${label ? ' ' : ''}${label}`,
message: metric(milestoneMetric),
color,
}
}
async fetch({ user, repo, number }) {
return this._requestJson({
url: `/repos/${user}/${repo}/milestones/${number}`,
schema,
httpErrors: httpErrorsFor('repo or milestone not found'),
})
}
async handle({ user, repo, variant, number }) {
const milestone = await this.fetch({ user, repo, number })
return this.constructor.render({ user, repo, variant, number, milestone })
}
}