Skip to content

Commit 55a1ddc

Browse files
committed
Add a include_yanked parameter to /api/v1/crates
Add a URL parameter to the `/api/v1/crates` endpoint that specifies whether or not the endpoint should return crates that have been fully yanked - i.e., where every version has been yanked. If not specified, this defaults to `true` because we have typically always returned all matching crates, whether or not their versions have been yanked. This makes it possible to display only non-yanked crates at various places in the UI, and is especially intended for the user and team pages to display only non-yanked crates.
1 parent 9c3a376 commit 55a1ddc

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/controllers/krate/search.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Endpoint for searching and discovery functionality
22
3+
use diesel::dsl::*;
34
use diesel::sql_types::{NotNull, Nullable};
45
use diesel_full_text_search::*;
56

@@ -43,6 +44,10 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
4344
.map(|s| &**s)
4445
.unwrap_or("recent-downloads");
4546
let mut has_filter = false;
47+
let include_yanked = params
48+
.get("include_yanked")
49+
.map(|s| s == "yes")
50+
.unwrap_or(true);
4651

4752
let selection = (
4853
ALL_COLUMNS,
@@ -150,6 +155,15 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
150155
);
151156
}
152157

158+
if !include_yanked {
159+
has_filter = true;
160+
query = query.filter(exists(
161+
versions::table
162+
.filter(versions::crate_id.eq(crates::id))
163+
.filter(versions::yanked.eq(false)),
164+
));
165+
}
166+
153167
if sort == "downloads" {
154168
query = query.then_order_by(crates::downloads.desc())
155169
} else if sort == "recent-downloads" {

src/tests/krate.rs

+43
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,49 @@ fn loose_search_order() {
454454
}
455455
}
456456

457+
#[test]
458+
fn index_include_yanked() {
459+
let (app, anon, user) = TestApp::init().with_user();
460+
let user = user.as_model();
461+
462+
app.db(|conn| {
463+
CrateBuilder::new("unyanked", user.id)
464+
.version(VersionBuilder::new("1.0.0"))
465+
.version(VersionBuilder::new("2.0.0"))
466+
.expect_build(conn);
467+
468+
CrateBuilder::new("newest_yanked", user.id)
469+
.version(VersionBuilder::new("1.0.0"))
470+
.version(VersionBuilder::new("2.0.0").yanked(true))
471+
.expect_build(conn);
472+
473+
CrateBuilder::new("oldest_yanked", user.id)
474+
.version(VersionBuilder::new("1.0.0").yanked(true))
475+
.version(VersionBuilder::new("2.0.0"))
476+
.expect_build(conn);
477+
478+
CrateBuilder::new("all_yanked", user.id)
479+
.version(VersionBuilder::new("1.0.0").yanked(true))
480+
.version(VersionBuilder::new("2.0.0").yanked(true))
481+
.expect_build(conn);
482+
});
483+
484+
// Include fully yanked (all versions were yanked) crates
485+
let json = anon.search("include_yanked=yes&sort=alphabetical");
486+
assert_eq!(json.meta.total, 4);
487+
assert_eq!(json.crates[0].name, "all_yanked");
488+
assert_eq!(json.crates[1].name, "newest_yanked");
489+
assert_eq!(json.crates[2].name, "oldest_yanked");
490+
assert_eq!(json.crates[3].name, "unyanked");
491+
492+
// Do not include fully yanked (all versions were yanked) crates
493+
let json = anon.search("include_yanked=no&sort=alphabetical");
494+
assert_eq!(json.meta.total, 3);
495+
assert_eq!(json.crates[0].name, "newest_yanked");
496+
assert_eq!(json.crates[1].name, "oldest_yanked");
497+
assert_eq!(json.crates[2].name, "unyanked");
498+
}
499+
457500
#[test]
458501
fn show() {
459502
let (app, anon, user) = TestApp::init().with_user();

0 commit comments

Comments
 (0)