Skip to content

Commit 85f5566

Browse files
committed
github/secret_scanning: Extract send_notification_email() function
1 parent 9ae12fd commit 85f5566

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/controllers/github/secret_scanning.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::models::{ApiToken, User};
33
use crate::schema::api_tokens;
44
use crate::util::read_fill;
55
use crate::util::token::SecureToken;
6+
use anyhow::{anyhow, Context};
67
use base64;
78
use once_cell::sync::Lazy;
89
use ring::signature;
@@ -167,37 +168,41 @@ fn alert_revoke_token(
167168
return Ok(GitHubSecretAlertFeedbackLabel::FalsePositive);
168169
};
169170

170-
// send email notification to the token owner
171-
let user = User::find(&conn, token.user_id)?;
172171
warn!(
173-
gh_login = %user.gh_login, user_id = %user.id, token_id = %token.id,
172+
token_id = %token.id, user_id = %token.user_id,
174173
"Revoked API token",
175174
);
176175

177-
if let Some(email) = user.email(&conn)? {
178-
let result = req.app().emails.send_token_exposed_notification(
179-
&email,
180-
&alert.url,
181-
"GitHub",
182-
&alert.source,
183-
&token.name,
184-
);
185-
if let Err(error) = result {
186-
warn!(
187-
gh_login = %user.gh_login, user_id = %user.id, ?error,
188-
"Failed to send email notification",
189-
);
190-
}
191-
} else {
176+
if let Err(error) = send_notification_email(&token, alert, req) {
192177
warn!(
193-
gh_login = %user.gh_login, user_id = %user.id, error = "No address found",
178+
token_id = %token.id, user_id = %token.user_id, ?error,
194179
"Failed to send email notification",
195-
);
196-
};
180+
)
181+
}
197182

198183
Ok(GitHubSecretAlertFeedbackLabel::TruePositive)
199184
}
200185

186+
fn send_notification_email(
187+
token: &ApiToken,
188+
alert: &GitHubSecretAlert,
189+
req: &dyn RequestExt,
190+
) -> anyhow::Result<()> {
191+
let conn = req.db_read()?;
192+
193+
let user = User::find(&conn, token.user_id).context("Failed to find user")?;
194+
let Some(email) = user.email(&conn)? else {
195+
return Err(anyhow!("No address found"));
196+
};
197+
198+
req.app()
199+
.emails
200+
.send_token_exposed_notification(&email, &alert.url, "GitHub", &alert.source, &token.name)
201+
.map_err(|error| anyhow!("{error}"))?;
202+
203+
Ok(())
204+
}
205+
201206
#[derive(Deserialize, Serialize)]
202207
pub struct GitHubSecretAlertFeedback {
203208
pub token_raw: String,

0 commit comments

Comments
 (0)