yank/unyank endpoints
Diff
chartered-db/src/crates.rs | 24 ++++++++++++++++++++++++
chartered-web/src/main.rs | 10 ++++++++--
chartered-web/src/endpoints/cargo_api/mod.rs | 3 +++
chartered-web/src/endpoints/cargo_api/yank.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+), 2 deletions(-)
@@ -188,6 +188,30 @@
})
.await?
}
pub async fn yank_version(
self: Arc<Self>,
conn: ConnectionPool,
given_version: String,
yank: bool,
) -> Result<()> {
use crate::schema::crate_versions::dsl::{crate_id, crate_versions, version, yanked};
tokio::task::spawn_blocking(move || {
let conn = conn.get()?;
diesel::update(
crate_versions
.filter(crate_id.eq(self.id))
.filter(version.eq(given_version)),
)
.set(yanked.eq(yank))
.execute(&conn)?;
Ok(())
})
.await?
}
}
impl<'a> From<Vec<chartered_types::cargo::CrateDependency<'a>>> for CrateDependencies<'a> {
@@ -45,8 +45,14 @@
)
.route("/crates/:crate/owners", put(hello_world))
.route("/crates/:crate/owners", delete(hello_world))
.route("/crates/:crate/:version/yank", delete(hello_world))
.route("/crates/:crate/:version/unyank", put(hello_world))
.route(
"/crates/:crate/:version/yank",
delete(endpoints::cargo_api::yank)
)
.route(
"/crates/:crate/:version/unyank",
put(endpoints::cargo_api::unyank)
)
.route(
"/crates/:crate/:version/download",
get(endpoints::cargo_api::download)
@@ -13,7 +13,10 @@
mod download;
mod owners;
mod publish;
mod yank;
pub use download::handle as download;
pub use owners::handle_get as get_owners;
pub use publish::handle as publish;
pub use yank::handle_unyank as unyank;
pub use yank::handle_yank as yank;
@@ -1,0 +1,78 @@
use axum::{extract, Json};
use chartered_db::{
crates::Crate,
users::{User, UserCratePermissionValue as Permission},
ConnectionPool,
};
use serde::Serialize;
use std::sync::Arc;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to query database")]
Database(#[from] chartered_db::Error),
#[error("The requested crate does not exist")]
NoCrate,
#[error("You don't have {0:?} permission for this crate")]
NoPermission(Permission),
}
impl Error {
pub fn status_code(&self) -> axum::http::StatusCode {
use axum::http::StatusCode;
match self {
Self::Database(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::NoCrate => StatusCode::NOT_FOUND,
Self::NoPermission(_) => StatusCode::FORBIDDEN,
}
}
}
define_error_response!(Error);
#[derive(Serialize)]
pub struct Response {
ok: bool,
}
pub async fn handle_yank(
extract::Path((_api_key, name, version)): extract::Path<(String, String, String)>,
extract::Extension(db): extract::Extension<ConnectionPool>,
extract::Extension(user): extract::Extension<Arc<User>>,
) -> Result<Json<Response>, Error> {
let crate_ = Crate::find_by_name(db.clone(), name)
.await?
.ok_or(Error::NoCrate)
.map(std::sync::Arc::new)?;
ensure_has_crate_perm!(
db, user, crate_,
Permission::VISIBLE | -> Error::NoCrate,
Permission::YANK_VERSION | -> Error::NoPermission(Permission::YANK_VERSION),
);
crate_.yank_version(db, version, true).await?;
Ok(Json(Response { ok: true }))
}
pub async fn handle_unyank(
extract::Path((_api_key, name, version)): extract::Path<(String, String, String)>,
extract::Extension(db): extract::Extension<ConnectionPool>,
extract::Extension(user): extract::Extension<Arc<User>>,
) -> Result<Json<Response>, Error> {
let crate_ = Crate::find_by_name(db.clone(), name)
.await?
.ok_or(Error::NoCrate)
.map(std::sync::Arc::new)?;
ensure_has_crate_perm!(
db, user, crate_,
Permission::VISIBLE | -> Error::NoCrate,
Permission::YANK_VERSION | -> Error::NoPermission(Permission::YANK_VERSION),
);
crate_.yank_version(db, version, false).await?;
Ok(Json(Response { ok: true }))
}