Skip to content

controllers/token: Move name check outside of spawn_blocking() callback #9574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/controllers/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ pub async fn new(
parts: Parts,
Json(new): Json<NewApiTokenRequest>,
) -> AppResult<Json<Value>> {
if new.api_token.name.is_empty() {
return Err(bad_request("name must have a value"));
}

let conn = app.db_write().await?;
spawn_blocking(move || {
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();

let name = &new.api_token.name;
if name.is_empty() {
return Err(bad_request("name must have a value"));
}

let auth = AuthCheck::default().check(&parts, conn)?;
if auth.api_token_id().is_some() {
return Err(bad_request(
Expand Down Expand Up @@ -141,15 +140,15 @@ pub async fn new(
let api_token = ApiToken::insert_with_scopes(
conn,
user.id,
name,
&new.api_token.name,
crate_scopes,
endpoint_scopes,
new.api_token.expired_at,
)?;

if let Some(recipient) = recipient {
let email = NewTokenEmail {
token_name: name,
token_name: &new.api_token.name,
user_name: &user.gh_login,
domain: &app.emails.domain,
};
Expand Down