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

Refactor - use renderVersionBadge - part 4 [github] #10656

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 3 additions & 12 deletions services/github/github-release.service.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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') {
Expand All @@ -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,
})
}
}
Expand Down
14 changes: 2 additions & 12 deletions services/github/github-tag.service.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
}
}
Expand Down
11 changes: 0 additions & 11 deletions services/github/github-tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion services/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
}
}

Expand Down
Loading