Skip to content

Commit 9235e1d

Browse files
author
Maurício Linhares
committed
Hides crates on index calls unless they have at least one available version
This makes the index call only show crates that have at least one available version. Crates that have all versions yanked will not show up anymore. Fixes #958
1 parent 405b2ad commit 9235e1d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/krate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
564564

565565
let recent_downloads = sql::<Nullable<BigInt>>("SUM(crate_downloads.downloads)");
566566

567+
let not_yanked_versions = sql::<Bool>("crates.id = ANY (SELECT vs.crate_id FROM versions vs WHERE vs.crate_id = crates.id AND vs.yanked IS FALSE)");
568+
567569
let mut query = crates::table
568570
.join(
569571
crate_downloads::table,
@@ -572,6 +574,7 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
572574
crate_downloads::date.gt(date(now - 90.days())),
573575
),
574576
)
577+
.filter(not_yanked_versions.clone())
575578
.group_by(crates::id)
576579
.select((
577580
ALL_COLUMNS,

src/tests/krate.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,7 @@ fn yanked_versions_not_included_in_reverse_dependencies() {
18151815
assert_eq!(deps.dependencies[0].crate_id, "c1");
18161816

18171817
// TODO: have this test call `version.yank()` once the yank method is converted to diesel
1818+
18181819
diesel::update(versions::table.filter(versions::num.eq("2.0.0")))
18191820
.set(versions::yanked.eq(true))
18201821
.execute(&*app.diesel_database.get().unwrap())
@@ -2108,6 +2109,45 @@ fn test_default_sort_recent() {
21082109
assert_eq!(json.crates[1].downloads, 20);
21092110
}
21102111

2112+
/* Given two crates, one with all versions yanked and another
2113+
with a good version only the one that doesn't have all versions
2114+
yanked is returned.
2115+
*/
2116+
#[test]
2117+
fn test_hides_yanked_crate() {
2118+
let (_b, app, middle) = ::app();
2119+
2120+
{
2121+
let conn = app.diesel_database.get().unwrap();
2122+
let user = ::new_user("Oskar").create_or_update(&conn).unwrap();
2123+
2124+
::CrateBuilder::new("green_ball", user.id)
2125+
.description("For fetching")
2126+
.downloads(0)
2127+
.recent_downloads(0)
2128+
.expect_build(&conn);
2129+
2130+
let yanked_crate = ::CrateBuilder::new("fully_yanked", user.id)
2131+
.description("Not here anymore")
2132+
.expect_build(&conn);
2133+
2134+
diesel::update(versions::table.filter(versions::crate_id.eq(yanked_crate.id)))
2135+
.set(versions::yanked.eq(true))
2136+
.execute(&*conn)
2137+
.unwrap();
2138+
}
2139+
2140+
let mut req = ::req(app, Method::Get, "/api/v1/crates");
2141+
let mut response = ok_resp!(middle.call(req.with_query("sort=recent-downloads")));
2142+
let json: CrateList = ::json(&mut response);
2143+
2144+
assert_eq!(json.meta.total, 1);
2145+
2146+
assert_eq!(json.crates[0].name, "green_ball");
2147+
assert_eq!(json.crates[0].recent_downloads, Some(0));
2148+
assert_eq!(json.crates[0].downloads, 0);
2149+
}
2150+
21112151
#[test]
21122152
fn block_blacklisted_documentation_url() {
21132153
let (_b, app, middle) = ::app();

0 commit comments

Comments
 (0)