Files
shields/services/docsrs/docsrs.service.js
Shaurya 6999e7fad7 Docs.rs badge, run [Docsrs] (#6098)
* Add docs.rs badge

* Do suggested changes

* Fix incorrect tests

* Make some small changes

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-01-31 22:40:23 +00:00

63 lines
1.4 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { BaseJsonService } = require('..')
const schema = Joi.array()
.items(
Joi.object({
build_status: Joi.boolean().required(),
})
)
.min(1)
.required()
module.exports = class DocsRs extends BaseJsonService {
static category = 'build'
static route = { base: 'docsrs', pattern: ':crate/:version?' }
static examples = [
{
title: 'docs.rs',
namedParams: { crate: 'regex', version: 'latest' },
staticPreview: this.render({ version: 'latest', buildStatus: true }),
keywords: ['rust'],
},
]
static defaultBadgeData = { label: 'docs' }
static render({ buildStatus, version }) {
let label = `docs@${version}`
if (version === 'latest') {
label = 'docs'
}
if (buildStatus) {
return {
label,
message: 'passing',
color: 'success',
}
} else {
return {
label,
message: 'failing',
color: 'critical',
}
}
}
async fetch({ crate, version }) {
return await this._requestJson({
schema,
url: `https://docs.rs/crate/${crate}/${version}/builds.json`,
})
}
async handle({ crate, version = 'latest' }) {
const { build_status: buildStatus } = (
await this.fetch({ crate, version })
).pop()
return this.constructor.render({ version, buildStatus })
}
}