-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
namedParams: { packageName: 'vibe-d' }, | ||
staticPreview: this.render({ score: 4.5 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'SCORE' } | ||
|
||
static render({ score }) { | ||
return { | ||
label: 'SCORE', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Labels should be lowercase (x2) https://github.com/badges/shields/blob/master/CONTRIBUTING.md#badge-guidelines |
||
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 }) | ||
} | ||
} |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
color: isScoreColor, | ||
}) | ||
|
||
t.create('version (not found)') | ||
.get('/not-a-package.json') | ||
.expectBadge({ label: 'SCORE', message: 'not found' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.