-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecate ansible role name and legacy downloads, add new downloads
- Loading branch information
Showing
2 changed files
with
68 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |