Support latest crate badge (#137)

Co-authored-by: Paolo Barbolini <paolo@paolo565.org>
This commit is contained in:
Rob Ede 2022-09-06 08:55:04 +01:00 committed by GitHub
parent edfdb969c7
commit 5b022c44b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 7 deletions

View file

@ -237,7 +237,7 @@ impl Engine {
} }
} }
pub async fn find_latest_crate_release( pub async fn find_latest_stable_crate_release(
&self, &self,
name: CrateName, name: CrateName,
req: VersionReq, req: VersionReq,

View file

@ -42,6 +42,7 @@ enum Route {
RepoStatus(StatusFormat), RepoStatus(StatusFormat),
CrateRedirect, CrateRedirect,
CrateStatus(StatusFormat), CrateStatus(StatusFormat),
LatestCrateBadge,
} }
#[derive(Clone)] #[derive(Clone)]
@ -75,6 +76,7 @@ impl App {
"/crate/:name/:version", "/crate/:name/:version",
Route::CrateStatus(StatusFormat::Html), Route::CrateStatus(StatusFormat::Html),
); );
router.add("/crate/:name/latest/status.svg", Route::LatestCrateBadge);
router.add( router.add(
"/crate/:name/:version/status.svg", "/crate/:name/:version/status.svg",
Route::CrateStatus(StatusFormat::Svg), Route::CrateStatus(StatusFormat::Svg),
@ -111,6 +113,11 @@ impl App {
.await .await
} }
(&Method::GET, Route::LatestCrateBadge) => {
self.crate_status(req, route_match.params().clone(), logger, StatusFormat::Svg)
.await
}
(&Method::GET, Route::CrateRedirect) => { (&Method::GET, Route::CrateRedirect) => {
self.crate_redirect(req, route_match.params().clone(), logger) self.crate_redirect(req, route_match.params().clone(), logger)
.await .await
@ -249,7 +256,7 @@ impl App {
Ok(crate_name) => { Ok(crate_name) => {
let release_result = engine let release_result = engine
.find_latest_crate_release(crate_name, VersionReq::STAR) .find_latest_stable_crate_release(crate_name, VersionReq::STAR)
.await; .await;
match release_result { match release_result {
@ -301,14 +308,45 @@ impl App {
let server = self.clone(); let server = self.clone();
let name = params.find("name").expect("route param 'name' not found"); let name = params.find("name").expect("route param 'name' not found");
let version = params
.find("version")
.expect("route param 'version' not found");
let version = match params.find("version") {
Some(ver) => ver.to_owned(),
None => {
let crate_name = match name.parse() {
Ok(name) => name,
Err(_) => {
let mut response = views::html::error::render(
"Could not parse crate path",
"Please make sure to provide a valid crate name and version.",
);
*response.status_mut() = StatusCode::BAD_REQUEST;
return Ok(response);
}
};
match server
.engine
.find_latest_stable_crate_release(crate_name, VersionReq::STAR)
.await
{
Ok(Some(latest_rel)) => latest_rel.version.to_string(),
Ok(None) => return Ok(not_found()),
Err(err) => {
error!(logger, "error: {}", err);
let mut response = views::html::error::render(
"Could not fetch crate information",
"Please make sure to provide a valid crate name.",
);
*response.status_mut() = StatusCode::NOT_FOUND;
return Ok(response);
}
}
}
};
let crate_path_result = CratePath::from_parts(name, &version);
let badge_knobs = BadgeKnobs::from_query_string(req.uri().query()); let badge_knobs = BadgeKnobs::from_query_string(req.uri().query());
let crate_path_result = CratePath::from_parts(name, version);
match crate_path_result { match crate_path_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); error!(logger, "error: {}", err);