1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00
shields/services/snapcraft/snapcraft-last-update.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

96 lines
2.6 KiB
JavaScript

import Joi from 'joi'
import { pathParams, queryParam, NotFound } from '../index.js'
import { renderDateBadge } from '../date.js'
import SnapcraftBase, { snapcraftPackageParam } from './snapcraft-base.js'
const queryParamSchema = Joi.object({
arch: Joi.string(),
})
const lastUpdateSchema = Joi.object({
'channel-map': Joi.array()
.items(
Joi.object({
channel: Joi.object({
architecture: Joi.string().required(),
risk: Joi.string().required(),
track: Joi.string().required(),
'released-at': Joi.string().required(),
}),
}).required(),
)
.min(1)
.required(),
}).required()
export default class SnapcraftLastUpdate extends SnapcraftBase {
static category = 'activity'
static route = {
base: 'snapcraft/last-update',
pattern: ':package/:track/:risk',
queryParamSchema,
}
static openApi = {
'/snapcraft/last-update/{package}/{track}/{risk}': {
get: {
summary: 'Snapcraft Last Update',
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: 'last updated' }
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(lastUpdateSchema, { packageName })
// filter results by track, risk and arch
const { channel } = this.constructor.transform(
parsedData,
track,
risk,
arch,
)
return renderDateBadge(channel['released-at'])
}
}