Skip to content

Commit

Permalink
Use correct latest crates.io version
Browse files Browse the repository at this point in the history
For download count and license, get the correct latest version from the versions array.

Fix badges#9453
  • Loading branch information
nyurik committed Aug 11, 2023
1 parent 47a7209 commit f93d337
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion services/crates/crates-downloads.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ export default class CratesDownloads extends BaseCratesService {
transform({ variant, json }) {
switch (variant) {
case 'dv':
return json.crate ? json.versions[0].downloads : json.version.downloads
let lastVer = json.version
if (json.crate) {
const lastVerNum = json.crate.max_stable_version
? json.crate.max_stable_version
: json.crate.max_version
lastVer =
json.versions.find(ver => ver.num === lastVerNum) ||
json.versions[0]
}
return lastVer.downloads
case 'dr':
return json.crate.recent_downloads || 0
default:
Expand Down
15 changes: 13 additions & 2 deletions services/crates/crates-license.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ export default class CratesLicense extends BaseCratesService {
return { message }
}

static transform({ errors, version, versions }) {
static transform({ errors, version, versions, crate }) {
// 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 InvalidResponse({ prettyMessage: errors[0].detail })
}

const license = version ? version.license : versions[0].license
let license
if (version) {
license = version.license
} else {
const lastVerNum = crate.max_stable_version
? crate.max_stable_version
: crate.max_version
const lastVer =
versions.find(ver => ver.num === lastVerNum) || versions[0]
license = lastVer.license
}

if (!license) {
throw new InvalidResponse({ prettyMessage: 'invalid null license' })
}
Expand Down

0 comments on commit f93d337

Please sign in to comment.