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

[CratesMSRV] Badge #9871

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions services/crates/crates-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const crateSchema = Joi.object({
Joi.object({
downloads: nonNegativeInteger,
license: Joi.string().required().allow(null),
rust_version: Joi.string().allow(null),
}),
)
.min(1)
Expand All @@ -24,6 +25,7 @@ const versionSchema = Joi.object({
downloads: nonNegativeInteger,
num: Joi.string().required(),
license: Joi.string().required().allow(null),
rust_version: Joi.string().allow(null),
}).required(),
}).required()

Expand Down
74 changes: 74 additions & 0 deletions services/crates/crates-msrv.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { NotFound, pathParams } from '../index.js'
import {
BaseCratesService,
description as cratesIoDescription,
} from './crates-base.js'

const description = `
${cratesIoDescription}

MSRV is a crate's minimum suppported rust version,
the oldest version of Rust supported by the crate.
See the [Cargo Book](https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field)
for more info.
`

export default class CratesMSRV extends BaseCratesService {
static category = 'platform-support'
static route = {
base: 'crates/msrv',
pattern: ':crate/:version?',
}

static openApi = {
'/crates/msrv/{crate}': {
get: {
summary: 'Crates.io MSRV',
description,
parameters: pathParams({
name: 'crate',
example: 'serde',
}),
},
},
'/crates/msrv/{crate}/{version}': {
get: {
summary: 'Crates.io MSRV (version)',
description,
parameters: pathParams(
{
name: 'crate',
example: 'serde',
},
{
name: 'version',
example: '1.0.194',
},
),
},
},
}

static defaultBadgeData = { label: 'msrv', color: 'blue' }

static transform({ errors, version, versions }) {
// crates.io returns a 200 response with an errors object in
// error scenarios, e.g. https://crates.io/api/v1/crates/libc/0.1
if (errors) {
throw new NotFound({ prettyMessage: errors[0].detail })
}
Sky9x marked this conversation as resolved.
Show resolved Hide resolved

const msrv = version ? version.rust_version : versions[0].rust_version
if (!msrv) {
throw new NotFound({ prettyMessage: 'unknown' })
}

return { msrv }
}

async handle({ crate, version }) {
const json = await this.fetch({ crate, version })
const { msrv } = this.constructor.transform(json)
return { message: msrv }
}
}
15 changes: 15 additions & 0 deletions services/crates/crates-msrv.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

// dummy crate i created specifically for this test case
t.create('msrv')
.get('/shields-test-dummy-crate-msrv-3452398210.json')
.expectBadge({ label: 'msrv', message: '1.69' })

t.create('msrv (with version)')
.get('/shields-test-dummy-crate-msrv-3452398210/0.69.0.json')
.expectBadge({ label: 'msrv', message: '1.69' })

t.create('msrv (not found)')
.get('/not-a-real-package.json')
.expectBadge({ label: 'msrv', message: 'not found' })