|
6 | 6 |
|
7 | 7 | use axum::extract::Path;
|
8 | 8 | use axum::Json;
|
| 9 | +use crates_io_worker::BackgroundJob; |
| 10 | +use diesel::{ |
| 11 | + BoolExpressionMethods, ExpressionMethods, PgExpressionMethods, QueryDsl, RunQueryDsl, |
| 12 | +}; |
9 | 13 | use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
|
| 14 | +use http::request::Parts; |
| 15 | +use http::StatusCode; |
| 16 | +use serde::Deserialize; |
10 | 17 | use serde_json::Value;
|
| 18 | +use tokio::runtime::Handle; |
11 | 19 |
|
12 | 20 | use crate::app::AppState;
|
13 |
| -use crate::models::VersionOwnerAction; |
| 21 | +use crate::auth::AuthCheck; |
| 22 | +use crate::models::token::EndpointScope; |
| 23 | +use crate::models::{ |
| 24 | + insert_version_owner_action, Crate, Rights, Version, VersionAction, VersionOwnerAction, |
| 25 | +}; |
| 26 | +use crate::rate_limiter::LimitedAction; |
| 27 | +use crate::schema::versions; |
14 | 28 | use crate::tasks::spawn_blocking;
|
15 |
| -use crate::util::errors::{version_not_found, AppResult}; |
| 29 | +use crate::util::diesel::Conn; |
| 30 | +use crate::util::errors::{bad_request, custom, version_not_found, AppResult}; |
16 | 31 | use crate::views::{EncodableDependency, EncodableVersion};
|
| 32 | +use crate::worker::jobs::{self, UpdateDefaultVersion}; |
17 | 33 |
|
18 | 34 | use super::version_and_crate;
|
19 | 35 |
|
| 36 | +#[derive(Deserialize)] |
| 37 | +pub struct VersionUpdate { |
| 38 | + yanked: Option<bool>, |
| 39 | + yank_message: Option<String>, |
| 40 | +} |
| 41 | +#[derive(Deserialize)] |
| 42 | +pub struct VersionUpdateRequest { |
| 43 | + version: VersionUpdate, |
| 44 | +} |
| 45 | + |
20 | 46 | /// Handles the `GET /crates/:crate_id/:version/dependencies` route.
|
21 | 47 | ///
|
22 | 48 | /// This information can be obtained directly from the index.
|
@@ -84,3 +110,132 @@ pub async fn show(
|
84 | 110 | })
|
85 | 111 | .await
|
86 | 112 | }
|
| 113 | + |
| 114 | +/// Handles the `PATCH /crates/:crate/:version` route. |
| 115 | +/// |
| 116 | +/// This endpoint allows updating the yanked state of a version, including a yank message. |
| 117 | +pub async fn update( |
| 118 | + state: AppState, |
| 119 | + Path((crate_name, version)): Path<(String, String)>, |
| 120 | + req: Parts, |
| 121 | + Json(update_request): Json<VersionUpdateRequest>, |
| 122 | +) -> AppResult<Json<Value>> { |
| 123 | + if semver::Version::parse(&version).is_err() { |
| 124 | + return Err(version_not_found(&crate_name, &version)); |
| 125 | + } |
| 126 | + |
| 127 | + let conn = state.db_write().await?; |
| 128 | + spawn_blocking(move || { |
| 129 | + let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into(); |
| 130 | + let (mut version, krate) = version_and_crate(conn, &crate_name, &version)?; |
| 131 | + |
| 132 | + validate_yank_update(&update_request.version, &version)?; |
| 133 | + perform_version_yank_update( |
| 134 | + &state, |
| 135 | + &req, |
| 136 | + conn, |
| 137 | + &mut version, |
| 138 | + &krate, |
| 139 | + update_request.version.yanked, |
| 140 | + update_request.version.yank_message, |
| 141 | + )?; |
| 142 | + |
| 143 | + let published_by = version.published_by(conn); |
| 144 | + let actions = VersionOwnerAction::by_version(conn, &version)?; |
| 145 | + let updated_version = EncodableVersion::from(version, &krate.name, published_by, actions); |
| 146 | + Ok(Json(json!({ "version": updated_version }))) |
| 147 | + }) |
| 148 | + .await |
| 149 | +} |
| 150 | + |
| 151 | +fn validate_yank_update(update_data: &VersionUpdate, version: &Version) -> AppResult<()> { |
| 152 | + match (update_data.yanked, &update_data.yank_message) { |
| 153 | + (Some(false), Some(_)) => { |
| 154 | + return Err(bad_request("Cannot set yank message when unyanking")); |
| 155 | + } |
| 156 | + (None, Some(_)) => { |
| 157 | + if !version.yanked { |
| 158 | + return Err(bad_request( |
| 159 | + "Cannot update yank message for a version that is not yanked", |
| 160 | + )); |
| 161 | + } |
| 162 | + } |
| 163 | + _ => {} |
| 164 | + } |
| 165 | + Ok(()) |
| 166 | +} |
| 167 | + |
| 168 | +pub fn perform_version_yank_update( |
| 169 | + state: &AppState, |
| 170 | + req: &Parts, |
| 171 | + conn: &mut impl Conn, |
| 172 | + version: &mut Version, |
| 173 | + krate: &Crate, |
| 174 | + yanked: Option<bool>, |
| 175 | + yank_message: Option<String>, |
| 176 | +) -> AppResult<()> { |
| 177 | + let auth = AuthCheck::default() |
| 178 | + .with_endpoint_scope(EndpointScope::Yank) |
| 179 | + .for_crate(&krate.name) |
| 180 | + .check(req, conn)?; |
| 181 | + |
| 182 | + state |
| 183 | + .rate_limiter |
| 184 | + .check_rate_limit(auth.user_id(), LimitedAction::YankUnyank, conn)?; |
| 185 | + |
| 186 | + let api_token_id = auth.api_token_id(); |
| 187 | + let user = auth.user(); |
| 188 | + let owners = krate.owners(conn)?; |
| 189 | + |
| 190 | + let yanked = yanked.unwrap_or(version.yanked); |
| 191 | + |
| 192 | + if Handle::current().block_on(user.rights(state, &owners))? < Rights::Publish { |
| 193 | + if user.is_admin { |
| 194 | + let action = if yanked { "yanking" } else { "unyanking" }; |
| 195 | + warn!( |
| 196 | + "Admin {} is {action} {}@{}", |
| 197 | + user.gh_login, krate.name, version.num |
| 198 | + ); |
| 199 | + } else { |
| 200 | + return Err(custom( |
| 201 | + StatusCode::FORBIDDEN, |
| 202 | + "must already be an owner to yank or unyank", |
| 203 | + )); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + // Check if the yanked state or yank message has changed and update if necessary |
| 208 | + let updated_cnt = diesel::update( |
| 209 | + versions::table.find(version.id).filter( |
| 210 | + versions::yanked |
| 211 | + .is_distinct_from(yanked) |
| 212 | + .or(versions::yank_message.is_distinct_from(&yank_message)), |
| 213 | + ), |
| 214 | + ) |
| 215 | + .set(( |
| 216 | + versions::yanked.eq(yanked), |
| 217 | + versions::yank_message.eq(&yank_message), |
| 218 | + )) |
| 219 | + .execute(conn)?; |
| 220 | + |
| 221 | + // If no rows were updated, return early |
| 222 | + if updated_cnt == 0 { |
| 223 | + return Ok(()); |
| 224 | + } |
| 225 | + |
| 226 | + // Apply the update to the version |
| 227 | + version.yanked = yanked; |
| 228 | + version.yank_message = yank_message; |
| 229 | + |
| 230 | + let action = if yanked { |
| 231 | + VersionAction::Yank |
| 232 | + } else { |
| 233 | + VersionAction::Unyank |
| 234 | + }; |
| 235 | + insert_version_owner_action(conn, version.id, user.id, api_token_id, action)?; |
| 236 | + |
| 237 | + jobs::enqueue_sync_to_index(&krate.name, conn)?; |
| 238 | + UpdateDefaultVersion::new(krate.id).enqueue(conn)?; |
| 239 | + |
| 240 | + Ok(()) |
| 241 | +} |
0 commit comments