Files
shields/services/ansible/ansible-role.service.js
Caleb Cartwright e4e7b09009 Add a render helper for downloads badges, run [amo ansible apm chromewebstore clojars conda crates docker dub eclipse gem githubdownloads] (#7163)
* refactor: add render helper for downloads badges

* refactor: use new helper in some download badge classes

* doc renderer function

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-10-26 23:19:20 +00:00

74 lines
1.9 KiB
JavaScript

import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
const ansibleRoleSchema = Joi.object({
download_count: nonNegativeInteger,
name: Joi.string().required(),
summary_fields: Joi.object({
namespace: Joi.object({
name: Joi.string().required(),
}),
}),
}).required()
class AnsibleGalaxyRole extends BaseJsonService {
async fetch({ roleId }) {
const url = `https://galaxy.ansible.com/api/v1/roles/${roleId}/`
return this._requestJson({
url,
schema: ansibleRoleSchema,
})
}
}
class AnsibleGalaxyRoleDownloads extends AnsibleGalaxyRole {
static category = 'downloads'
static route = { base: 'ansible/role/d', pattern: ':roleId' }
static examples = [
{
title: 'Ansible Role',
namedParams: { roleId: '3078' },
staticPreview: renderDownloadsBadge({ downloads: 76 }),
},
]
static defaultBadgeData = { label: 'role downloads' }
async handle({ roleId }) {
const json = await this.fetch({ roleId })
return renderDownloadsBadge({ downloads: json.download_count })
}
}
class AnsibleGalaxyRoleName extends AnsibleGalaxyRole {
static category = 'other'
static route = { base: 'ansible/role', pattern: ':roleId' }
static examples = [
{
title: 'Ansible Role',
namedParams: { roleId: '3078' },
staticPreview: this.render({
name: 'ansible-roles.sublimetext3_packagecontrol',
}),
},
]
static defaultBadgeData = { label: 'role' }
static render({ name }) {
return { message: name, color: 'blue' }
}
async handle({ roleId }) {
const json = await this.fetch({ roleId })
const name = `${json.summary_fields.namespace.name}.${json.name}`
return this.constructor.render({ name })
}
}
export { AnsibleGalaxyRoleDownloads, AnsibleGalaxyRoleName }