diff --git a/services/ansible/ansible-quality.service.js b/services/ansible/ansible-quality.service.js new file mode 100644 index 0000000000..98c96498ac --- /dev/null +++ b/services/ansible/ansible-quality.service.js @@ -0,0 +1,70 @@ +'use strict' + +const Joi = require('joi') +const BaseJsonService = require('../base-json') +const { floorCount } = require('../../lib/color-formatters') +const { InvalidResponse } = require('../errors') + +const ansibleContentSchema = Joi.object({ + quality_score: Joi.number() + .allow(null) + .required(), +}).required() + +class AnsibleGalaxyContent extends BaseJsonService { + async fetch({ projectId }) { + const url = `https://galaxy.ansible.com/api/v1/content/${projectId}/` + return this._requestJson({ + url, + schema: ansibleContentSchema, + }) + } +} + +module.exports = class AnsibleGalaxyContentQualityScore extends AnsibleGalaxyContent { + static render({ qualityScore }) { + return { + message: qualityScore, + color: floorCount(qualityScore, 2, 3, 4), + } + } + + async handle({ projectId }) { + const { quality_score: qualityScore } = await this.fetch({ projectId }) + + if (qualityScore === null) { + throw new InvalidResponse({ + prettyMessage: 'no score available', + }) + } + + return this.constructor.render({ qualityScore }) + } + + static get defaultBadgeData() { + return { label: 'quality' } + } + + static get category() { + return 'analysis' + } + + static get route() { + return { + base: 'ansible/quality', + pattern: ':projectId', + } + } + + static get examples() { + return [ + { + title: 'Ansible Quality Score', + namedParams: { + projectId: '432', + }, + staticPreview: this.render({ qualityScore: 4.125 }), + }, + ] + } +} diff --git a/services/ansible/ansible-quality.tester.js b/services/ansible/ansible-quality.tester.js new file mode 100644 index 0000000000..b7563eafc5 --- /dev/null +++ b/services/ansible/ansible-quality.tester.js @@ -0,0 +1,16 @@ +'use strict' + +const Joi = require('joi') +const { nonNegativeInteger } = require('../validators') + +const t = (module.exports = require('../create-service-tester')()) + +t.create('quality score (valid)') + .get('/432.json') + .expectJSONTypes( + Joi.object().keys({ name: 'quality', value: nonNegativeInteger }) + ) + +t.create('quality score (not found)') + .get('/0101.json') + .expectJSON({ name: 'quality', value: 'no score available' }) diff --git a/services/ansible/ansible.service.js b/services/ansible/ansible-role.service.js similarity index 100% rename from services/ansible/ansible.service.js rename to services/ansible/ansible-role.service.js diff --git a/services/ansible/ansible.tester.js b/services/ansible/ansible-role.tester.js similarity index 77% rename from services/ansible/ansible.tester.js rename to services/ansible/ansible-role.tester.js index 4abc539351..cad57ee271 100644 --- a/services/ansible/ansible.tester.js +++ b/services/ansible/ansible-role.tester.js @@ -5,24 +5,25 @@ const ServiceTester = require('../service-tester') const { isMetric } = require('../test-validators') const t = (module.exports = new ServiceTester({ - id: 'ansible', - title: 'Ansible Galaxy', + id: 'AnsibleRole', + title: 'AnsibleRole', + pathPrefix: '/ansible/role', })) t.create('role name (valid)') - .get('/role/14542.json') + .get('/14542.json') .expectJSON({ name: 'role', value: 'openwisp.openwisp2' }) t.create('role name (not found)') - .get('/role/000.json') + .get('/000.json') .expectJSON({ name: 'role', value: 'not found' }) t.create('role downloads (valid)') - .get('/role/d/14542.json') + .get('/d/14542.json') .expectJSONTypes( Joi.object().keys({ name: 'role downloads', value: isMetric }) ) t.create('role downloads (not found)') - .get('/role/d/does-not-exist.json') + .get('/d/does-not-exist.json') .expectJSON({ name: 'role downloads', value: 'not found' })