|
| 1 | +//! Endpoint for triggering a docs.rs rebuild |
| 2 | +
|
| 3 | +use super::update::authenticate; |
| 4 | +use super::CrateVersionPath; |
| 5 | +use crate::app::AppState; |
| 6 | +use crate::controllers::helpers::ok_true; |
| 7 | +use crate::rate_limiter::LimitedAction; |
| 8 | +use crate::util::errors::{forbidden, AppResult}; |
| 9 | +use axum::response::Response; |
| 10 | +use http::request::Parts; |
| 11 | + |
| 12 | +/// Trigger a rebuild for the crate documentation on docs.rs. |
| 13 | +#[utoipa::path( |
| 14 | + post, |
| 15 | + path = "/api/v1/crates/{name}/{version}/rebuild_docs", |
| 16 | + params(CrateVersionPath), |
| 17 | + security( |
| 18 | + ("api_token" = []), |
| 19 | + ("cookie" = []), |
| 20 | + ), |
| 21 | + tag = "versions", |
| 22 | + responses((status = 201, description = "Successful Response")), |
| 23 | +)] |
| 24 | +pub async fn rebuild_version_docs( |
| 25 | + app: AppState, |
| 26 | + path: CrateVersionPath, |
| 27 | + req: Parts, |
| 28 | +) -> AppResult<Response> { |
| 29 | + let Some(ref docs_rs_api_token) = app.config.docs_rs_api_token else { |
| 30 | + return Err(forbidden("docs.rs integration is not configured")); |
| 31 | + }; |
| 32 | + |
| 33 | + let mut conn = app.db_read().await?; |
| 34 | + // FIXME: which scope to use? |
| 35 | + let auth = authenticate(&req, &mut conn, &path.name).await?; |
| 36 | + |
| 37 | + // FIXME: rate limiting needed here? which Action? |
| 38 | + app.rate_limiter |
| 39 | + .check_rate_limit(auth.user_id(), LimitedAction::YankUnyank, &mut conn) |
| 40 | + .await?; |
| 41 | + |
| 42 | + let target_url = app |
| 43 | + .config |
| 44 | + .docs_rs_url |
| 45 | + .join(&format!("/crate/{}/{}/rebuild", path.name, path.version)) |
| 46 | + .unwrap(); // FIXME: handle error |
| 47 | + |
| 48 | + let client = reqwest::Client::new(); |
| 49 | + let response = client |
| 50 | + .post(target_url.as_str()) |
| 51 | + .bearer_auth(docs_rs_api_token) |
| 52 | + .send() |
| 53 | + .await?; |
| 54 | + |
| 55 | + let status= = response.status(); |
| 56 | + |
| 57 | + // reqwest:: |
| 58 | + |
| 59 | + // perform_version_yank_update( |
| 60 | + // &state, |
| 61 | + // &mut conn, |
| 62 | + // &mut version, |
| 63 | + // &krate, |
| 64 | + // &auth, |
| 65 | + // Some(yanked), |
| 66 | + // None, |
| 67 | + // ) |
| 68 | + // .await?; |
| 69 | + |
| 70 | + ok_true() |
| 71 | +} |
0 commit comments