Files
shields/services/docker/docker-stars.service.js
chris48s 880c1fb49c call [docker] with auth (#9803)
* allow user to set dockerhub credentials

* add withJwtAuth function to AuthHelper

* use withJwtAuth in DockerHub badges

* add unit tests for JWT auth

* use auth when calling docker cloud

* refactor and assert fetch helpers call withJwtAuth

* store token for a max duration (defaults to 1 hour)

* tangent: update test example
2023-12-31 14:55:18 +00:00

71 lines
1.6 KiB
JavaScript

import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, pathParams } from '../index.js'
import {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} from './docker-helpers.js'
import { fetch } from './docker-hub-common-fetch.js'
const schema = Joi.object({
star_count: nonNegativeInteger.required(),
}).required()
export default class DockerStars extends BaseJsonService {
static category = 'rating'
static route = buildDockerUrl('stars')
static auth = {
userKey: 'dockerhub_username',
passKey: 'dockerhub_pat',
authorizedOrigins: ['https://hub.docker.com'],
isRequired: false,
}
static openApi = {
'/docker/stars/{user}/{repo}': {
get: {
summary: 'Docker Stars',
parameters: pathParams(
{
name: 'user',
example: '_',
},
{
name: 'repo',
example: 'ubuntu',
},
),
},
},
}
static _cacheLength = 14400
static defaultBadgeData = { label: 'docker stars' }
static render({ stars }) {
return {
message: metric(stars),
color: dockerBlue,
}
}
async fetch({ user, repo }) {
return await fetch(this, {
schema,
url: `https://hub.docker.com/v2/repositories/${getDockerHubUser(
user,
)}/${repo}/`,
httpErrors: { 404: 'repo not found' },
})
}
async handle({ user, repo }) {
const resp = await this.fetch({ user, repo })
return this.constructor.render({ stars: resp.star_count })
}
}