Files
shields/services/docker/docker-helpers.js
Paula Barszcz bb326a0f93 [DockerSize] Docker image size multi arch (#8290)
* Get the size of the docker image taking architecture into account

Co-authored-by: chris48s <chris48s@users.noreply.github.com>
2022-08-25 19:17:05 +01:00

86 lines
1.9 KiB
JavaScript

import Joi from 'joi'
// see https://github.com/badges/shields/pull/1690
import { NotFound } from '../index.js'
const dockerBlue = '066da5'
// Valid architecture values: https://golang.org/doc/install/source#environment (GOARCH)
const archSchema = Joi.alternatives(
Joi.string().valid(
'amd64',
'arm',
'arm64',
's390x',
'386',
'ppc64',
'ppc64le',
'wasm',
'mips',
'mipsle',
'mips64',
'mips64le',
'riscv64'
),
Joi.number().valid(386).cast('string')
)
function buildDockerUrl(badgeName, includeTagRoute) {
if (includeTagRoute) {
return {
base: `docker/${badgeName}`,
pattern: ':user/:repo/:tag*',
}
} else {
return {
base: `docker/${badgeName}`,
pattern: ':user/:repo',
}
}
}
function getDockerHubUser(user) {
return user === '_' ? 'library' : user
}
async function getMultiPageData({ user, repo, fetch }) {
const data = await fetch({ user, repo })
if (data.count === 0) {
throw new NotFound({ prettyMessage: 'repository not found' })
}
const numberOfPages = Math.ceil(data.count / 100) // Maximum of 100 results can be returned per page
if (numberOfPages === 1) {
return data.results
}
const pageData = await Promise.all(
[...Array(numberOfPages - 1).keys()].map((_, i) =>
fetch({ user, repo, page: ++i + 1 })
)
)
return [...data.results].concat(...pageData.map(p => p.results))
}
function getDigestSemVerMatches({ data, digest }) {
const matches = data
.filter(d => d.images.some(i => i.digest === digest))
.map(d => d.name)
let version = matches[0]
matches.forEach(name => {
const dots = (name.match(/\./g) || []).length
const olddots = (version.match(/\./g) || []).length
version = dots >= olddots && name !== 'latest' ? name : version
})
return version
}
export {
archSchema,
dockerBlue,
buildDockerUrl,
getDockerHubUser,
getMultiPageData,
getDigestSemVerMatches,
}