Skip to content

Commit

Permalink
add renderVersionBadge from version and remove the manually added ren…
Browse files Browse the repository at this point in the history
…derer
  • Loading branch information
MherZaqaryan committed Jan 16, 2023
1 parent cf16210 commit 9f6cf86
Showing 1 changed file with 4 additions and 38 deletions.
42 changes: 4 additions & 38 deletions services/polymart/polymart-latest-version.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NotFound } from '../../core/base-service/errors.js'
import { renderVersionBadge } from '../version.js'
import { BasePolymartService, documentation } from './polymart-base.js'

const ignoredVersionPatterns = /^[^0-9]|[0-9]{4}-[0-9]{2}-[0-9]{2}/
export default class PolymartLatestVersion extends BasePolymartService {
static category = 'version'

Expand All @@ -16,8 +15,8 @@ export default class PolymartLatestVersion extends BasePolymartService {
namedParams: {
resourceId: '323',
},
staticPreview: this.render({
version: '1.2.9',
staticPreview: renderVersionBadge({
version: 'v1.2.9',
}),
documentation,
},
Expand All @@ -27,45 +26,12 @@ export default class PolymartLatestVersion extends BasePolymartService {
label: 'polymart',
}

static addv(version) {
version = `${version}`
if (version.startsWith('v') || ignoredVersionPatterns.test(version)) {
return version
} else {
return `v${version}`
}
}

static versionColor(version) {
if (typeof version !== 'string' && typeof version !== 'number') {
throw new Error(`Can't generate a version color for ${version}`)
}
version = `${version}`
let first = version[0]
if (first === 'v') {
first = version[1]
}
if (first === '0' || /alpha|beta|snapshot|dev|pre/i.test(version)) {
return 'orange'
} else {
return 'blue'
}
}

static render({ version, tag, defaultLabel }) {
return {
label: tag ? `${defaultLabel}@${tag}` : undefined,
message: this.addv(version),
color: this.versionColor(version),
}
}

async handle({ resourceId }) {
const { response } = await this.fetch({ resourceId })
if (!response.resource) {
throw new NotFound()
}
return this.constructor.render({
return renderVersionBadge({
version: response.resource.updates.latest.version,
})
}
Expand Down

0 comments on commit 9f6cf86

Please sign in to comment.