Skip to content

Commit

Permalink
Refactor - use renderVersionBadge - part 4 [githubrelease githubtag] (#…
Browse files Browse the repository at this point in the history
…10656)

* feat: add forcePrerelease option to renderVersionBadge function

Sometimes API would indicate if a version is pre-release while the version number does not have to be semantically a prerelease like in github-release service.
We don't use a isPrerelease that can also force a non-preleases as we trust here developer semantic over API tagging.

* refactor: GithubRelease to use renderVersionBadge

* refactor: GithubTag use renderVersionBadge

* refactor: change forcePrerelease to isPrerelease
  • Loading branch information
jNullj authored Nov 11, 2024
1 parent 4d203e1 commit 04638ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 35 deletions.
13 changes: 2 additions & 11 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,9 +64,8 @@ class GithubRelease extends GithubAuthV3Service {
latestRelease,
queryParams.display_name,
)
return this.constructor.render({
return renderVersionBadge({
version,
sort: queryParams.sort,
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.isPrerelease] - Whether the version is explicitly marked as a prerelease by upstream API
* @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,
isPrerelease,
}) {
return {
label: tag ? `${defaultLabel}@${tag}` : defaultLabel,
message:
(prefix ? `${prefix}${version}` : addv(version)) +
(postfix ? ` ${postfix}` : ''),
color: versionFormatter(version),
color: versionFormatter(isPrerelease ? 'pre' : version),
}
}

Expand Down

0 comments on commit 04638ab

Please sign in to comment.