Files
shields/services/docker/docker-cloud-build.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

51 lines
1.4 KiB
JavaScript

import { BaseJsonService, NotFound } from '../index.js'
import { dockerBlue, buildDockerUrl } from './docker-helpers.js'
import { fetchBuild } from './docker-cloud-common-fetch.js'
export default class DockerCloudBuild extends BaseJsonService {
static category = 'build'
static route = buildDockerUrl('cloud/build')
static auth = {
userKey: 'dockerhub_username',
passKey: 'dockerhub_pat',
authorizedOrigins: ['https://hub.docker.com', 'https://cloud.docker.com'],
isRequired: false,
}
static examples = [
{
title: 'Docker Cloud Build Status',
documentation: '<p>For the new Docker Hub (https://cloud.docker.com)</p>',
namedParams: {
user: 'jrottenberg',
repo: 'ffmpeg',
},
staticPreview: this.render({ state: 'Success' }),
},
]
static defaultBadgeData = { label: 'docker build' }
static render({ state }) {
if (state === 'Success') {
return { message: 'passing', color: 'brightgreen' }
}
if (state === 'Failed') {
return { message: 'failing', color: 'red' }
}
return { message: 'building', color: dockerBlue }
}
async handle({ user, repo }) {
const data = await fetchBuild(this, { user, repo })
if (data.objects.length === 0) {
throw new NotFound({
prettyMessage: 'automated builds not set up',
})
}
return this.constructor.render({ state: data.objects[0].state })
}
}