1
0
mirror of https://github.com/badges/shields.git synced 2025-04-20 06:47:51 +03:00
shields/services/pypi/pypi-python-versions.service.js
Samuel FORESTIER d3a7afdec0
[PyPI] Fixes Python versions order by using proper Semver comparison (#6000)
* [PyPI] Fixes Python versions order by using proper Semver comparison

* [PyPI] Adds a proper specification file for Python versions service

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-01-02 18:17:09 +00:00

69 lines
1.7 KiB
JavaScript

'use strict'
const semver = require('semver')
const PypiBase = require('./pypi-base')
const { parseClassifiers } = require('./pypi-helpers')
module.exports = class PypiPythonVersions extends PypiBase {
static category = 'platform-support'
static route = this.buildRoute('pypi/pyversions')
static examples = [
{
title: 'PyPI - Python Version',
pattern: ':packageName',
namedParams: { packageName: 'Django' },
staticPreview: this.render({ versions: ['3.5', '3.6', '3.7'] }),
},
]
static defaultBadgeData = { label: 'python' }
static render({ versions }) {
const versionSet = new Set(versions)
// We only show v2 if eg. v2.4 does not appear.
// See https://github.com/badges/shields/pull/489 for more.
;['2', '3'].forEach(majorVersion => {
if (Array.from(versions).some(v => v.startsWith(`${majorVersion}.`))) {
versionSet.delete(majorVersion)
}
})
if (versionSet.size) {
return {
message: Array.from(versionSet)
.sort((v1, v2) =>
semver.compare(semver.coerce(v1), semver.coerce(v2))
)
.join(' | '),
color: 'blue',
}
} else {
return {
message: 'missing',
color: 'red',
}
}
}
async handle({ egg }) {
const packageData = await this.fetch({ egg })
const versions = parseClassifiers(
packageData,
/^Programming Language :: Python :: ([\d.]+)$/
)
// If no versions are found yet, check "X :: Only" as a fallback.
if (versions.length === 0) {
versions.push(
...parseClassifiers(
packageData,
/^Programming Language :: Python :: (\d+) :: Only$/
)
)
}
return this.constructor.render({ versions })
}
}