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

Add [dub] score badge service #9549

Merged
merged 4 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions services/dub/dub-score.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { colorScale } from '../color-formatters.js'

const schema = Joi.object({
score: Joi.number().required(),
})

export default class DubScore extends BaseJsonService {
static category = 'rating'

static route = { base: 'dub/score', pattern: ':packageName' }

static examples = [
{
title: 'DUB SCORE',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: 'DUB SCORE',
title: 'DUB Score',

namedParams: { packageName: 'vibe-d' },
staticPreview: this.render({ score: 4.5 }),
},
]

static defaultBadgeData = { label: 'SCORE' }

static render({ score }) {
return {
label: 'SCORE',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color: colorScale([1, 2, 3, 4, 5])(score),
message: score,
}
}

async fetch({ packageName }) {
const url = `https://code.dlang.org/api/packages/${packageName}/stats`
return this._requestJson({ schema, url })
}

async handle({ packageName }) {
let { score } = await this.fetch({ packageName })
score = score.toFixed(1)
return this.constructor.render({ score })
}
}
26 changes: 26 additions & 0 deletions services/dub/dub-score.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Joi from 'joi'
import { isWithinRange } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

const isScoreColor = Joi.equal(
'red',
'orange',
'yellow',
'yellowgreen',
'green',
'brightgreen',
)

t.create('version (valid)')
.get('/vibe-d.json')
.expectBadge({
label: 'SCORE',
message: isWithinRange(0, 5),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Joi.number() on numeric strings. Instead of a regex, we can just use Joi.number().min(0).max(5) here.

color: isScoreColor,
})

t.create('version (not found)')
.get('/not-a-package.json')
.expectBadge({ label: 'SCORE', message: 'not found' })
18 changes: 18 additions & 0 deletions services/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ const isCurrency = withRegex(
/(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$/,
)

/**
* Returns a Joi schema that validates strings that represent numbers within the range [start, end].
*
* @param {number} start - The lower bound of the range (inclusive)
* @param {number} end - The upper bound of the range (inclusive)
* @returns {Joi.StringSchema} A Joi schema that uses a regular expression to check if the string is a number in the range
* @example
* isWithinRange(0, 5) // 0 => true
* isWithinRange(0, 5) // 2.5 => true
* isWithinRange(0, 5) // 5 => true
* isWithinRange(0, 5) // 5.0 => true
* isWithinRange(0, 5) // -1 => false
* isWithinRange(0, 5) // 5.1 => false
*/
const isWithinRange = (start, end) =>
withRegex(new RegExp(`^([${start}-${end - 1}](.[0-9])?)|(${end}(.0)?)$`))

export {
isSemver,
isVPlusTripleDottedVersion,
Expand Down Expand Up @@ -214,4 +231,5 @@ export {
isOrdinalNumberDaily,
isHumanized,
isCurrency,
isWithinRange,
}
Loading