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

Improve python version formatting 🐍 affects [pypi piwheels github] #7682

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@fontsource/lato": "^4.5.2",
"@fontsource/lekton": "^4.5.3",
"@renovate/pep440": "^1.0.0",
"@sentry/node": "^6.18.1",
"@shields_io/camp": "^18.1.1",
"badge-maker": "file:badge-maker",
Expand Down
13 changes: 13 additions & 0 deletions services/color-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* including colours based off download count, version number, etc.
*/
import moment from 'moment'
import pep440 from '@renovate/pep440'

function version(version) {
if (typeof version !== 'string' && typeof version !== 'number') {
Expand All @@ -20,6 +21,17 @@ function version(version) {
}
}

function pep440VersionColor(version) {
if (!pep440.valid(version)) {
return 'lightgrey'
}
const parsedVersion = pep440.explain(version)
if (parsedVersion.is_prerelease || parsedVersion.public.startsWith('0.')) {
return 'orange'
}
return 'blue'
}

function floorCount(value, yellow, yellowgreen, green) {
if (value <= 0) {
return 'red'
Expand Down Expand Up @@ -106,6 +118,7 @@ function age(date) {

export {
version,
pep440VersionColor,
downloadCount,
coveragePercentage,
floorCount,
Expand Down
42 changes: 42 additions & 0 deletions services/color-formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
letterScore,
age,
version,
pep440VersionColor,
} from './color-formatters.js'

describe('Color formatters', function () {
Expand Down Expand Up @@ -106,4 +107,45 @@ describe('Color formatters', function () {
"Can't generate a version color for [object Object]"
)
})

test(pep440VersionColor, () => {
forCases([
given('1.0.1'),
given('v2.1.6'),
given('1.0.1+abcd'),
given('1.0'),
given('v1'),
given(9),
given(1.0),
]).expect('blue')

forCases([
given('1.0.1-rc1'),
given('1.0.1rc1'),
given('1.0.0-Beta'),
given('1.0.0Beta'),
given('1.1.0-alpha'),
given('1.1.0alpha'),
given('1.0.1-dev'),
given('1.0.1dev'),
given('2.1.6-b1'),
given('2.1.6b1'),
given('0.1.0'),
given('v0.1.0'),
given('v2.1.6-b1'),
given('0.1.0+abcd'),
given('2.1.6-b1+abcd'),
given('0.0.0'),
given(0.1),
given('0.9'),
]).expect('orange')

forCases([
given('6.0.0-SNAPSHOT'),
given('2.1.6-prerelease'),
given(true),
given(null),
given('cheese'),
]).expect('lightgrey')
})
})
4 changes: 3 additions & 1 deletion services/github/github-pipenv.service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { pep440VersionColor } from '../color-formatters.js'
import { renderVersionBadge } from '../version.js'
import { isLockfile, getDependencyVersion } from '../pipenv-helpers.js'
import { addv } from '../text-formatters.js'
Expand Down Expand Up @@ -80,6 +81,7 @@ class GithubPipenvLockedPythonVersion extends ConditionalGithubAuthV3Service {
version,
tag: branch,
defaultLabel: 'python',
versionFormatter: pep440VersionColor,
})
}

Expand Down Expand Up @@ -147,7 +149,7 @@ class GithubPipenvLockedDependencyVersion extends ConditionalGithubAuthV3Service
return {
label: dependency,
message: version ? addv(version) : ref,
color: 'blue',
color: version ? pep440VersionColor(version) : 'blue',
}
Comment on lines 149 to 153
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use renderVersionBadge here, either with a conditional or ternary operator? For example:

return version ? renderVersionBadge({ version, versionFormatter: pep440VersionColor }) 
               : { label: dependency, message: ref, color: 'blue' }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to extend renderVersionBadge() to also take a custom label. For this badge the label is the name of the locked dependency. Tbh I'm inclined to leave it

}

Expand Down
3 changes: 2 additions & 1 deletion services/piwheels/piwheels-version.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Joi from 'joi'
import { BaseJsonService, InvalidResponse } from '../index.js'
import { renderVersionBadge } from '../version.js'
import { pep440VersionColor } from '../color-formatters.js'

const schema = Joi.object({
releases: Joi.object()
Expand Down Expand Up @@ -46,7 +47,7 @@ export default class PiWheelsVersion extends BaseJsonService {
static defaultBadgeData = { label: 'piwheels' }

static render({ version }) {
return renderVersionBadge({ version })
return renderVersionBadge({ version, versionFormatter: pep440VersionColor })
}

async fetch({ wheel }) {
Expand Down
3 changes: 2 additions & 1 deletion services/pypi/pypi-version.service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { pep440VersionColor } from '../color-formatters.js'
import { renderVersionBadge } from '../version.js'
import PypiBase from './pypi-base.js'

Expand All @@ -19,7 +20,7 @@ export default class PypiVersion extends PypiBase {
static defaultBadgeData = { label: 'pypi' }

static render({ version }) {
return renderVersionBadge({ version })
return renderVersionBadge({ version, versionFormatter: pep440VersionColor })
}

async handle({ egg }) {
Expand Down
9 changes: 7 additions & 2 deletions services/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,16 @@ function rangeStart(v) {
return range.set[0][0].semver.version
}

function renderVersionBadge({ version, tag, defaultLabel }) {
function renderVersionBadge({
version,
tag,
defaultLabel,
versionFormatter = versionColor,
}) {
return {
label: tag ? `${defaultLabel}@${tag}` : undefined,
message: addv(version),
color: versionColor(version),
color: versionFormatter(version),
}
}

Expand Down