Skip to content

EncodableCrate: Add max_stable_version field #3163

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

Merged
merged 3 commits into from
Jan 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ impl Crate {
.map(|v| v.to_string())
.unwrap_or_else(|| "0.0.0".to_string());

let max_stable_version = top_versions.highest_stable.as_ref().map(|v| v.to_string());

EncodableCrate {
id: name.clone(),
name: name.clone(),
Expand All @@ -370,6 +372,7 @@ impl Crate {
badges,
max_version,
newest_version,
max_stable_version,
documentation,
homepage,
exact_match,
Expand Down
35 changes: 32 additions & 3 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct NewVersion {
pub struct TopVersions {
/// The "highest" version in terms of semver
pub highest: Option<semver::Version>,
/// The "highest" non-prerelease version
pub highest_stable: Option<semver::Version>,
/// The "newest" version in terms of publishing date
pub newest: Option<semver::Version>,
}
Expand All @@ -61,9 +63,19 @@ impl TopVersions {
T: Clone + IntoIterator<Item = (NaiveDateTime, semver::Version)>,
{
let newest = pairs.clone().into_iter().max().map(|(_, v)| v);
let highest = pairs.into_iter().map(|(_, v)| v).max();
let highest = pairs.clone().into_iter().map(|(_, v)| v).max();

Self { newest, highest }
let highest_stable = pairs
.into_iter()
.map(|(_, v)| v)
.filter(|v| !v.is_prerelease())
.max();

Self {
newest,
highest,
highest_stable,
}
}
}

Expand Down Expand Up @@ -262,6 +274,7 @@ mod tests {
TopVersions::from_date_version_pairs(versions),
TopVersions {
highest: None,
highest_stable: None,
newest: None,
}
);
Expand All @@ -274,23 +287,39 @@ mod tests {
TopVersions::from_date_version_pairs(versions),
TopVersions {
highest: Some(version("1.0.0")),
highest_stable: Some(version("1.0.0")),
newest: Some(version("1.0.0")),
}
);
}

#[test]
fn top_versions_prerelease() {
let versions = vec![(date("2020-12-03T12:34:56"), version("1.0.0-beta.5"))];
assert_eq!(
TopVersions::from_date_version_pairs(versions),
TopVersions {
highest: Some(version("1.0.0-beta.5")),
highest_stable: None,
newest: Some(version("1.0.0-beta.5")),
}
);
}

#[test]
fn top_versions_multiple() {
let versions = vec![
(date("2018-12-03T12:34:56"), version("1.0.0")),
(date("2019-12-03T12:34:56"), version("2.0.0-alpha.1")),
(date("2020-12-03T12:34:56"), version("1.1.0")),
(date("2020-12-31T12:34:56"), version("1.0.4")),
];
assert_eq!(
TopVersions::from_date_version_pairs(versions),
TopVersions {
highest: Some(version("2.0.0-alpha.1")),
newest: Some(version("1.1.0")),
highest_stable: Some(version("1.1.0")),
newest: Some(version("1.0.4")),
}
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/tests/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,27 @@ fn yanked_versions_are_not_considered_for_max_version() {
assert_eq!(json.crates[0].max_version, "1.0.0");
}

#[test]
fn max_stable_version() {
let (app, anon, user) = TestApp::init().with_user();
let user = user.as_model();

app.db(|conn| {
CrateBuilder::new("foo", user.id)
.description("foo")
.version("0.3.0")
.version("1.0.0")
.version(VersionBuilder::new("1.1.0").yanked(true))
.version("2.0.0-beta.1")
.version("0.3.1")
.expect_build(conn);
});

let json = anon.search("q=foo");
assert_eq!(json.meta.total, 1);
assert_eq!(json.crates[0].max_stable_version, Some("1.0.0".to_string()));
}

/* Given two crates, one with downloads less than 90 days ago, the
other with all downloads greater than 90 days ago, check that
the order returned is by recent downloads, descending. Check
Expand Down
2 changes: 2 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub struct EncodableCrate {
// NOTE: Used by shields.io, altering `max_version` requires a PR with shields.io
pub max_version: String,
pub newest_version: String, // Most recently updated version, which may not be max
pub max_stable_version: Option<String>,
pub description: Option<String>,
pub homepage: Option<String>,
pub documentation: Option<String>,
Expand Down Expand Up @@ -510,6 +511,7 @@ mod tests {
recent_downloads: None,
max_version: "".to_string(),
newest_version: "".to_string(),
max_stable_version: None,
description: None,
homepage: None,
documentation: None,
Expand Down