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

79 lines
2.2 KiB
JavaScript

import Joi from 'joi'
import { InvalidResponse } from '../index.js'
import { httpErrorsFor } from './github-helpers.js'
const issueSchema = Joi.object({
head: Joi.object({
sha: Joi.string().required(),
}).required(),
}).required()
async function fetchIssue(serviceInstance, { user, repo, number }) {
return serviceInstance._requestJson({
schema: issueSchema,
url: `/repos/${user}/${repo}/pulls/${number}`,
httpErrors: httpErrorsFor('pull request or repo not found'),
})
}
const contentSchema = Joi.object({
// https://github.com/hapijs/joi/issues/1430
content: Joi.string().required(),
encoding: Joi.equal('base64').required(),
}).required()
async function fetchRepoContent(
serviceInstance,
{ user, repo, branch = 'HEAD', filename }
) {
const httpErrors = httpErrorsFor(
`repo not found, branch not found, or ${filename} missing`
)
if (serviceInstance.staticAuthConfigured) {
const { content } = await serviceInstance._requestJson({
schema: contentSchema,
url: `/repos/${user}/${repo}/contents/${filename}`,
options: { searchParams: { ref: branch } },
httpErrors,
})
try {
return Buffer.from(content, 'base64').toString('utf-8')
} catch (e) {
throw new InvalidResponse({ prettyMessage: 'undecodable content' })
}
} else {
const { buffer } = await serviceInstance._request({
url: `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`,
httpErrors,
})
return buffer
}
}
async function fetchJsonFromRepo(
serviceInstance,
{ schema, user, repo, branch = 'HEAD', filename }
) {
if (serviceInstance.staticAuthConfigured) {
const buffer = await fetchRepoContent(serviceInstance, {
user,
repo,
branch,
filename,
})
const json = serviceInstance._parseJson(buffer)
return serviceInstance.constructor._validate(json, schema)
} else {
return serviceInstance._requestJson({
schema,
url: `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`,
httpErrors: httpErrorsFor(
`repo not found, branch not found, or ${filename} missing`
),
})
}
}
export { fetchIssue, fetchRepoContent, fetchJsonFromRepo }