Files
shields/services/github/github-common-fetch.js
dependabot[bot] b9d96755ec chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 (#9357)
* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* reformat all the things (prettier 3)

* update tests to await calls to prettier.format()

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
2023-07-10 09:27:51 +00: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 }