Add Docker image size support (#333)

* Add Docker image size badge
* Handle tag API responses with pagination
* Support querying specific Docker image arch
* Default to latest amd64
* Demo scoped Docker image size
pull/336/head
Luke Childs 2019-12-12 13:01:22 +07:00 zatwierdzone przez 晋晓炜 Amio /
rodzic eed2475f16
commit ec13cca438
1 zmienionych plików z 47 dodań i 2 usunięć

Wyświetl plik

@ -7,15 +7,18 @@ export default createBadgenHandler({
examples: {
'/docker/pulls/library/ubuntu': 'pulls (library)',
'/docker/stars/library/ubuntu': 'stars (library)',
'/docker/size/library/ubuntu': 'size (library)',
'/docker/pulls/amio/node-chrome': 'pulls (scoped)',
'/docker/stars/library/mongo?icon=docker&label=stars': 'stars (icon & label)',
'/docker/size/lukechilds/bitcoind/latest/amd64': 'size (scoped/tag/architecture)',
},
handlers: {
'/docker/:topic<stars|pulls>/:scope/:name': handler
'/docker/:topic<stars|pulls>/:scope/:name': starPullHandler,
'/docker/size/:scope/:name/:tag?/:architecture?': sizeHandler
}
})
async function handler ({ topic, scope, name }: PathArgs) {
async function starPullHandler ({ topic, scope, name }: PathArgs) {
if (!['stars', 'pulls'].includes(topic)) {
return {
subject: 'docker',
@ -43,3 +46,45 @@ async function handler ({ topic, scope, name }: PathArgs) {
}
}
}
async function sizeHandler ({ scope, name, tag, architecture }: PathArgs) {
tag = tag ? tag : 'latest'
architecture = architecture ? architecture : 'amd64'
/* eslint-disable camelcase */
const endpoint = `https://hub.docker.com/v2/repositories/${scope}/${name}/tags`
let body = await got(endpoint).then(res => res.body)
let results = [...body.results]
while (body.next) {
body = await got(body.next).then(res => res.body)
results = [...results, ...body.results]
}
const tagData = results.find(tagData => tagData.name === tag)
if (!tagData) {
return {
subject: 'docker',
status: 'unknown tag',
color: 'grey'
}
}
const imageData = tagData.images.find(image => image.architecture === architecture)
if (!imageData) {
return {
subject: 'docker',
status: 'unknown architecture',
color: 'grey'
}
}
const sizeInMegabytes = (imageData.size / 1024 / 1024).toFixed(2)
return {
subject: 'docker image size',
status: `${sizeInMegabytes} MB`,
color: 'blue'
}
}