diff --git a/services/github/github-release.service.js b/services/github/github-release.service.js index b376e91e9194f..2cf66a074f2a3 100644 --- a/services/github/github-release.service.js +++ b/services/github/github-release.service.js @@ -1,7 +1,6 @@ import Joi from 'joi' -import { addv } from '../text-formatters.js' -import { version as versionColor } from '../color-formatters.js' import { redirector, pathParam, queryParam } from '../index.js' +import { renderVersionBadge } from '../version.js' import { GithubAuthV3Service } from './github-auth-service.js' import { fetchLatestRelease, @@ -46,13 +45,6 @@ class GithubRelease extends GithubAuthV3Service { static defaultBadgeData = { label: 'release' } - static render({ version, sort, isPrerelease }) { - let color = 'blue' - color = sort === 'semver' ? versionColor(version) : color - color = isPrerelease ? 'orange' : color - return { message: addv(version), color } - } - static transform(latestRelease, display) { const { name, tag_name: tagName, prerelease: isPrerelease } = latestRelease if (display === 'tag') { @@ -72,10 +64,9 @@ class GithubRelease extends GithubAuthV3Service { latestRelease, queryParams.display_name, ) - return this.constructor.render({ + return renderVersionBadge({ version, - sort: queryParams.sort, - isPrerelease, + forcePrerelease: isPrerelease, }) } } diff --git a/services/github/github-tag.service.js b/services/github/github-tag.service.js index 2c6fa6224e99a..42a9ced1e7f66 100644 --- a/services/github/github-tag.service.js +++ b/services/github/github-tag.service.js @@ -1,9 +1,7 @@ import gql from 'graphql-tag' import Joi from 'joi' import { matcher } from 'matcher' -import { addv } from '../text-formatters.js' -import { version as versionColor } from '../color-formatters.js' -import { latest } from '../version.js' +import { latest, renderVersionBadge } from '../version.js' import { NotFound, redirector, pathParam } from '../index.js' import { GithubAuthV4Service } from './github-auth-service.js' import { @@ -55,13 +53,6 @@ class GithubTag extends GithubAuthV4Service { label: 'tag', } - static render({ version, sort }) { - return { - message: addv(version), - color: sort === 'semver' ? versionColor(version) : 'blue', - } - } - static getLimit({ sort, filter }) { if (!filter && sort === 'date') { return 1 @@ -123,13 +114,12 @@ class GithubTag extends GithubAuthV4Service { const prettyMessage = filter ? 'no matching tags found' : 'no tags found' throw new NotFound({ prettyMessage }) } - return this.constructor.render({ + return renderVersionBadge({ version: this.constructor.getLatestTag({ tags, sort, includePrereleases, }), - sort, }) } } diff --git a/services/github/github-tag.spec.js b/services/github/github-tag.spec.js index 058180cd03067..9645a7f3930e9 100644 --- a/services/github/github-tag.spec.js +++ b/services/github/github-tag.spec.js @@ -43,17 +43,6 @@ describe('GithubTag', function () { }).expect('1.2.0-beta') }) - test(GithubTag.render, () => { - given({ usingSemver: false, version: '1.2.3' }).expect({ - message: 'v1.2.3', - color: 'blue', - }) - given({ usingSemver: true, version: '2.0.0' }).expect({ - message: 'v2.0.0', - color: 'blue', - }) - }) - test(GithubTag.getLimit, () => { given({ sort: 'date', filter: undefined }).expect(1) given({ sort: 'date', filter: '' }).expect(1) diff --git a/services/version.js b/services/version.js index cdbb90aec3b30..a151af6324494 100644 --- a/services/version.js +++ b/services/version.js @@ -237,6 +237,7 @@ function rangeStart(v) { * @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 + * @param {boolean} [options.forcePrerelease] - Whether to force the badge to be considered a prerelease for cases where upstream API indicates a prerelease version but it is not semantically a prerelease * @returns {object} A badge object that has three properties: label, message, and color * @example * renderVersionBadge({version: '1.2.3', tag: 'alpha', defaultLabel: 'npm'}) // returns {label: 'npm@alpha', message: 'v1.2.3', color: 'orange'} because @@ -250,13 +251,14 @@ function renderVersionBadge({ prefix, postfix, versionFormatter = versionColor, + forcePrerelease, }) { return { label: tag ? `${defaultLabel}@${tag}` : defaultLabel, message: (prefix ? `${prefix}${version}` : addv(version)) + (postfix ? ` ${postfix}` : ''), - color: versionFormatter(version), + color: versionFormatter(forcePrerelease ? 'pre' : version), } }