Add [Ansible] quality score badge (#2620)
Adds quality score badge from Ansible, closes #2602
This commit is contained in:
committed by
Paul Melnikow
parent
fd41131579
commit
7face4995f
70
services/ansible/ansible-quality.service.js
Normal file
70
services/ansible/ansible-quality.service.js
Normal file
@@ -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 }),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
16
services/ansible/ansible-quality.tester.js
Normal file
16
services/ansible/ansible-quality.tester.js
Normal file
@@ -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' })
|
||||
@@ -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' })
|
||||
Reference in New Issue
Block a user