Skip to content

Commit 4f51afb

Browse files
committed
Reuse the code to perform version yank update
Signed-off-by: Rustin170506 <[email protected]>
1 parent 58b48b0 commit 4f51afb

File tree

2 files changed

+63
-96
lines changed

2 files changed

+63
-96
lines changed

src/controllers/version/metadata.rs

+58-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use axum::extract::Path;
88
use axum::Json;
99
use crates_io_worker::BackgroundJob;
10-
use diesel::{ExpressionMethods, RunQueryDsl};
10+
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
1111
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
1212
use http::request::Parts;
1313
use http::StatusCode;
@@ -142,7 +142,47 @@ fn apply_yank_update(
142142
) -> AppResult<()> {
143143
// Try to update the yank state first, to avoid unnecessary checks.
144144
update_version_yank_state(version, update_data)?;
145+
perform_version_yank_update(state, req, conn, version, krate)?;
145146

147+
Ok(())
148+
}
149+
150+
fn update_version_yank_state(version: &mut Version, update_data: &VersionUpdate) -> AppResult<()> {
151+
match (update_data.yanked, &update_data.yank_message) {
152+
(Some(true), Some(message)) => {
153+
version.yanked = true;
154+
version.yank_message = Some(message.clone());
155+
}
156+
(Some(yanked), None) => {
157+
version.yanked = yanked;
158+
version.yank_message = None;
159+
}
160+
(Some(false), Some(_)) => {
161+
return Err(bad_request("Cannot set yank message when unyanking"));
162+
}
163+
(None, Some(message)) => {
164+
if version.yanked {
165+
version.yank_message = Some(message.clone());
166+
} else {
167+
return Err(bad_request(
168+
"Cannot update yank message for a version that is not yanked",
169+
));
170+
}
171+
}
172+
// If both yanked and yank_message are None, do nothing.
173+
// This function only cares about updating the yanked state and yank message.
174+
(None, None) => {}
175+
}
176+
Ok(())
177+
}
178+
179+
pub fn perform_version_yank_update(
180+
state: &AppState,
181+
req: &Parts,
182+
conn: &mut impl Conn,
183+
version: &Version,
184+
krate: &Crate,
185+
) -> AppResult<()> {
146186
// Add authentication check
147187
let auth = AuthCheck::default()
148188
.with_endpoint_scope(EndpointScope::Yank)
@@ -168,12 +208,27 @@ fn apply_yank_update(
168208
} else {
169209
return Err(custom(
170210
StatusCode::FORBIDDEN,
171-
"must already be an owner to update version",
211+
"must already be an owner to yank or unyank",
172212
));
173213
}
174214
}
175215

