diff --git a/services/ansible/ansible-collection.service.js b/services/ansible/ansible-collection.service.js new file mode 100644 index 0000000000..ae7cb07062 --- /dev/null +++ b/services/ansible/ansible-collection.service.js @@ -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 } diff --git a/services/ansible/ansible-collection.tester.js b/services/ansible/ansible-collection.tester.js new file mode 100644 index 0000000000..611702c98e --- /dev/null +++ b/services/ansible/ansible-collection.tester.js @@ -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' })