Files
shields/services/docsrs/docsrs.service.js
Nemo157 3dc56134d9 Use the latest build status when checking docs.rs (#7613)
When documentation for a crate is rebuilt the `builds.json` can return
multiple results, ordered latest first, so we must take the first
element of the array rather than the last.

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2022-02-20 01:15:40 +00:00

59 lines
1.3 KiB
JavaScript

import Joi from 'joi'
import { BaseJsonService } from '../index.js'
const schema = Joi.array()
.items(
Joi.object({
build_status: Joi.boolean().required(),
})
)
.min(1)
.required()
export default 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 })
return this.constructor.render({ version, buildStatus })
}
}