176-
diesel::update(&*version)
216+
// Check if the yanked state or yank message has changed
217+
let (yanked, yank_message) = crate::schema::versions::table
218+
.find(version.id)
219+
.select((
220+
crate::schema::versions::yanked,
221+
crate::schema::versions::yank_message,
222+
))
223+
.first::<(bool, Option<String>)>(conn)?;
224+
225+
if yanked == version.yanked && yank_message == version.yank_message {
226+
// No changes, return early
227+
return Ok(());
228+
}
229+
230+
// Proceed with the update
231+
diesel::update(version)
177232
.set((
178233
crate::schema::versions::yanked.eq(version.yanked),
179234
crate::schema::versions::yank_message.eq(&version.yank_message),
@@ -194,32 +249,3 @@ fn apply_yank_update(
194249

195250
Ok(())
196251
}
197-
198-
fn update_version_yank_state(version: &mut Version, update_data: &VersionUpdate) -> AppResult<()> {
199-
match (update_data.yanked, &update_data.yank_message) {
200-
(Some(true), Some(message)) => {
201-
version.yanked = true;
202-
version.yank_message = Some(message.clone());
203-
}
204-
(Some(yanked), None) => {
205-
version.yanked = yanked;
206-
version.yank_message = None;
207-
}
208-
(Some(false), Some(_)) => {
209-
return Err(bad_request("Cannot set yank message when unyanking"));
210-
}
211-
(None, Some(message)) => {
212-
if version.yanked {
213-
version.yank_message = Some(message.clone());
214-
} else {
215-
return Err(bad_request(
216-
"Cannot update yank message for a version that is not yanked",
217-
));
218-
}
219-
}
220-
// If both yanked and yank_message are None, do nothing.
221-
// This function only cares about updating the yanked state and yank message.
222-
(None, None) => {}
223-
}
224-
Ok(())
225-
}

src/controllers/version/yank.rs

+5-64
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
//! Endpoints for yanking and unyanking specific versions of crates
22
3+
use super::metadata::perform_version_yank_update;
34
use super::version_and_crate;
45
use crate::app::AppState;
5-
use crate::auth::AuthCheck;
66
use crate::controllers::helpers::ok_true;
7-
use crate::models::token::EndpointScope;
8-
use crate::models::Rights;
9-
use crate::models::{insert_version_owner_action, VersionAction};
10-
use crate::rate_limiter::LimitedAction;
11-
use crate::schema::versions;
127
use crate::tasks::spawn_blocking;
13-
use crate::util::errors::{custom, version_not_found, AppResult};
14-
use crate::worker::jobs;
15-
use crate::worker::jobs::UpdateDefaultVersion;
8+
use crate::util::errors::{version_not_found, AppResult};
169
use axum::extract::Path;
1710
use axum::response::Response;
18-
use crates_io_worker::BackgroundJob;
19-
use diesel::prelude::*;
2011
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
2112
use http::request::Parts;
22-
use http::StatusCode;
23-
use tokio::runtime::Handle;
2413

2514
/// Handles the `DELETE /crates/:crate_id/:version/yank` route.
2615
/// This does not delete a crate version, it makes the crate
@@ -66,57 +55,9 @@ async fn modify_yank(
6655
let conn = state.db_write().await?;
6756
spawn_blocking(move || {
6857
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();
69-
70-
let auth = AuthCheck::default()
71-
.with_endpoint_scope(EndpointScope::Yank)
72-
.for_crate(&crate_name)
73-
.check(&req, conn)?;
74-
75-
state
76-
.rate_limiter
77-
.check_rate_limit(auth.user_id(), LimitedAction::YankUnyank, conn)?;
78-
79-
let (version, krate) = version_and_crate(conn, &crate_name, &version)?;
80-
let api_token_id = auth.api_token_id();
81-
let user = auth.user();
82-
let owners = krate.owners(conn)?;
83-
84-
if Handle::current().block_on(user.rights(&state, &owners))? < Rights::Publish {
85-
if user.is_admin {
86-
let action = if yanked { "yanking" } else { "unyanking" };
87-
warn!(
88-
"Admin {} is {action} {}@{}",
89-
user.gh_login, krate.name, version.num
90-
);
91-
} else {
92-
return Err(custom(
93-
StatusCode::FORBIDDEN,
94-
"must already be an owner to yank or unyank",
95-
));
96-
}
97-
}
98-
99-
if version.yanked == yanked {
100-
// The crate is already in the state requested, nothing to do
101-
return ok_true();
102-
}
103-
104-
diesel::update(&version)
105-
.set(versions::yanked.eq(yanked))
106-
.execute(conn)?;
107-
108-
let action = if yanked {
109-
VersionAction::Yank
110-
} else {
111-
VersionAction::Unyank
112-
};
113-
114-
insert_version_owner_action(conn, version.id, user.id, api_token_id, action)?;
115-
116-
jobs::enqueue_sync_to_index(&krate.name, conn)?;
117-
118-
UpdateDefaultVersion::new(krate.id).enqueue(conn)?;
119-
58+
let (mut version, krate) = version_and_crate(conn, &crate_name, &version)?;
59+
version.yanked = yanked;
60+
perform_version_yank_update(&state, &req, conn, &mut version, &krate)?;
12061
ok_true()
12162
})
12263
.await

0 commit comments

Comments
 (0)