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 8, 2023
1 parent 7d966ab commit 35604cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 8 additions & 1 deletion services/crates/crates-downloads.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ 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;
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]
} else {
lastVer = json.version
}
return lastVer.downloads
case 'dr':
return json.crate.recent_downloads || 0
default:
Expand Down
12 changes: 10 additions & 2 deletions services/crates/crates-license.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ 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 35604cc

Please sign in to comment.