Skip to content

Commit 391b0b7

Browse files
committed
admin/delete_crate: Add --deleted-by CLI option
1 parent abd3190 commit 391b0b7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/bin/crates-admin/delete_crate.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::dialoguer;
22
use anyhow::Context;
33
use chrono::{NaiveDateTime, Utc};
44
use colored::Colorize;
5-
use crates_io::models::NewDeletedCrate;
5+
use crates_io::models::{NewDeletedCrate, User};
66
use crates_io::schema::{crate_downloads, deleted_crates};
77
use crates_io::worker::jobs;
88
use crates_io::{db, schema::crates};
@@ -29,6 +29,10 @@ pub struct Opts {
2929
/// Don't ask for confirmation: yes, we are sure. Best for scripting.
3030
#[arg(short, long)]
3131
yes: bool,
32+
33+
/// Your GitHub username.
34+
#[arg(long)]
35+
deleted_by: Option<String>,
3236
}
3337

3438
pub async fn run(opts: Opts) -> anyhow::Result<()> {
@@ -63,6 +67,8 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
6367
return Ok(());
6468
}
6569

70+
let deleted_by = get_deleted_by(&mut conn, opts.deleted_by.as_deref()).await?;
71+
6672
let now = Utc::now();
6773

6874
for name in &crate_names {
@@ -73,6 +79,7 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
7379
let deleted_crate = NewDeletedCrate::builder(name)
7480
.created_at(&created_at)
7581
.deleted_at(&now)
82+
.maybe_deleted_by(deleted_by)
7683
.available_at(&now)
7784
.build();
7885

@@ -109,6 +116,18 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
109116
Ok(())
110117
}
111118

119+
async fn get_deleted_by(
120+
conn: &mut AsyncPgConnection,
121+
deleted_by: Option<&str>,
122+
) -> QueryResult<Option<i32>> {
123+
let Some(deleted_by) = deleted_by else {
124+
return Ok(None);
125+
};
126+
127+
let user = User::async_find_by_login(conn, deleted_by).await?;
128+
Ok(Some(user.id))
129+
}
130+
112131
async fn delete_from_database(
113132
conn: &mut AsyncPgConnection,
114133
crate_id: i32,

src/models/user.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ impl User {
5151
.first(conn)
5252
}
5353

54+
pub async fn async_find_by_login(
55+
conn: &mut AsyncPgConnection,
56+
login: &str,
57+
) -> QueryResult<User> {
58+
use diesel_async::RunQueryDsl;
59+
60+
users::table
61+
.filter(lower(users::gh_login).eq(login.to_lowercase()))
62+
.filter(users::gh_id.ne(-1))
63+
.order(users::gh_id.desc())
64+
.first(conn)
65+
.await
66+
}
67+
5468
pub fn owning(krate: &Crate, conn: &mut impl Conn) -> QueryResult<Vec<Owner>> {
5569
use diesel::RunQueryDsl;
5670

0 commit comments

Comments
 (0)