Skip to content

Commit d7f9385

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 97e1474 commit d7f9385

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/krate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,19 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
696696
));
697697
}
698698

699+
if vec![params.get("user_id"), params.get("team_id")]
700+
.iter()
701+
.any(|s| s.is_some())
702+
{
703+
query = query
704+
.left_join(crates_versions.on(
705+
crates::id.eq(crates_versions::crate_id).and(
706+
crates_versions::yanked.eq(false),
707+
),
708+
))
709+
.filter(crates_versions::id.is_null())
710+
}
711+
699712
// The database query returns a tuple within a tuple , with the root
700713
// tuple containing 3 items.
701714
let data = query

src/tests/krate.rs

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

18551855
// TODO: have this test call `version.yank()` once the yank method is converted to diesel
1856+
18561857
diesel::update(versions::table.filter(versions::num.eq("2.0.0")))
18571858
.set(versions::yanked.eq(true))
18581859
.execute(&*app.diesel_database.get().unwrap())
@@ -2146,6 +2147,46 @@ fn test_default_sort_recent() {
21462147
assert_eq!(json.crates[1].downloads, 20);
21472148
}
21482149

2150+
/* Given two crates, one with all versions yanked and another
2151+
with a good version only the one that doesn't have all versions
2152+
yanked is returned.
2153+
*/
2154+
#[test]
2155+
fn test_hides_yanked_crate() {
2156+
let (_b, app, middle) = ::app();
2157+
2158+
{
2159+
let conn = app.diesel_database.get().unwrap();
2160+
let user = ::new_user("Oskar").create_or_update(&conn).unwrap();
2161+
2162+
::CrateBuilder::new("green_ball", user.id)
2163+
.description("For fetching")
2164+
.downloads(0)
2165+
.recent_downloads(0)
2166+
.expect_build(&conn);
2167+
2168+
let yanked_crate = ::CrateBuilder::new("fully_yanked", user.id)
2169+
.description("Not here anymore")
2170+
.expect_build(&conn);
2171+
2172+
diesel::update(versions::table.filter(
2173+
versions::crate_id.eq(yanked_crate.id),
2174+
)).set(versions::yanked.eq(true))
2175+
.execute(&*conn)
2176+
.unwrap();
2177+
}
2178+
2179+
let mut req = ::req(app, Method::Get, "/api/v1/crates");
2180+
let mut response = ok_resp!(middle.call(req.with_query("sort=recent-downloads")));
2181+
let json: CrateList = ::json(&mut response);
2182+
2183+
assert_eq!(json.meta.total, 1);
2184+
2185+
assert_eq!(json.crates[0].name, "green_ball");
2186+
assert_eq!(json.crates[0].recent_downloads, Some(0));
2187+
assert_eq!(json.crates[0].downloads, 0);
2188+
}
2189+
21492190
#[test]
21502191
fn block_blacklisted_documentation_url() {
21512192
let (_b, app, middle) = ::app();

0 commit comments

Comments
 (0)