[Ansible] Add collection badge (#8578)

* Add ansible collection badge

* Ansible Collection: Remove unneccesary base class

* Ansible Collection: Fix test (label "collection" rather than "role")
This commit is contained in:
Sebastian P
2022-11-01 21:22:41 +01:00
committed by GitHub
parent 81c5c25d59
commit a190bb718f
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
const ansibleCollectionSchema = Joi.object({
name: Joi.string().required(),
namespace: Joi.object({
name: Joi.string().required(),
}),
}).required()
class AnsibleGalaxyCollectionName extends BaseJsonService {
static category = 'other'
static route = { base: 'ansible/collection', pattern: ':collectionId' }
static examples = [
{
title: 'Ansible Collection',
namedParams: { collectionId: '278' },
staticPreview: this.render({
name: 'community.general',
}),
},
]
static defaultBadgeData = { label: 'collection' }
static render({ name }) {
return { message: name, color: 'blue' }
}
async fetch({ collectionId }) {
const url = `https://galaxy.ansible.com/api/v2/collections/${collectionId}/`
return this._requestJson({
url,
schema: ansibleCollectionSchema,
})
}
async handle({ collectionId }) {
const json = await this.fetch({ collectionId })
const name = `${json.namespace.name}.${json.name}`
return this.constructor.render({ name })
}
}
export { AnsibleGalaxyCollectionName }

View File

@@ -0,0 +1,14 @@
import { ServiceTester } from '../tester.js'
export const t = new ServiceTester({
id: 'AnsibleCollection',
title: 'AnsibleCollection',
pathPrefix: '/ansible/collection',
})
t.create('collection name (valid)')
.get('/278.json')
.expectBadge({ label: 'collection', message: 'community.general' })
t.create('collection name (not found)')
.get('/000.json')
.expectBadge({ label: 'collection', message: 'not found' })