From 4896c4bd29a7553b1038de748248fbed929b1a44 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:39:35 +0300 Subject: [PATCH 1/9] use defaultLabel in renderVersionBadge without tag As we refactor the codebase to use renderVersionBadge. some badges need to show default label regardless of tag existance. This is usefull for cases where the label is dynamic. This change requires fixing test for npm, not sure how it worked before. --- services/npm/npm-base.spec.js | 2 +- services/version.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/npm/npm-base.spec.js b/services/npm/npm-base.spec.js index 4ec6301601b7e..b8a6bec47f007 100644 --- a/services/npm/npm-base.spec.js +++ b/services/npm/npm-base.spec.js @@ -34,7 +34,7 @@ describe('npm', function () { await NpmVersion.invoke(defaultContext, config, { packageName: 'npm' }), ).to.deep.equal({ color: 'orange', - label: undefined, + label: 'npm', message: 'v0.1.0', }) diff --git a/services/version.js b/services/version.js index 7322ceab5c0e7..54d8efd0f5aeb 100644 --- a/services/version.js +++ b/services/version.js @@ -248,7 +248,7 @@ function renderVersionBadge({ versionFormatter = versionColor, }) { return { - label: tag ? `${defaultLabel}@${tag}` : undefined, + label: tag ? `${defaultLabel}@${tag}` : defaultLabel, message: addv(version), color: versionFormatter(version), } From 75a54876a9fc6eba515524b51d9d67c9ed36189f Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:42:42 +0300 Subject: [PATCH 2/9] Refactor AurVersion to use renderVersionBadge part of #2026 --- services/aur/aur.service.js | 5 +++-- services/aur/aur.spec.js | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/services/aur/aur.service.js b/services/aur/aur.service.js index 3eb8e68753db9..ee8381f7245c1 100644 --- a/services/aur/aur.service.js +++ b/services/aur/aur.service.js @@ -4,7 +4,7 @@ import { age as ageColor, } from '../color-formatters.js' import { renderLicenseBadge } from '../licenses.js' -import { addv, metric, formatDate } from '../text-formatters.js' +import { metric, formatDate } from '../text-formatters.js' import { nonNegativeInteger } from '../validators.js' import { BaseJsonService, @@ -12,6 +12,7 @@ import { InvalidResponse, pathParams, } from '../index.js' +import { renderVersionBadge } from '../version.js' const aurSchema = Joi.object({ resultcount: nonNegativeInteger, @@ -170,7 +171,7 @@ class AurVersion extends BaseAurService { static render({ version, outOfDate }) { const color = outOfDate === null ? 'blue' : 'orange' - return { message: addv(version), color } + return renderVersionBadge({ version, versionFormatter: () => color }) } async handle({ packageName }) { diff --git a/services/aur/aur.spec.js b/services/aur/aur.spec.js index 0bdc44bec721b..a707fa7d5f1c2 100644 --- a/services/aur/aur.spec.js +++ b/services/aur/aur.spec.js @@ -6,11 +6,13 @@ describe('AurVersion', function () { given({ version: '1:1.1.42.622-1', outOfDate: 1 }).expect({ color: 'orange', message: 'v1:1.1.42.622-1', + label: undefined, }) given({ version: '7', outOfDate: null }).expect({ color: 'blue', message: 'v7', + label: undefined, }) }) }) From 024f59c9d0ebaf2613c86d670d586d256a247154 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:43:28 +0300 Subject: [PATCH 3/9] Refactor CondaVersion to use renderVersionBadge part of #2026 --- services/conda/conda-version.service.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/services/conda/conda-version.service.js b/services/conda/conda-version.service.js index ab9d57c08577c..fa915f94b7610 100644 --- a/services/conda/conda-version.service.js +++ b/services/conda/conda-version.service.js @@ -1,6 +1,5 @@ import { pathParams } from '../index.js' -import { addv as versionText } from '../text-formatters.js' -import { version as versionColor } from '../color-formatters.js' +import { renderVersionBadge } from '../version.js' import BaseCondaService from './conda-base.js' export default class CondaVersion extends BaseCondaService { @@ -33,20 +32,12 @@ export default class CondaVersion extends BaseCondaService { }, } - static render({ variant, channel, version }) { - return { - label: variant === 'vn' ? channel : `conda | ${channel}`, - message: versionText(version), - color: versionColor(version), - } - } - async handle({ variant, channel, packageName }) { const json = await this.fetch({ channel, packageName }) - return this.constructor.render({ - variant, - channel, + const defaultLabel = variant === 'vn' ? channel : `conda | ${channel}` + return renderVersionBadge({ version: json.latest_version, + defaultLabel, }) } } From 168c0b1ee292dd897d82475de516727ccb630000 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:48:26 +0300 Subject: [PATCH 4/9] Refactor WordpressRequiresVersion to use renderVersionBadge --- services/wordpress/wordpress-platform.service.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/services/wordpress/wordpress-platform.service.js b/services/wordpress/wordpress-platform.service.js index 974409aebe058..2aae1662854b0 100644 --- a/services/wordpress/wordpress-platform.service.js +++ b/services/wordpress/wordpress-platform.service.js @@ -1,5 +1,6 @@ import { NotFound, pathParams } from '../index.js' import { addv } from '../text-formatters.js' +import { renderVersionBadge } from '../version.js' import { version as versionColor } from '../color-formatters.js' import { description, BaseWordpress } from './wordpress-base.js' import { versionColorForWordpressVersion } from './wordpress-version-color.js' @@ -46,13 +47,6 @@ function WordpressRequiresVersion(extensionType) { static defaultBadgeData = { label: 'wordpress' } - static render({ wordpressVersion }) { - return { - message: addv(wordpressVersion), - color: versionColor(wordpressVersion), - } - } - async handle({ slug }) { const { requires: wordpressVersion } = await this.fetch({ extensionType, @@ -65,7 +59,7 @@ function WordpressRequiresVersion(extensionType) { }) } - return this.constructor.render({ wordpressVersion }) + return renderVersionBadge({ version: wordpressVersion }) } } } From be33c65cce9ba113a307426fb81d559a21669867 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:59:02 +0300 Subject: [PATCH 5/9] add postfix option to renderVersionBadge --- services/version.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/version.js b/services/version.js index 54d8efd0f5aeb..8fa84bf71f2ae 100644 --- a/services/version.js +++ b/services/version.js @@ -234,6 +234,7 @@ function rangeStart(v) { * @param {string} options.version - The version number to display on the badge * @param {string} [options.tag] - The tag to display on the badge, such as "alpha" or "beta" * @param {string} [options.defaultLabel] - The default label to display on the badge, such as "npm" or "github" + * @param {string} [options.postfix] - The postfix to display on the message, such as "tested" * @param {Function} [options.versionFormatter=versionColor] - The function to use to format the color of the badge based on the version number * @returns {object} A badge object that has three properties: label, message, and color * @example @@ -245,11 +246,12 @@ function renderVersionBadge({ version, tag, defaultLabel, + postfix, versionFormatter = versionColor, }) { return { label: tag ? `${defaultLabel}@${tag}` : defaultLabel, - message: addv(version), + message: addv(version) + (postfix ? ` ${postfix}` : ''), color: versionFormatter(version), } } From 6a0295132563a6077a44f240ae96d45696ed2e79 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:00:14 +0300 Subject: [PATCH 6/9] add missing tests for renderVersionBadge add defaultLabel without tag test add postfix test add test for all options together --- services/version.spec.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/services/version.spec.js b/services/version.spec.js index d4ecce758d221..66d1118b0b81f 100644 --- a/services/version.spec.js +++ b/services/version.spec.js @@ -150,5 +150,25 @@ describe('Version helpers', function () { message: 'v1.2.3', color: 'blue', }) + given({ version: '1.2.3', defaultLabel: 'npm' }).expect({ + label: 'npm', + message: 'v1.2.3', + color: 'blue', + }) + given({ version: '1.2.3', postfix: 'tested' }).expect({ + label: undefined, + message: 'v1.2.3 tested', + color: 'blue', + }) + given({ + version: '1.2.3', + tag: 'beta', + defaultLabel: 'github', + postfix: 'tested', + }).expect({ + label: 'github@beta', + message: 'v1.2.3 tested', + color: 'blue', + }) }) }) From ec97261b82918432737fc144c62b3477dd908e67 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:22:12 +0300 Subject: [PATCH 7/9] Refactor WordpressPluginTestedVersion to use renderVersionBadge --- .../wordpress/wordpress-platform.service.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/services/wordpress/wordpress-platform.service.js b/services/wordpress/wordpress-platform.service.js index 2aae1662854b0..58f013bc1aeee 100644 --- a/services/wordpress/wordpress-platform.service.js +++ b/services/wordpress/wordpress-platform.service.js @@ -1,5 +1,4 @@ import { NotFound, pathParams } from '../index.js' -import { addv } from '../text-formatters.js' import { renderVersionBadge } from '../version.js' import { version as versionColor } from '../color-formatters.js' import { description, BaseWordpress } from './wordpress-base.js' @@ -87,21 +86,18 @@ class WordpressPluginTestedVersion extends BaseWordpress { static defaultBadgeData = { label: 'wordpress' } - static async render({ testedVersion }) { - // Atypically, the `render()` function of this badge is `async` because it needs to pull - // data from the server. - return { - message: `${addv(testedVersion)} tested`, - color: await versionColorForWordpressVersion(testedVersion), - } - } - async handle({ slug }) { const { tested: testedVersion } = await this.fetch({ extensionType: 'plugin', slug, }) - return this.constructor.render({ testedVersion }) + // Atypically, pulling color data from the server with async operation. + const color = await versionColorForWordpressVersion(testedVersion) + return renderVersionBadge({ + version: testedVersion, + postfix: 'tested', + versionFormatter: () => color, + }) } } From 1ae6c44ad5c06a41eb4e8b80b60d16f348a25d58 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:29:58 +0300 Subject: [PATCH 8/9] add prefix override to renderVersionBadge adds tests for all options with prefix as well used for #2026 but also usefull for usage letting people override v prefix for versions all over the project once #2026 is done as requested for example in #10574 --- services/version.js | 6 +++++- services/version.spec.js | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/services/version.js b/services/version.js index 8fa84bf71f2ae..cdbb90aec3b30 100644 --- a/services/version.js +++ b/services/version.js @@ -234,6 +234,7 @@ function rangeStart(v) { * @param {string} options.version - The version number to display on the badge * @param {string} [options.tag] - The tag to display on the badge, such as "alpha" or "beta" * @param {string} [options.defaultLabel] - The default label to display on the badge, such as "npm" or "github" + * @param {string} [options.prefix] - The prefix to display on the message, such as ">=", "v", overrides the default behavior of using addv * @param {string} [options.postfix] - The postfix to display on the message, such as "tested" * @param {Function} [options.versionFormatter=versionColor] - The function to use to format the color of the badge based on the version number * @returns {object} A badge object that has three properties: label, message, and color @@ -246,12 +247,15 @@ function renderVersionBadge({ version, tag, defaultLabel, + prefix, postfix, versionFormatter = versionColor, }) { return { label: tag ? `${defaultLabel}@${tag}` : defaultLabel, - message: addv(version) + (postfix ? ` ${postfix}` : ''), + message: + (prefix ? `${prefix}${version}` : addv(version)) + + (postfix ? ` ${postfix}` : ''), color: versionFormatter(version), } } diff --git a/services/version.spec.js b/services/version.spec.js index 66d1118b0b81f..251cfc892a6be 100644 --- a/services/version.spec.js +++ b/services/version.spec.js @@ -170,5 +170,49 @@ describe('Version helpers', function () { message: 'v1.2.3 tested', color: 'blue', }) + given({ version: '1.2.3', prefix: '^' }).expect({ + label: undefined, + message: '^1.2.3', + color: 'blue', + }) + given({ + version: '1.2.3', + tag: 'alpha', + defaultLabel: 'npm', + prefix: '^', + }).expect({ + label: 'npm@alpha', + message: '^1.2.3', + color: 'blue', + }) + given({ + version: '1.2.3', + defaultLabel: 'npm', + prefix: '^', + }).expect({ + label: 'npm', + message: '^1.2.3', + color: 'blue', + }) + given({ + version: '1.2.3', + prefix: '^', + postfix: 'tested', + }).expect({ + label: undefined, + message: '^1.2.3 tested', + color: 'blue', + }) + given({ + version: '1.2.3', + tag: 'beta', + defaultLabel: 'github', + prefix: '^', + postfix: 'tested', + }).expect({ + label: 'github@beta', + message: '^1.2.3 tested', + color: 'blue', + }) }) }) From c19f8c128145289a0a99aa34bc3b5efe750fadf3 Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:35:15 +0300 Subject: [PATCH 9/9] Refactor RequiresPHPVersionForType to use renderVersionBadge --- services/wordpress/wordpress-platform.service.js | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/services/wordpress/wordpress-platform.service.js b/services/wordpress/wordpress-platform.service.js index 58f013bc1aeee..4de1087ff68a3 100644 --- a/services/wordpress/wordpress-platform.service.js +++ b/services/wordpress/wordpress-platform.service.js @@ -1,6 +1,5 @@ import { NotFound, pathParams } from '../index.js' import { renderVersionBadge } from '../version.js' -import { version as versionColor } from '../color-formatters.js' import { description, BaseWordpress } from './wordpress-base.js' import { versionColorForWordpressVersion } from './wordpress-version-color.js' @@ -132,14 +131,6 @@ function RequiresPHPVersionForType(extensionType) { static defaultBadgeData = { label: 'php' } - static render({ version }) { - return { - label: 'php', - message: `>=${version}`, - color: versionColor(version), - } - } - async handle({ slug }) { const { requires_php: requiresPhp } = await this.fetch({ extensionType, @@ -152,9 +143,7 @@ function RequiresPHPVersionForType(extensionType) { }) } - return this.constructor.render({ - version: requiresPhp, - }) + return renderVersionBadge({ version: requiresPhp, prefix: '>=' }) } } }