Skip to content

Commit d099a4f

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 7ef5d56 commit d099a4f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/controllers/krate/search.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Endpoint for searching and discovery functionality
22
33
use diesel::sql_types::{NotNull, Nullable};
4+
use diesel::dsl::*;
45
use diesel_full_text_search::*;
56

67
use crate::controllers::helpers::Paginate;
@@ -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,
@@ -149,6 +154,14 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
149154
);
150155
}
151156

157+
if !include_yanked {
158+
query = query.filter(exists(
159+
versions::table
160+
.filter(versions::crate_id.eq(crates::id))
161+
.filter(versions::yanked.eq(false)),
162+
));
163+
}
164+
152165
if sort == "downloads" {
153166
query = query.then_order_by(crates::downloads.desc())
154167
} else if sort == "recent-downloads" {

src/tests/krate.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,49 @@ fn exact_match_on_queries_with_sort() {
414414
assert_eq!(json.crates[3].name, "other_sort");
415415
}
416416

417+
#[test]
418+
fn index_include_yanked() {
419+
let (app, anon, user) = TestApp::init().with_user();
420+
let user = user.as_model();
421+
422+
app.db(|conn| {
423+
CrateBuilder::new("unyanked", user.id)
424+
.version(VersionBuilder::new("1.0.0"))
425+
.version(VersionBuilder::new("2.0.0"))
426+
.expect_build(conn);
427+
428+
CrateBuilder::new("newest_yanked", user.id)
429+
.version(VersionBuilder::new("1.0.0"))
430+
.version(VersionBuilder::new("2.0.0").yanked(true))
431+
.expect_build(conn);
432+
433+
CrateBuilder::new("oldest_yanked", user.id)
434+
.version(VersionBuilder::new("1.0.0").yanked(true))
435+
.version(VersionBuilder::new("2.0.0"))
436+
.expect_build(conn);
437+
438+
CrateBuilder::new("all_yanked", user.id)
439+
.version(VersionBuilder::new("1.0.0").yanked(true))
440+
.version(VersionBuilder::new("2.0.0").yanked(true))
441+
.expect_build(conn);
442+
});
443+
444+
// Include fully yanked (all versions were yanked) crates
445+
let json = anon.search("include_yanked=yes&sort=alphabetical");
446+
assert_eq!(json.meta.total, 4);
447+
assert_eq!(json.crates[0].name, "all_yanked");
448+
assert_eq!(json.crates[1].name, "newest_yanked");
449+
assert_eq!(json.crates[2].name, "oldest_yanked");
450+
assert_eq!(json.crates[3].name, "unyanked");
451+
452+
// Do not include fully yanked (all versions were yanked) crates
453+
let json = anon.search("include_yanked=no&sort=alphabetical");
454+
assert_eq!(json.meta.total, 3);
455+
assert_eq!(json.crates[0].name, "newest_yanked");
456+
assert_eq!(json.crates[1].name, "oldest_yanked");
457+
assert_eq!(json.crates[2].name, "unyanked");
458+
}
459+
417460
#[test]
418461
fn show() {
419462
let (app, anon, user) = TestApp::init().with_user();

0 commit comments

Comments
 (0)