Skip to content

Commit

Permalink
deprecate ansible role name and legacy downloads, add new downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Oct 10, 2023
1 parent 4031366 commit b1d35b3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 58 deletions.
108 changes: 59 additions & 49 deletions services/ansible/ansible-role.service.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,83 @@
import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, pathParams } from '../index.js'
import {
BaseJsonService,
deprecatedService,
NotFound,
pathParams,
} 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(),
}),
}),
results: Joi.array()
.items(
Joi.object({
download_count: nonNegativeInteger,
}),
)
.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,
})
}
}
const AnsibleGalaxyRoleName = deprecatedService({
name: 'DeprecatedAnsibleGalaxyRoleName',
category: 'other',
route: {
base: 'ansible/role',
pattern: ':roleId',
},
label: 'role',
dateAdded: new Date('2023-10-10'),
})

class AnsibleGalaxyRoleDownloads extends AnsibleGalaxyRole {
const AnsibleGalaxyRoleLegacyDownloads = deprecatedService({
name: 'DeprecatedAnsibleGalaxyRoleDownloads',
category: 'downloads',
route: {
base: 'ansible/role/d',
pattern: ':roleId',
},
label: 'role downloads',
dateAdded: new Date('2023-10-10'),
})

class AnsibleGalaxyRoleDownloads extends BaseJsonService {
static category = 'downloads'
static route = { base: 'ansible/role/d', pattern: ':roleId' }
static route = { base: 'ansible/role/d', pattern: ':namespace/:name' }

static openApi = {
'/ansible/role/d/{roleId}': {
'/ansible/role/d/{namespace}/{name}': {
get: {
summary: 'Ansible Role',
parameters: pathParams({ name: 'roleId', example: '3078' }),
parameters: pathParams(
{ name: 'namespace', example: 'openwisp' },
{ name: 'name', example: 'openwisp2' },
),
},
},
}

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 openApi = {
'/ansible/role/{roleId}': {
get: {
summary: 'Ansible Galaxy Role Name',
parameters: pathParams({ name: 'roleId', example: '3078' }),
},
},
}

static defaultBadgeData = { label: 'role' }

static render({ name }) {
return { message: name, color: 'blue' }
async fetch({ namespace, name }) {
const url = 'https://galaxy.ansible.com/api/v1/roles/'
return this._requestJson({
url,
schema: ansibleRoleSchema,
options: { searchParams: { namespace, name, limit: 1 } },
})
}

async handle({ roleId }) {
const json = await this.fetch({ roleId })
const name = `${json.summary_fields.namespace.name}.${json.name}`
return this.constructor.render({ name })
async handle({ namespace, name }) {
const json = await this.fetch({ namespace, name })
if (json.results.length === 0) {
throw new NotFound({ prettyMessage: 'not found' })
}
return renderDownloadsBadge({ downloads: json.results[0].download_count })
}
}

export { AnsibleGalaxyRoleDownloads, AnsibleGalaxyRoleName }
export {
AnsibleGalaxyRoleDownloads,
AnsibleGalaxyRoleName,
AnsibleGalaxyRoleLegacyDownloads,
}
18 changes: 9 additions & 9 deletions services/ansible/ansible-role.tester.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { isMetric } from '../test-validators.js'
import { ServiceTester } from '../tester.js'
export const t = new ServiceTester({
id: 'AnsibleRole',
title: 'AnsibleRole',
id: 'AnsibleGalaxyRole',
title: 'AnsibleGalaxyRole',
pathPrefix: '/ansible/role',
})

t.create('role name (valid)')
t.create('role name')
.get('/14542.json')
.expectBadge({ label: 'role', message: 'openwisp.openwisp2' })
.expectBadge({ label: 'role', message: 'no longer available' })

t.create('role name (not found)')
.get('/000.json')
.expectBadge({ label: 'role', message: 'not found' })
t.create('role downloads (deprecated)')
.get('/d/14542.json')
.expectBadge({ label: 'role downloads', message: 'no longer available' })

t.create('role downloads (valid)')
.get('/d/14542.json')
.get('/d/openwisp/openwisp2.json')
.expectBadge({ label: 'role downloads', message: isMetric })

t.create('role downloads (not found)')
.get('/d/does-not-exist.json')
.get('/d/does-not/exist.json')
.expectBadge({ label: 'role downloads', message: 'not found' })

0 comments on commit b1d35b3

Please sign in to comment.