Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: global version category prefix query param [amo archlinux aur bower cdnjs chromewebstore cocoapods conan conda cookbook cpan cran crates ctan curseforge debian dockerVersion dub eclipsemarketplace elmpackage f-droid factorio fedora feedz flathub galaxytoolshed gem gitea] #10685

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2a41f74
feat: add global category-specific query parameters
jNullj Nov 20, 2024
a135b23
feat: add global version category query param versionPrefix
jNullj Nov 20, 2024
f140fa9
feat: add versionPrefix to amo-version
jNullj Nov 20, 2024
750a94d
feat: add versionPrefix to archlinux
jNullj Nov 20, 2024
99a5fdf
feat: add versionPrefix support to AurVersion
jNullj Nov 20, 2024
ae0d16b
feat: add versionPrefix support to BowerVersion
jNullj Nov 20, 2024
5030593
feat: add versionPrefix support to Cdnjs
jNullj Nov 20, 2024
c21fa38
feat: add versionPrefix support to ChromeWebStoreVersion
jNullj Nov 20, 2024
c72db20
feat: add versionPrefix support to CocoapodsVersion
jNullj Nov 20, 2024
152603e
feat: add versionPrefix support to ConanVersion
jNullj Nov 20, 2024
b58b2e8
feat: add versionPrefix support to CondaVersion
jNullj Nov 20, 2024
e81ba37
feat: add versionPrefix support to Cookbook
jNullj Nov 20, 2024
6d22815
feat: add versionPrefix support to CpanVersion
jNullj Nov 20, 2024
454d5b7
feat: add versionPrefix support to cran
jNullj Nov 20, 2024
f3b7898
feat: add versionPrefix support to CratesVersion
jNullj Nov 20, 2024
c462111
feat: add versionPrefix support to CtanVersion
jNullj Nov 20, 2024
cdbbc49
feat: add versionPrefix support to CurseForgeVersion
jNullj Nov 20, 2024
9222d2e
feat: add versionPrefix support to Debian
jNullj Nov 20, 2024
4048737
feat: support versionPrefix in DockerVersion
jNullj Nov 20, 2024
8c0f256
feat: add versionPrefix support to DubVersion
jNullj Nov 20, 2024
3890885
feat: support versionPrefix in EclipseMarketplaceVersion
jNullj Nov 20, 2024
f080e80
feat: add versionPrefix support to ElmPackage
jNullj Nov 20, 2024
e6b0868
feat: add versionPrefix support to FDroid
jNullj Nov 20, 2024
e9fcd53
feat: add versionPrefix support to FactorioModPortal
jNullj Nov 20, 2024
f543c57
feat: add versionPrefix support to Fedora
jNullj Nov 20, 2024
5de7549
feat: add versionPrefix support to FeedzVersionService
jNullj Nov 20, 2024
a2eaff0
feat: add versionPrefix support to Flathub
jNullj Nov 20, 2024
fed7172
feat: add versionPrefix support to GalaxyToolshedVersion
jNullj Nov 20, 2024
7386171
feat: add versionPrefix support to GemVersion
jNullj Nov 20, 2024
355586a
feat: add versionPrefix support to GiteaRelease
jNullj Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion core/base-service/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Deprecated,
} from './errors.js'
import { fetch } from './got.js'
import { getEnum } from './openapi.js'
import { getEnum, categoryGlobalQueryParams } from './openapi.js'
import {
makeFullUrl,
assertValidRoute,
Expand Down Expand Up @@ -203,6 +203,25 @@ class BaseService {
}
}

static addGlobalCategoryQueryParams() {
if (!categoryGlobalQueryParams[this.category.toLowerCase()]) {
return
}
for (const path of Object.values(this.openApi)) {
path?.get?.parameters.push(
...categoryGlobalQueryParams[this.category.toLowerCase()].openApiParams,
)
}
if (this.route.queryParamSchema) {
this.route.queryParamSchema = this.route.queryParamSchema.concat(
categoryGlobalQueryParams[this.category.toLowerCase()].joiSchema,
)
} else {
this.route.queryParamSchema =
categoryGlobalQueryParams[this.category.toLowerCase()].joiSchema
}
}

