Files
shields/services/github/github-common-fetch.js
Anton Gilgur 7f7ecfd111 add sort and prerelease query params to [GitHubCommitsSince] (also refactors [GitHubRelease], [GitHubTag]) (#4395)
* move getLatestRelease logic from [GitHubRelease] to common-fetch

- so that the sorting and prerelease logic can be used for other
  services too, like commits-since

* add sort and prerelease query params to [GitHubCommitsSince]

- they only work when using the 'latest' version of course

- uses same logic as release service

* reuse queryParamSchema from common-fetch for [GitHubTag]

- as they were the exact same schema

* refactor [GitHubCommitsSince] tests to use withRegex

- and the same variable everywhere

* split out release/tag logic from common-fetch to common-release (affects [GitHubCommitsSince], [GitHubRelease], [GitHubTag])

- this logic is only used for code related to releases & tags, so made
  sense as a split point

Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
2020-02-13 22:18:29 -06:00

85 lines
2.3 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { errorMessagesFor } = require('./github-helpers')
const { InvalidResponse } = require('..')
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}`,
errorMessages: errorMessagesFor('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 = 'master', filename }
) {
const errorMessages = errorMessagesFor(
`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: { qs: { ref: branch } },
errorMessages,
})
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}`,
errorMessages,
})
return buffer
}
}
async function fetchJsonFromRepo(
serviceInstance,
{ schema, user, repo, branch = 'master', 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}`,
errorMessages: errorMessagesFor(
`repo not found, branch not found, or ${filename} missing`
),
})
}
}
module.exports = {
fetchIssue,
fetchRepoContent,
fetchJsonFromRepo,
}