Skip to content

Commit edbf691

Browse files
committed
Simplify builds.json to return what the clients actually want
AFAIK our primary consumers are: * [shields.io](https://github.com/badges/shields/blob/bf1bea8b4a106227cd81a9c5ac88e0266cf9c2c2/services/docsrs/docsrs.service.js#L4-L11) * [crates.io](https://github.com/rust-lang/crates.io/blob/609a7f312429a0e3c99714858b7e7655c8fe36e4/app/models/version.js#L136-L143) They both only care: > will going to `https://docs.rs/<crate>/<version>` show docs They emulate this by checking whether the latest build is successful, but this relies on docs.rs also using this as the signal (it currently mostly is for other reasons, but we _should_ show docs in cases where a previous build was successful but a newer rebuild failed, and there were previously bugs around proc-macros). So, remove all the extraneous data they don't depend on and just return a simple boolean showing whether the docs are good or not, the same one we use to determine whether to redirect to show a build failure warning or not: https://github.com/rust-lang/docs.rs/blob/f1a7e46a620d763db485a25efb5e510fd3fe0594/src/web/rustdoc.rs#L476
1 parent f1a7e46 commit edbf691

File tree

1 file changed

+28
-68
lines changed

1 file changed

+28
-68
lines changed

src/web/builds.rs

+28-68
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,26 @@ pub(crate) async fn build_list_json_handler(
9797
}
9898
};
9999

100-
let builds = spawn_blocking({
100+
let rustdoc_status = spawn_blocking({
101101
move || {
102102
let mut conn = pool.get()?;
103-
get_builds(&mut conn, &name, &version)
103+
let row = conn.query_one(
104+
"SELECT releases.rustdoc_status
105+
FROM releases
106+
INNER JOIN crates ON releases.crate_id = crates.id
107+
WHERE crates.name = $1 AND releases.version = $2
108+
",
109+
&[&name, &version],
110+
)?;
111+
Ok(row.get::<_, bool>("rustdoc_status"))
104112
}
105113
})
106114
.await?;
107115

108116
Ok((
109117
Extension(CachePolicy::NoStoreMustRevalidate),
110118
[(ACCESS_CONTROL_ALLOW_ORIGIN, "*")],
111-
Json(builds),
119+
Json(serde_json::json!([{"build_status": rustdoc_status}])),
112120
)
113121
.into_response())
114122
}
@@ -197,81 +205,33 @@ mod tests {
197205

198206
#[test]
199207
fn build_list_json() {
208+
wrapper(|env| {
209+
env.fake_release().name("foo").version("0.1.0").create()?;
210+
211+
let response = env.frontend().get("/crate/foo/0.1.0/builds.json").send()?;
212+
assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config());
213+
let value: serde_json::Value = serde_json::from_str(&response.text()?)?;
214+
215+
assert_eq!(value, serde_json::json!([{"build_status": true}]));
216+
217+
Ok(())
218+
});
219+
}
220+
221+
#[test]
222+
fn build_list_json_failure() {
200223
wrapper(|env| {
201224
env.fake_release()
202225
.name("foo")
203226
.version("0.1.0")
204-
.builds(vec![
205-
FakeBuild::default()
206-
.rustc_version("rustc (blabla 2019-01-01)")
207-
.docsrs_version("docs.rs 1.0.0"),
208-
FakeBuild::default()
209-
.successful(false)
210-
.rustc_version("rustc (blabla 2020-01-01)")
211-
.docsrs_version("docs.rs 2.0.0"),
212-
FakeBuild::default()
213-
.rustc_version("rustc (blabla 2021-01-01)")
214-
.docsrs_version("docs.rs 3.0.0"),
215-
])
227+
.build_result_failed()
216228
.create()?;
217229

218230
let response = env.frontend().get("/crate/foo/0.1.0/builds.json").send()?;
219231
assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config());
220232
let value: serde_json::Value = serde_json::from_str(&response.text()?)?;
221233

222-
assert_eq!(value.pointer("/0/build_status"), Some(&true.into()));
223-
assert_eq!(
224-
value.pointer("/0/docsrs_version"),
225-
Some(&"docs.rs 3.0.0".into())
226-
);
227-
assert_eq!(
228-
value.pointer("/0/rustc_version"),
229-
Some(&"rustc (blabla 2021-01-01)".into())
230-
);
231-
assert!(value.pointer("/0/id").unwrap().is_i64());
232-
assert!(serde_json::from_value::<DateTime<Utc>>(
233-
value.pointer("/0/build_time").unwrap().clone()
234-
)
235-
.is_ok());
236-
237-
assert_eq!(value.pointer("/1/build_status"), Some(&false.into()));
238-
assert_eq!(
239-
value.pointer("/1/docsrs_version"),
240-
Some(&"docs.rs 2.0.0".into())
241-
);
242-
assert_eq!(
243-
value.pointer("/1/rustc_version"),
244-
Some(&"rustc (blabla 2020-01-01)".into())
245-
);
246-
assert!(value.pointer("/1/id").unwrap().is_i64());
247-
assert!(serde_json::from_value::<DateTime<Utc>>(
248-
value.pointer("/1/build_time").unwrap().clone()
249-
)
250-
.is_ok());
251-
252-
assert_eq!(value.pointer("/2/build_status"), Some(&true.into()));
253-
assert_eq!(
254-
value.pointer("/2/docsrs_version"),
255-
Some(&"docs.rs 1.0.0".into())
256-
);
257-
assert_eq!(
258-
value.pointer("/2/rustc_version"),
259-
Some(&"rustc (blabla 2019-01-01)".into())
260-
);
261-
assert!(value.pointer("/2/id").unwrap().is_i64());
262-
assert!(serde_json::from_value::<DateTime<Utc>>(
263-
value.pointer("/2/build_time").unwrap().clone()
264-
)
265-
.is_ok());
266-
267-
assert!(
268-
value.pointer("/1/build_time").unwrap().as_str().unwrap()
269-
< value.pointer("/0/build_time").unwrap().as_str().unwrap()
270-
);
271-
assert!(
272-
value.pointer("/2/build_time").unwrap().as_str().unwrap()
273-
< value.pointer("/1/build_time").unwrap().as_str().unwrap()
274-
);
234+
assert_eq!(value, serde_json::json!([{"build_status": false}]));
275235

276236
Ok(())
277237
});

0 commit comments

Comments
 (0)