static getDefinition() {
const { category, name, isDeprecated, openApi } = this
const { base, format, pattern } = this.route
Expand Down
2 changes: 2 additions & 0 deletions core/base-service/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ async function loadServiceClasses(servicePaths) {
serviceClass.serviceFamily = servicePath
.replace(serviceDir, '')
.split(path.sep)[1]
// add global category specific query params
serviceClass.addGlobalCategoryQueryParams()
serviceClass.validateDefinition()
return serviceClasses.push(serviceClass)
}
Expand Down
47 changes: 42 additions & 5 deletions core/base-service/openapi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Joi from 'joi'

/**
* Functions for publishing the shields.io URL schema as an OpenAPI Document
*
Expand All @@ -17,6 +19,21 @@ const globalParamRefs = [
{ $ref: '#/components/parameters/link' },
]

const categoryGlobalQueryParams = {
version: {
openApiParams: queryParams({
name: 'versionPrefix',
example: 'v',
schema: { type: 'string' },
description:
'Overide version prefix. Can be used to remove default v prefix or replace it.',
}),
joiSchema: Joi.object({
versionPrefix: Joi.string(),
}).required(),
},
}

function getCodeSamples(altText) {
return [
{
Expand Down Expand Up @@ -72,18 +89,37 @@ function addGlobalProperties(endpoints) {
return paths
}

function addCategoryProperties(endpoints, category) {
if (!categoryGlobalQueryParams[category.name.toLowerCase()]) {
return endpoints
}
const paths = {}
for (const key of Object.keys(endpoints)) {
paths[key] = endpoints[key]
const additionalParams =
categoryGlobalQueryParams[category.name.toLowerCase()]?.openApiParams ||
[]
paths[key].get.parameters = [
...paths[key].get.parameters,
...additionalParams,
]
paths[key].get['x-code-samples'] = getCodeSamples(paths[key].get.summary)
}
return paths
}

function sortPaths(obj) {
const entries = Object.entries(obj)
entries.sort((a, b) => a[1].get.summary.localeCompare(b[1].get.summary))
return Object.fromEntries(entries)
}

function services2openapi(services, sort) {
function services2openapi(services, category, sort) {
const paths = {}
for (const service of services) {
for (const [key, value] of Object.entries(
addGlobalProperties(service.openApi),
)) {
let servicePaths = addCategoryProperties(service.openApi, category)
servicePaths = addGlobalProperties(servicePaths)
for (const [key, value] of Object.entries(servicePaths)) {
if (key in paths) {
throw new Error(`Conflicting route: ${key}`)
}
Expand Down Expand Up @@ -214,7 +250,7 @@ function category2openapi({ category, services, sort = false }) {
},
},
},
paths: services2openapi(services, sort),
paths: services2openapi(services, category, sort),
}

return spec
Expand Down Expand Up @@ -355,4 +391,5 @@ export {
pathParams,
queryParam,
queryParams,
categoryGlobalQueryParams,
}
7 changes: 5 additions & 2 deletions services/amo/amo-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export default class AmoVersion extends BaseAmoService {
},
}

async handle({ addonId }) {
async handle({ addonId }, { versionPrefix }) {
const data = await this.fetch({ addonId })
return renderVersionBadge({ version: data.current_version.version })
return renderVersionBadge({
version: data.current_version.version,
prefix: versionPrefix,
})
}
}
4 changes: 2 additions & 2 deletions services/archlinux/archlinux.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ArchLinux extends BaseJsonService {

static defaultBadgeData = { label: 'arch linux' }

async handle({ repository, architecture, packageName }) {
async handle({ repository, architecture, packageName }, { versionPrefix }) {
const data = await this._requestJson({
schema,
url: `https://www.archlinux.org/packages/${encodeURIComponent(
Expand All @@ -46,6 +46,6 @@ export default class ArchLinux extends BaseJsonService {
packageName,
)}/json/`,
})
return renderVersionBadge({ version: data.pkgver })
return renderVersionBadge({ version: data.pkgver, prefix: versionPrefix })
}
}
11 changes: 8 additions & 3 deletions services/aur/aur.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,23 @@ class AurVersion extends BaseAurService {

static _cacheLength = 3600

static render({ version, outOfDate }) {
static render({ version, outOfDate, versionPrefix }) {
const color = outOfDate === null ? 'blue' : 'orange'
return renderVersionBadge({ version, versionFormatter: () => color })
return renderVersionBadge({
version,
versionFormatter: () => color,
prefix: versionPrefix,
})
}

async handle({ packageName }) {
async handle({ packageName }, { versionPrefix }) {
const {
results: [{ Version: version, OutOfDate: outOfDate }],
} = await this.fetch({ packageName })
return this.constructor.render({
version,
outOfDate,
versionPrefix,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions services/bower/bower-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export default class BowerVersion extends BaseBowerService {
return version
}

async handle({ packageName }) {
async handle({ packageName }, { versionPrefix }) {
const data = await this.fetch({ packageName })
const version = this.constructor.transform(data)

return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
8 changes: 2 additions & 6 deletions services/cdnjs/cdnjs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export default class Cdnjs extends BaseJsonService {

static defaultBadgeData = { label: 'cdnjs' }

static render({ version }) {
return renderVersionBadge({ version })
}

async fetch({ library }) {
const url = `https://api.cdnjs.com/libraries/${library}?fields=version`
return this._requestJson({
Expand All @@ -37,7 +33,7 @@ export default class Cdnjs extends BaseJsonService {
})
}

async handle({ library }) {
async handle({ library }, { versionPrefix }) {
const json = await this.fetch({ library })

if (Object.keys(json).length === 0) {
Expand All @@ -46,6 +42,6 @@ export default class Cdnjs extends BaseJsonService {
throw new NotFound()
}

return this.constructor.render({ version: json.version })
return renderVersionBadge({ version: json.version, prefix: versionPrefix })
}
}
4 changes: 2 additions & 2 deletions services/chrome-web-store/chrome-web-store-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export default class ChromeWebStoreVersion extends BaseChromeWebStoreService {

static defaultBadgeData = { label: 'chrome web store' }

async handle({ storeId }) {
async handle({ storeId }, { versionPrefix }) {
const chromeWebStore = await this.fetch({ storeId })
const version = chromeWebStore.version()
if (version == null) {
throw new NotFound({ prettyMessage: 'not found' })
}
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
4 changes: 2 additions & 2 deletions services/cocoapods/cocoapods-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class CocoapodsVersion extends BaseCocoaPodsService {

static defaultBadgeData = { label: 'pod' }

async handle({ spec }) {
async handle({ spec }, { versionPrefix }) {
const { version } = await this.fetch({ spec })
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
4 changes: 2 additions & 2 deletions services/conan/conan-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ConanVersion extends ConditionalGithubAuthV3Service {

static defaultBadgeData = { label: 'conan' }

async handle({ packageName }) {
async handle({ packageName }, { versionPrefix }) {
const configContent = await fetchRepoContent(this, {
user: 'conan-io',
repo: 'conan-center-index',
Expand All @@ -34,6 +34,6 @@ export default class ConanVersion extends ConditionalGithubAuthV3Service {

const version = parseLatestVersionFromConfig(configContent)

return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
3 changes: 2 additions & 1 deletion services/conda/conda-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ export default class CondaVersion extends BaseCondaService {
},
}

async handle({ variant, channel, packageName }) {
async handle({ variant, channel, packageName }, { versionPrefix }) {
const json = await this.fetch({ channel, packageName })
const defaultLabel = variant === 'vn' ? channel : `conda | ${channel}`
return renderVersionBadge({
version: json.latest_version,
defaultLabel,
prefix: versionPrefix,
})
}
}
4 changes: 2 additions & 2 deletions services/cookbook/cookbook.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default class Cookbook extends BaseJsonService {
return this._requestJson({ schema, url })
}

async handle({ cookbook }) {
async handle({ cookbook }, { versionPrefix }) {
const { version } = await this.fetch({ cookbook })
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
4 changes: 2 additions & 2 deletions services/cpan/cpan-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default class CpanVersion extends BaseCpanService {
},
}

async handle({ packageName }) {
async handle({ packageName }, { versionPrefix }) {
const { version } = await this.fetch({ packageName })
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
8 changes: 2 additions & 6 deletions services/cran/cran.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ class CranVersion extends BaseCranService {
},
}

static render({ version }) {
return renderVersionBadge({ version })
}

async handle({ packageName }) {
async handle({ packageName }, { versionPrefix }) {
const data = await this.fetch({ packageName })
return this.constructor.render({ version: data.Version })
return renderVersionBadge({ version: data.Version, prefix: versionPrefix })
}
}

Expand Down
4 changes: 2 additions & 2 deletions services/crates/crates-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default class CratesVersion extends BaseCratesService {
},
}

async handle({ crate }) {
async handle({ crate }, { versionPrefix }) {
const json = await this.fetch({ crate })
const version = this.constructor.getLatestVersion(json)
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
9 changes: 3 additions & 6 deletions services/ctan/ctan.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,18 @@ class CtanVersion extends BaseCtanService {
},
}

static render({ version }) {
return renderVersionBadge({ version })
}

async handle({ library }) {
async handle({ library }, { versionPrefix }) {
const json = await this.fetch({ library })
const version = json.version.number
if (version !== '') {
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
} else {
const date = json.version.date
if (date !== '') {
return renderVersionBadge({
version: date,
versionFormatter: color => 'blue',
prefix: versionPrefix,
})
} else {
return new InvalidResponse({
Expand Down
4 changes: 2 additions & 2 deletions services/curseforge/curseforge-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default class CurseForgeVersion extends BaseCurseForgeService {

static defaultBadgeData = { label: 'version' }

async handle({ projectId }) {
async handle({ projectId }, { versionPrefix }) {
const { version } = await this.fetchMod({ projectId })
return renderVersionBadge({ version })
return renderVersionBadge({ version, prefix: versionPrefix })
}
}
10 changes: 8 additions & 2 deletions services/debian/debian.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export default class Debian extends BaseJsonService {

static defaultBadgeData = { label: 'debian' }

async handle({ packageName, distribution = defaultDistribution }) {
async handle(
{ packageName, distribution = defaultDistribution },
{ versionPrefix },
) {
const data = await this._requestJson({
schema,
url: 'https://api.ftp-master.debian.org/madison',
Expand All @@ -84,6 +87,9 @@ export default class Debian extends BaseJsonService {
throw new NotFound()
}
const versions = Object.keys(packageData[distKeys[0]])
return renderVersionBadge({ version: latest(versions) })
return renderVersionBadge({
version: latest(versions),
prefix: versionPrefix,
})
}
}
Loading
Loading