From d73a5ebb1ab14b773ebf72beeb5ad18269ada8e2 Mon Sep 17 00:00:00 2001 From: confused_techie Date: Tue, 15 Aug 2023 01:34:55 -0700 Subject: [PATCH] [Pulsar] Add Pulsar Badges for Stargazers & Downloads (#8767) * Added pulsar-edit Downloads & Stargazers * Manage colour similar to how Docker Badges do * Fixed typo in colour * Or it seems no other (not found) tests check colour, likely overrided elsewhere * Remove usage of 'Edit' * errorMessages => httpErrors && downloads => dt --------- Co-authored-by: Caleb Cartwright --- services/pulsar/pulsar-downloads.service.js | 47 ++++++++++++++++++++ services/pulsar/pulsar-downloads.tester.js | 18 ++++++++ services/pulsar/pulsar-helper.js | 7 +++ services/pulsar/pulsar-stargazers.service.js | 47 ++++++++++++++++++++ services/pulsar/pulsar-stargazers.tester.js | 20 +++++++++ 5 files changed, 139 insertions(+) create mode 100644 services/pulsar/pulsar-downloads.service.js create mode 100644 services/pulsar/pulsar-downloads.tester.js create mode 100644 services/pulsar/pulsar-helper.js create mode 100644 services/pulsar/pulsar-stargazers.service.js create mode 100644 services/pulsar/pulsar-stargazers.tester.js diff --git a/services/pulsar/pulsar-downloads.service.js b/services/pulsar/pulsar-downloads.service.js new file mode 100644 index 0000000000000..50d23cc6a1ea6 --- /dev/null +++ b/services/pulsar/pulsar-downloads.service.js @@ -0,0 +1,47 @@ +import Joi from 'joi' +import { BaseJsonService } from '../index.js' +import { metric } from '../text-formatters.js' +import { nonNegativeInteger } from '../validators.js' +import { pulsarPurple } from './pulsar-helper.js' + +const schema = Joi.object({ + downloads: nonNegativeInteger, +}) + +export default class PulsarDownloads extends BaseJsonService { + static category = 'downloads' + + static route = { base: 'pulsar/dt', pattern: ':packageName' } + + static examples = [ + { + title: 'Pulsar Downloads', + namedParams: { packageName: 'hey-pane' }, + staticPreview: this.render({ downloadCount: 1000 }), + }, + ] + + static defaultBadgeData = { label: 'downloads' } + + static render({ downloadCount }) { + return { + label: 'downloads', + message: metric(downloadCount), + color: pulsarPurple, + } + } + + async fetch({ packageName }) { + return this._requestJson({ + schema, + url: `https://api.pulsar-edit.dev/api/packages/${packageName}`, + httpErrors: { 404: 'package not found' }, + }) + } + + async handle({ packageName }) { + const packageData = await this.fetch({ packageName }) + const downloadCount = packageData.downloads + return this.constructor.render({ downloadCount }) + } +} diff --git a/services/pulsar/pulsar-downloads.tester.js b/services/pulsar/pulsar-downloads.tester.js new file mode 100644 index 0000000000000..1758516a12fae --- /dev/null +++ b/services/pulsar/pulsar-downloads.tester.js @@ -0,0 +1,18 @@ +import { isMetric } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +import { pulsarPurple } from './pulsar-helper.js' + +export const t = await createServiceTester() + +t.create('pulsar downloads (valid)') + .get('/hey-pane.json') + .expectBadge({ + label: 'downloads', + message: isMetric, + color: `#${pulsarPurple}`, + }) + +t.create('pulsar downloads (not found)').get('/test-package.json').expectBadge({ + label: 'downloads', + message: 'package not found', +}) diff --git a/services/pulsar/pulsar-helper.js b/services/pulsar/pulsar-helper.js new file mode 100644 index 0000000000000..42e437622cd21 --- /dev/null +++ b/services/pulsar/pulsar-helper.js @@ -0,0 +1,7 @@ +// This is based on the format the Docker badges have taken. +// Seems Tests require `#` before colors, whereas the badges do not. +// So a color variable can be exported for all modules to use as needed. + +const pulsarPurple = '662d91' + +export { pulsarPurple } diff --git a/services/pulsar/pulsar-stargazers.service.js b/services/pulsar/pulsar-stargazers.service.js new file mode 100644 index 0000000000000..33156f274e1de --- /dev/null +++ b/services/pulsar/pulsar-stargazers.service.js @@ -0,0 +1,47 @@ +import Joi from 'joi' +import { BaseJsonService } from '../index.js' +import { metric } from '../text-formatters.js' +import { nonNegativeInteger } from '../validators.js' +import { pulsarPurple } from './pulsar-helper.js' + +const schema = Joi.object({ + stargazers_count: nonNegativeInteger, +}) + +export default class PulsarStargazers extends BaseJsonService { + static category = 'rating' + + static route = { base: 'pulsar/stargazers', pattern: ':packageName' } + + static examples = [ + { + title: 'Pulsar Stargazers', + namedParams: { packageName: 'hey-pane' }, + staticPreview: this.render({ stargazerCount: 1000 }), + }, + ] + + static defaultBadgeData = { label: 'stargazers' } + + static render({ stargazerCount }) { + return { + label: 'stargazers', + message: metric(stargazerCount), + color: pulsarPurple, + } + } + + async fetch({ packageName }) { + return this._requestJson({ + schema, + url: `https://api.pulsar-edit.dev/api/packages/${packageName}`, + httpErrors: { 404: 'package not found' }, + }) + } + + async handle({ packageName }) { + const packageData = await this.fetch({ packageName }) + const stargazerCount = packageData.stargazers_count + return this.constructor.render({ stargazerCount }) + } +} diff --git a/services/pulsar/pulsar-stargazers.tester.js b/services/pulsar/pulsar-stargazers.tester.js new file mode 100644 index 0000000000000..3207c4ebd2184 --- /dev/null +++ b/services/pulsar/pulsar-stargazers.tester.js @@ -0,0 +1,20 @@ +import { isMetric } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +import { pulsarPurple } from './pulsar-helper.js' + +export const t = await createServiceTester() + +t.create('pulsar stargazers (valid)') + .get('/hey-pane.json') + .expectBadge({ + label: 'stargazers', + message: isMetric, + color: `#${pulsarPurple}`, + }) + +t.create('pulsar stargazers (not found)') + .get('/test-package.json') + .expectBadge({ + label: 'stargazers', + message: 'package not found', + })