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

[Pulsar] Add Pulsar Badges for Stargazers & Downloads #8767

Merged
merged 11 commits into from
Aug 15, 2023
47 changes: 47 additions & 0 deletions services/pulsar/pulsar-downloads.service.js
Original file line number Diff line number Diff line change
@@ -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/downloads', pattern: ':packageName' }
confused-Techie marked this conversation as resolved.
Show resolved Hide resolved

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}`,
errorMessages: { 404: 'package not found' },
confused-Techie marked this conversation as resolved.
Show resolved Hide resolved
})
}

async handle({ packageName }) {
const packageData = await this.fetch({ packageName })
const downloadCount = packageData.downloads
return this.constructor.render({ downloadCount })
}
}
18 changes: 18 additions & 0 deletions services/pulsar/pulsar-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -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',
})
7 changes: 7 additions & 0 deletions services/pulsar/pulsar-helper.js
Original file line number Diff line number Diff line change
@@ -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 }
47 changes: 47 additions & 0 deletions services/pulsar/pulsar-stargazers.service.js
Original file line number Diff line number Diff line change
@@ -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}`,
errorMessages: { 404: 'package not found' },
})
}

async handle({ packageName }) {
const packageData = await this.fetch({ packageName })
const stargazerCount = packageData.stargazers_count
return this.constructor.render({ stargazerCount })
}
}
20 changes: 20 additions & 0 deletions services/pulsar/pulsar-stargazers.tester.js
Original file line number Diff line number Diff line change
@@ -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',
})