1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00
shields/services/snapcraft/snapcraft-version.service.js
chris48s c567f6cde4
Migrate to ESLint 9 (#10762)
* update ESLint related packages

* migrate to flat config format

* Fix prefer-const error

Fixes
'overrideLogoSize' is never reassigned. Use 'const' instead

* remove irrelevant eslint-disable comment

These comments came from a swizzled upstream
component but never did anything in our codebase.

ESLint 9 does not allow disable comments
for rules that are not registered.

* remove irrelevant eslint-disable comments

These were here because in the past we were applying
mocha lint rules to files which contained no tests

ESLint 9 now flags eslint-disable comments
that aren't doing anythings

* remove irrelevant eslint-disable comment

ESLint 9 now flags eslint-disable comments
that aren't doing anything

* there are no .tsx files in our code any more

* include .mjs files in linting and formatting

* update sort-class-members rule for openApi property

and update the handful of files violating it
2024-12-31 13:54:25 +00:00

99 lines
2.6 KiB
JavaScript

import Joi from 'joi'
import { NotFound, pathParams, queryParam } from '../index.js'
import { renderVersionBadge } from '../version.js'
import SnapcraftBase, { snapcraftPackageParam } from './snapcraft-base.js'
const queryParamSchema = Joi.object({
arch: Joi.string(),
})
const versionSchema = Joi.object({
'channel-map': Joi.array()
.items(
Joi.object({
channel: Joi.object({
architecture: Joi.string().required(),
risk: Joi.string().required(),
track: Joi.string().required(),
}),
version: Joi.string().required(),
}).required(),
)
.min(1)
.required(),
}).required()
export default class SnapcraftVersion extends SnapcraftBase {
static category = 'version'
static route = {
base: 'snapcraft/v',
pattern: ':package/:track/:risk',
queryParamSchema,
}
static openApi = {
'/snapcraft/v/{package}/{track}/{risk}': {
get: {
summary: 'Snapcraft Version',
parameters: [
snapcraftPackageParam,
...pathParams(
{ name: 'track', example: 'latest' },
{ name: 'risk', example: 'stable' },
),
queryParam({
name: 'arch',
example: 'amd64',
description:
'Architecture, When not specified, this will default to `amd64`.',
}),
],
},
},
}
static defaultBadgeData = { label: 'snapcraft' }
static render({ version }) {
return renderVersionBadge({ version })
}
static transform(apiData, track, risk, arch) {
const channelMap = apiData['channel-map']
let filteredChannelMap = channelMap.filter(
({ channel }) => channel.architecture === arch,
)
if (filteredChannelMap.length === 0) {
throw new NotFound({ prettyMessage: 'arch not found' })
}
filteredChannelMap = filteredChannelMap.filter(
({ channel }) => channel.track === track,
)
if (filteredChannelMap.length === 0) {
throw new NotFound({ prettyMessage: 'track not found' })
}
filteredChannelMap = filteredChannelMap.filter(
({ channel }) => channel.risk === risk,
)
if (filteredChannelMap.length === 0) {
throw new NotFound({ prettyMessage: 'risk not found' })
}
return filteredChannelMap[0]
}
async handle({ package: packageName, track, risk }, { arch = 'amd64' }) {
const parsedData = await this.fetch(versionSchema, { packageName })
// filter results by track, risk and arch
const { version } = this.constructor.transform(
parsedData,
track,
risk,
arch,
)
return this.constructor.render({ version })
}
}