From d9bd685f34f3e81b56e46c92def4868a7bac703a Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 8 Aug 2023 19:49:39 -0400 Subject: [PATCH] Use correct latest crates.io version For download count and license, get the correct latest version from the versions array. Fix #9453 --- services/crates/crates-downloads.service.js | 11 ++++++++++- services/crates/crates-license.service.js | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/services/crates/crates-downloads.service.js b/services/crates/crates-downloads.service.js index a504f00d3a1b3..db578880e1939 100644 --- a/services/crates/crates-downloads.service.js +++ b/services/crates/crates-downloads.service.js @@ -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: diff --git a/services/crates/crates-license.service.js b/services/crates/crates-license.service.js index 562f44841cabe..74dc0502af369 100644 --- a/services/crates/crates-license.service.js +++ b/services/crates/crates-license.service.js @@ -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' }) }