From 17eaba449af476dd7777894ed8e752065c1decca Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Tue, 30 Aug 2022 23:25:23 +0400 Subject: [PATCH 01/14] Add [polymart] badges (#7429) --- services/polymart/polymart-base.js | 39 ++++++++++++ .../polymart/polymart-downloads.service.js | 32 ++++++++++ .../polymart/polymart-downloads.tester.js | 13 ++++ .../polymart-latest-version.service.js | 41 +++++++++++++ .../polymart-latest-version.tester.js | 15 +++++ services/polymart/polymart-raiting.tester.js | 27 ++++++++ services/polymart/polymart-rating.service.js | 61 +++++++++++++++++++ 7 files changed, 228 insertions(+) create mode 100644 services/polymart/polymart-base.js create mode 100644 services/polymart/polymart-downloads.service.js create mode 100644 services/polymart/polymart-downloads.tester.js create mode 100644 services/polymart/polymart-latest-version.service.js create mode 100644 services/polymart/polymart-latest-version.tester.js create mode 100644 services/polymart/polymart-raiting.tester.js create mode 100644 services/polymart/polymart-rating.service.js diff --git a/services/polymart/polymart-base.js b/services/polymart/polymart-base.js new file mode 100644 index 0000000000000..a089c5d32eade --- /dev/null +++ b/services/polymart/polymart-base.js @@ -0,0 +1,39 @@ +import Joi from 'joi' +import { BaseJsonService } from '../index.js' + +const resourceSchema = Joi.object({ + response: Joi.object({ + resource: Joi.object({ + price: Joi.number().required(), + downloads: Joi.string().required(), + reviews: Joi.object({ + count: Joi.number().required(), + stars: Joi.number().required(), + }), + updates: Joi.object({ + latest: Joi.object({ + version: Joi.string().required(), + }), + }), + }), + }), +}).required() + +const documentation = ` +

You can find your resource ID in the url for your resource page.

+

Example: https://polymart.org/resource/polymart-plugin.323 - Here the Resource ID is 323.

` + +class BasePolymartService extends BaseJsonService { + async fetch({ + resourceId, + schema = resourceSchema, + url = `https://api.polymart.org/v1/getResourceInfo/?resource_id=${resourceId}`, + }) { + return this._requestJson({ + schema, + url, + }) + } +} + +export { documentation, BasePolymartService } diff --git a/services/polymart/polymart-downloads.service.js b/services/polymart/polymart-downloads.service.js new file mode 100644 index 0000000000000..634288d45cc29 --- /dev/null +++ b/services/polymart/polymart-downloads.service.js @@ -0,0 +1,32 @@ +import { renderDownloadsBadge } from '../downloads.js' +import { BasePolymartService, documentation } from './polymart-base.js' + +export default class PolymartDownloads extends BasePolymartService { + static category = 'downloads' + + static route = { + base: 'polymart/downloads', + pattern: ':resourceId', + } + + static examples = [ + { + title: 'Polymart Downloads', + namedParams: { + resourceId: '323', + }, + staticPreview: renderDownloadsBadge({ downloads: 655 }), + documentation, + }, + ] + + static defaultBadgeData = { + label: 'downloads', + color: 'green', + } + + async handle({ resourceId }) { + const { response } = await this.fetch({ resourceId }) + return renderDownloadsBadge({ downloads: response.resource.downloads }) + } +} diff --git a/services/polymart/polymart-downloads.tester.js b/services/polymart/polymart-downloads.tester.js new file mode 100644 index 0000000000000..b7c72e16745e9 --- /dev/null +++ b/services/polymart/polymart-downloads.tester.js @@ -0,0 +1,13 @@ +import { isMetric } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Polymart Plugin (id 323)').get('/323.json').expectBadge({ + label: 'downloads', + message: isMetric, +}) + +t.create('Invalid Resource (id 1)').get('/1.json').expectBadge({ + label: 'downloads', + message: 'not found', +}) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js new file mode 100644 index 0000000000000..a53dc93f53fde --- /dev/null +++ b/services/polymart/polymart-latest-version.service.js @@ -0,0 +1,41 @@ +import { BasePolymartService, documentation } from './polymart-base.js' + +export default class PolymartLatestVersion extends BasePolymartService { + static category = 'version' + + static route = { + base: 'polymart/version', + pattern: ':resourceId', + } + + static examples = [ + { + title: 'Polymart Version', + namedParams: { + resourceId: '323', + }, + staticPreview: this.render({ + version: '1.2.9', + }), + documentation, + }, + ] + + static defaultBadgeData = { + label: 'version', + color: 'blue', + } + + static render({ version }) { + return { + message: `${version}`, + } + } + + async handle({ resourceId }) { + const { response } = await this.fetch({ resourceId }) + return this.constructor.render({ + version: response.resource.updates.latest.version, + }) + } +} diff --git a/services/polymart/polymart-latest-version.tester.js b/services/polymart/polymart-latest-version.tester.js new file mode 100644 index 0000000000000..31d2759b34431 --- /dev/null +++ b/services/polymart/polymart-latest-version.tester.js @@ -0,0 +1,15 @@ +import { withRegex } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Polymart Plugin (id 323)') + .get('/323.json') + .expectBadge({ + label: 'version', + message: withRegex(/^(?!not found$)/), + }) + +t.create('Invalid Resource (id 1)').get('/1.json').expectBadge({ + label: 'version', + message: 'not found', +}) diff --git a/services/polymart/polymart-raiting.tester.js b/services/polymart/polymart-raiting.tester.js new file mode 100644 index 0000000000000..9f39100c4c04e --- /dev/null +++ b/services/polymart/polymart-raiting.tester.js @@ -0,0 +1,27 @@ +import { isStarRating, withRegex } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Stars - Polymart Plugin (id 323)') + .get('/stars/323.json') + .expectBadge({ + label: 'rating', + message: isStarRating, + }) + +t.create('Stars - Invalid Resource (id 1)').get('/stars/1.json').expectBadge({ + label: 'rating', + message: 'not found', +}) + +t.create('Rating - Polymart Plugin (id 323)') + .get('/rating/323.json') + .expectBadge({ + label: 'rating', + message: withRegex(/^(\d*\.\d+)(\/5 \()(\d+)(\))$/), + }) + +t.create('Rating - Invalid Resource (id 1)').get('/rating/1.json').expectBadge({ + label: 'rating', + message: 'not found', +}) diff --git a/services/polymart/polymart-rating.service.js b/services/polymart/polymart-rating.service.js new file mode 100644 index 0000000000000..320f3147e9505 --- /dev/null +++ b/services/polymart/polymart-rating.service.js @@ -0,0 +1,61 @@ +import { starRating, metric } from '../text-formatters.js' +import { floorCount } from '../color-formatters.js' +import { BasePolymartService, documentation } from './polymart-base.js' + +export default class PolymartRatings extends BasePolymartService { + static category = 'rating' + + static route = { + base: 'polymart', + pattern: ':format(rating|stars)/:resourceId', + } + + static examples = [ + { + title: 'Polymart Stars', + pattern: 'stars/:resourceId', + namedParams: { + resourceId: '323', + }, + staticPreview: this.render({ + format: 'stars', + total: 14, + average: 5, + }), + documentation, + }, + { + title: 'Polymart Rating', + pattern: 'rating/:resourceId', + namedParams: { + resourceId: '323', + }, + staticPreview: this.render({ total: 14, average: 5 }), + documentation, + }, + ] + + static defaultBadgeData = { + label: 'rating', + } + + static render({ format, total, average }) { + const message = + format === 'stars' + ? starRating(average) + : `${average}/5 (${metric(total)})` + return { + message, + color: floorCount(average, 2, 3, 4), + } + } + + async handle({ format, resourceId }) { + const { response } = await this.fetch({ resourceId }) + return this.constructor.render({ + format, + total: response.resource.reviews.count, + average: response.resource.reviews.stars.toFixed(2), + }) + } +} From a1bcd595c6364bb81fc54f26c1c17da858467bd9 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Wed, 31 Aug 2022 22:30:27 +0400 Subject: [PATCH 02/14] fix misspelling issue --- .../{polymart-rating.service.js => polymart-raiting.service.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename services/polymart/{polymart-rating.service.js => polymart-raiting.service.js} (100%) diff --git a/services/polymart/polymart-rating.service.js b/services/polymart/polymart-raiting.service.js similarity index 100% rename from services/polymart/polymart-rating.service.js rename to services/polymart/polymart-raiting.service.js From f6e5b9f51ec8b750daeb5d060e5244b1adbd0036 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Wed, 31 Aug 2022 22:43:04 +0400 Subject: [PATCH 03/14] add required to all joi objects --- services/polymart/polymart-base.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/polymart/polymart-base.js b/services/polymart/polymart-base.js index a089c5d32eade..52558f3b33241 100644 --- a/services/polymart/polymart-base.js +++ b/services/polymart/polymart-base.js @@ -9,14 +9,14 @@ const resourceSchema = Joi.object({ reviews: Joi.object({ count: Joi.number().required(), stars: Joi.number().required(), - }), + }).required(), updates: Joi.object({ latest: Joi.object({ version: Joi.string().required(), - }), - }), - }), - }), + }).required(), + }).required(), + }).required(), + }).required(), }).required() const documentation = ` From c797476a97dda66fa39be655d64870670677c045 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 21 Nov 2022 22:51:47 +0400 Subject: [PATCH 04/14] fix not found badge return a valid resource issue --- services/polymart/polymart-downloads.tester.js | 2 +- services/polymart/polymart-latest-version.tester.js | 2 +- services/polymart/polymart-raiting.tester.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/polymart/polymart-downloads.tester.js b/services/polymart/polymart-downloads.tester.js index b7c72e16745e9..4bc7f3c356646 100644 --- a/services/polymart/polymart-downloads.tester.js +++ b/services/polymart/polymart-downloads.tester.js @@ -7,7 +7,7 @@ t.create('Polymart Plugin (id 323)').get('/323.json').expectBadge({ message: isMetric, }) -t.create('Invalid Resource (id 1)').get('/1.json').expectBadge({ +t.create('Invalid Resource (id 0)').get('/0.json').expectBadge({ label: 'downloads', message: 'not found', }) diff --git a/services/polymart/polymart-latest-version.tester.js b/services/polymart/polymart-latest-version.tester.js index 31d2759b34431..db11da86a8d32 100644 --- a/services/polymart/polymart-latest-version.tester.js +++ b/services/polymart/polymart-latest-version.tester.js @@ -9,7 +9,7 @@ t.create('Polymart Plugin (id 323)') message: withRegex(/^(?!not found$)/), }) -t.create('Invalid Resource (id 1)').get('/1.json').expectBadge({ +t.create('Invalid Resource (id 0)').get('/0.json').expectBadge({ label: 'version', message: 'not found', }) diff --git a/services/polymart/polymart-raiting.tester.js b/services/polymart/polymart-raiting.tester.js index 9f39100c4c04e..547d0d988f6c3 100644 --- a/services/polymart/polymart-raiting.tester.js +++ b/services/polymart/polymart-raiting.tester.js @@ -9,7 +9,7 @@ t.create('Stars - Polymart Plugin (id 323)') message: isStarRating, }) -t.create('Stars - Invalid Resource (id 1)').get('/stars/1.json').expectBadge({ +t.create('Stars - Invalid Resource (id 0)').get('/stars/0.json').expectBadge({ label: 'rating', message: 'not found', }) @@ -21,7 +21,7 @@ t.create('Rating - Polymart Plugin (id 323)') message: withRegex(/^(\d*\.\d+)(\/5 \()(\d+)(\))$/), }) -t.create('Rating - Invalid Resource (id 1)').get('/rating/1.json').expectBadge({ +t.create('Rating - Invalid Resource (id 0)').get('/rating/0.json').expectBadge({ label: 'rating', message: 'not found', }) From 7e178835182c166dfca395e8e8ce494c4584aef2 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 21 Nov 2022 22:56:31 +0400 Subject: [PATCH 05/14] fix misspelling issue --- .../{polymart-raiting.service.js => polymart-rating.service.js} | 0 .../{polymart-raiting.tester.js => polymart-rating.tester.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename services/polymart/{polymart-raiting.service.js => polymart-rating.service.js} (100%) rename services/polymart/{polymart-raiting.tester.js => polymart-rating.tester.js} (100%) diff --git a/services/polymart/polymart-raiting.service.js b/services/polymart/polymart-rating.service.js similarity index 100% rename from services/polymart/polymart-raiting.service.js rename to services/polymart/polymart-rating.service.js diff --git a/services/polymart/polymart-raiting.tester.js b/services/polymart/polymart-rating.tester.js similarity index 100% rename from services/polymart/polymart-raiting.tester.js rename to services/polymart/polymart-rating.tester.js From 22cd70bc135a3f9b89534a9e004df84ed547cd0f Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Sun, 15 Jan 2023 22:49:42 +0400 Subject: [PATCH 06/14] fix invalid resource issue --- services/polymart/polymart-base.js | 14 +++++++++++++- services/polymart/polymart-downloads.service.js | 4 ++++ .../polymart/polymart-latest-version.service.js | 4 ++++ services/polymart/polymart-rating.service.js | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/services/polymart/polymart-base.js b/services/polymart/polymart-base.js index 52558f3b33241..9eec32824368c 100644 --- a/services/polymart/polymart-base.js +++ b/services/polymart/polymart-base.js @@ -19,6 +19,18 @@ const resourceSchema = Joi.object({ }).required(), }).required() +const notFoundResourceSchema = Joi.object({ + response: Joi.object({ + success: Joi.boolean().required(), + errors: Joi.object().required(), + }).required(), +}) + +const resourceFoundOrNotSchema = Joi.alternatives( + resourceSchema, + notFoundResourceSchema +) + const documentation = `

You can find your resource ID in the url for your resource page.

Example: https://polymart.org/resource/polymart-plugin.323 - Here the Resource ID is 323.

` @@ -26,7 +38,7 @@ const documentation = ` class BasePolymartService extends BaseJsonService { async fetch({ resourceId, - schema = resourceSchema, + schema = resourceFoundOrNotSchema, url = `https://api.polymart.org/v1/getResourceInfo/?resource_id=${resourceId}`, }) { return this._requestJson({ diff --git a/services/polymart/polymart-downloads.service.js b/services/polymart/polymart-downloads.service.js index 634288d45cc29..7944a6b62be20 100644 --- a/services/polymart/polymart-downloads.service.js +++ b/services/polymart/polymart-downloads.service.js @@ -1,3 +1,4 @@ +import { NotFound } from '../../core/base-service/errors.js' import { renderDownloadsBadge } from '../downloads.js' import { BasePolymartService, documentation } from './polymart-base.js' @@ -27,6 +28,9 @@ export default class PolymartDownloads extends BasePolymartService { async handle({ resourceId }) { const { response } = await this.fetch({ resourceId }) + if (!response.resource) { + throw new NotFound() + } return renderDownloadsBadge({ downloads: response.resource.downloads }) } } diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index a53dc93f53fde..393f12940bd43 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -1,3 +1,4 @@ +import { NotFound } from '../../core/base-service/errors.js' import { BasePolymartService, documentation } from './polymart-base.js' export default class PolymartLatestVersion extends BasePolymartService { @@ -34,6 +35,9 @@ export default class PolymartLatestVersion extends BasePolymartService { async handle({ resourceId }) { const { response } = await this.fetch({ resourceId }) + if (!response.resource) { + throw new NotFound() + } return this.constructor.render({ version: response.resource.updates.latest.version, }) diff --git a/services/polymart/polymart-rating.service.js b/services/polymart/polymart-rating.service.js index 320f3147e9505..fa067c5861d60 100644 --- a/services/polymart/polymart-rating.service.js +++ b/services/polymart/polymart-rating.service.js @@ -1,5 +1,6 @@ import { starRating, metric } from '../text-formatters.js' import { floorCount } from '../color-formatters.js' +import { NotFound } from '../../core/base-service/errors.js' import { BasePolymartService, documentation } from './polymart-base.js' export default class PolymartRatings extends BasePolymartService { @@ -52,6 +53,9 @@ export default class PolymartRatings extends BasePolymartService { async handle({ format, resourceId }) { const { response } = await this.fetch({ resourceId }) + if (!response.resource) { + throw new NotFound() + } return this.constructor.render({ format, total: response.resource.reviews.count, From 1b2cc2b82a8bd3b1e58f151a4a75e8f0b97a2048 Mon Sep 17 00:00:00 2001 From: Mher Zaqaryan Date: Mon, 16 Jan 2023 09:05:43 -0800 Subject: [PATCH 07/14] remove default badge color Co-authored-by: chris48s --- services/polymart/polymart-downloads.service.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/polymart/polymart-downloads.service.js b/services/polymart/polymart-downloads.service.js index 7944a6b62be20..5e5757dee030e 100644 --- a/services/polymart/polymart-downloads.service.js +++ b/services/polymart/polymart-downloads.service.js @@ -22,8 +22,7 @@ export default class PolymartDownloads extends BasePolymartService { ] static defaultBadgeData = { - label: 'downloads', - color: 'green', + label: 'downloads' } async handle({ resourceId }) { From 2e9f697f99dd033dc82c8ef7d936a878ee9ed23f Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:07:26 +0400 Subject: [PATCH 08/14] remove version default badge color --- services/polymart/polymart-latest-version.service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index 393f12940bd43..ca26baca48aa1 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -24,7 +24,6 @@ export default class PolymartLatestVersion extends BasePolymartService { static defaultBadgeData = { label: 'version', - color: 'blue', } static render({ version }) { From 11049ac070db98bf8fe08af5b13106ddf5d6c7b5 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:09:46 +0400 Subject: [PATCH 09/14] change version label to the service name --- services/polymart/polymart-latest-version.service.js | 2 +- services/polymart/polymart-latest-version.tester.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index ca26baca48aa1..ae87484640345 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -23,7 +23,7 @@ export default class PolymartLatestVersion extends BasePolymartService { ] static defaultBadgeData = { - label: 'version', + label: 'polymart', } static render({ version }) { diff --git a/services/polymart/polymart-latest-version.tester.js b/services/polymart/polymart-latest-version.tester.js index db11da86a8d32..f5486485104f7 100644 --- a/services/polymart/polymart-latest-version.tester.js +++ b/services/polymart/polymart-latest-version.tester.js @@ -5,11 +5,11 @@ export const t = await createServiceTester() t.create('Polymart Plugin (id 323)') .get('/323.json') .expectBadge({ - label: 'version', + label: 'polymart', message: withRegex(/^(?!not found$)/), }) t.create('Invalid Resource (id 0)').get('/0.json').expectBadge({ - label: 'version', + label: 'polymart', message: 'not found', }) From 058d1a1956a3d0693971e251e0d97bb77e5685e1 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:23:43 +0400 Subject: [PATCH 10/14] add renderVersionBadge() method for version rendering --- .../polymart-latest-version.service.js | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index ae87484640345..e8e9583f92194 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -1,6 +1,7 @@ import { NotFound } from '../../core/base-service/errors.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' @@ -26,9 +27,36 @@ export default class PolymartLatestVersion extends BasePolymartService { label: 'polymart', } - static render({ version }) { + static addv(version) { + version = `${version}` + if (version.startsWith('v') || ignoredVersionPatterns.test(version)) { + return version + } else { + return `v${version}` + } + } + + static version(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 { - message: `${version}`, + label: tag ? `${defaultLabel}@${tag}` : undefined, + message: this.addv(version), + color: this.versionColor(version), } } From 93d516cf46c056d5a8f973f561100ec5a4ccf151 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:46:24 +0400 Subject: [PATCH 11/14] fix method name issue --- services/polymart/polymart-latest-version.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index e8e9583f92194..3a4e8203fbf97 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -36,7 +36,7 @@ export default class PolymartLatestVersion extends BasePolymartService { } } - static version(version) { + static versionColor(version) { if (typeof version !== 'string' && typeof version !== 'number') { throw new Error(`Can't generate a version color for ${version}`) } From a194a3dde5ed0eb1429c587e196b926190e56bda Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:47:30 +0400 Subject: [PATCH 12/14] change version regex --- services/polymart/polymart-latest-version.tester.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/services/polymart/polymart-latest-version.tester.js b/services/polymart/polymart-latest-version.tester.js index f5486485104f7..bff2a8e3c37f6 100644 --- a/services/polymart/polymart-latest-version.tester.js +++ b/services/polymart/polymart-latest-version.tester.js @@ -1,13 +1,11 @@ -import { withRegex } from '../test-validators.js' +import { isVPlusDottedVersionNClauses } from '../test-validators.js' import { createServiceTester } from '../tester.js' export const t = await createServiceTester() -t.create('Polymart Plugin (id 323)') - .get('/323.json') - .expectBadge({ - label: 'polymart', - message: withRegex(/^(?!not found$)/), - }) +t.create('Polymart Plugin (id 323)').get('/323.json').expectBadge({ + label: 'polymart', + message: isVPlusDottedVersionNClauses, +}) t.create('Invalid Resource (id 0)').get('/0.json').expectBadge({ label: 'polymart', From cf16210c3f37d850f41c76d58dfd3654b493a3c6 Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 21:58:32 +0400 Subject: [PATCH 13/14] fix code style issue --- services/polymart/polymart-downloads.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/polymart/polymart-downloads.service.js b/services/polymart/polymart-downloads.service.js index 5e5757dee030e..fe2d4f641ccea 100644 --- a/services/polymart/polymart-downloads.service.js +++ b/services/polymart/polymart-downloads.service.js @@ -22,7 +22,7 @@ export default class PolymartDownloads extends BasePolymartService { ] static defaultBadgeData = { - label: 'downloads' + label: 'downloads', } async handle({ resourceId }) { From 9f6cf861a93e3e704948fb3663fcc1d812e0530b Mon Sep 17 00:00:00 2001 From: MherZaqaryan Date: Mon, 16 Jan 2023 23:38:31 +0400 Subject: [PATCH 14/14] add renderVersionBadge from version and remove the manually added renderer --- .../polymart-latest-version.service.js | 42 ++----------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/services/polymart/polymart-latest-version.service.js b/services/polymart/polymart-latest-version.service.js index 3a4e8203fbf97..aa6083374e049 100644 --- a/services/polymart/polymart-latest-version.service.js +++ b/services/polymart/polymart-latest-version.service.js @@ -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' @@ -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, }, @@ -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, }) }