🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2021-09-13 0:42:45.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2021-09-13 0:42:45.0 +01:00:00
commit
15d6571ea9f6506d10b1639b8b9960e9f8641c9d [patch]
tree
7f9387a276c59a7a036aa2e88028329f6bcd8e4c
parent
061026e1155929b072b4f5b1aee748fa1cd565db
download
15d6571ea9f6506d10b1639b8b9960e9f8641c9d.tar.gz

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(-)

diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs
index 49fb933..aa29fe9 100644
--- a/chartered-db/src/crates.rs
+++ a/chartered-db/src/crates.rs
@@ -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> {
diff --git a/chartered-web/src/main.rs b/chartered-web/src/main.rs
index 0dad29b..2c5145e 100644
--- a/chartered-web/src/main.rs
+++ a/chartered-web/src/main.rs
@@ -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)
diff --git a/chartered-web/src/endpoints/cargo_api/mod.rs b/chartered-web/src/endpoints/cargo_api/mod.rs
index d903363..425ae74 100644
--- a/chartered-web/src/endpoints/cargo_api/mod.rs
+++ a/chartered-web/src/endpoints/cargo_api/mod.rs
@@ -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;
diff --git a/chartered-web/src/endpoints/cargo_api/yank.rs b/chartered-web/src/endpoints/cargo_api/yank.rs
new file mode 100644
index 0000000..0a338e7 100644
--- /dev/null
+++ a/chartered-web/src/endpoints/cargo_api/yank.rs
@@ -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 }))
}