Skip to content

Commit

Permalink
[Pulsar] Add Pulsar Badges for Stargazers & Downloads (#8767)
Browse files Browse the repository at this point in the history
* 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 <calebcartwright@users.noreply.github.com>
  • Loading branch information
confused-Techie and calebcartwright committed Aug 15, 2023
1 parent 96e9e13 commit d73a5eb
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
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/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 })
}
}
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}`,
httpErrors: { 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',
})

0 comments on commit d73a5eb

Please sign in to comment.