I recently published https://github.com/metabolize/rq-dashboard-on-heroku and want to add badges to show the locked version of Python and rq-dashboard, the main dependency it’s wrapping. This is along the lines of #2259, which was for package.json-based applications, and also included some discussion of a Python application that used `requirements.txt`. It’s useful for showing the pinned version of any dependency in a Python application that uses a lockfile. In the future, as an alternative to reading Pipfile.lock, I could see expanding this to read Pipfile. However for my purposes I prefer to show the locked dependency, since that’s the version that a user of my package would actually get if they ran it on Heroku.
105 lines
2.8 KiB
JavaScript
105 lines
2.8 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 url = `/repos/${user}/${repo}/contents/${filename}`
|
|
const options = { qs: { ref: branch } }
|
|
const { content } = await serviceInstance._requestJson({
|
|
schema: contentSchema,
|
|
url,
|
|
options,
|
|
errorMessages,
|
|
})
|
|
|
|
try {
|
|
return Buffer.from(content, 'base64').toString('utf-8')
|
|
} catch (e) {
|
|
throw new InvalidResponse({ prettyMessage: 'undecodable content' })
|
|
}
|
|
} else {
|
|
const url = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`
|
|
return serviceInstance._request({
|
|
url,
|
|
errorMessages,
|
|
})
|
|
}
|
|
}
|
|
|
|
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 {
|
|
const url = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`
|
|
return serviceInstance._requestJson({
|
|
schema,
|
|
url,
|
|
errorMessages,
|
|
})
|
|
}
|
|
}
|
|
|
|
const releaseInfoSchema = Joi.object({
|
|
tag_name: Joi.string().required(),
|
|
prerelease: Joi.boolean().required(),
|
|
}).required()
|
|
|
|
// Fetch the 'latest' release (as defined by the GitHub API)
|
|
async function fetchLatestRelease(serviceInstance, { user, repo }) {
|
|
const commonAttrs = {
|
|
errorMessages: errorMessagesFor('no releases or repo not found'),
|
|
}
|
|
const releaseInfo = await serviceInstance._requestJson({
|
|
schema: releaseInfoSchema,
|
|
url: `/repos/${user}/${repo}/releases/latest`,
|
|
...commonAttrs,
|
|
})
|
|
return releaseInfo
|
|
}
|
|
|
|
module.exports = {
|
|
fetchIssue,
|
|
fetchJsonFromRepo,
|
|
fetchLatestRelease,
|
|
releaseInfoSchema,
|
|
}
|