Skip to content

Commit c619862

Browse files
committed
Replace trustpub::Config{Created,Deleted}Email structs with EmailMessage::from_template()
1 parent 77b7087 commit c619862

File tree

8 files changed

+85
-120
lines changed

8 files changed

+85
-120
lines changed

src/controllers/trustpub/github_configs/create/mod.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
33
use crate::controllers::krate::load_crate;
4-
use crate::controllers::trustpub::github_configs::emails::ConfigCreatedEmail;
54
use crate::controllers::trustpub::github_configs::json;
5+
use crate::email::EmailMessage;
66
use crate::util::errors::{AppResult, bad_request, forbidden};
7+
use anyhow::Context;
78
use axum::Json;
89
use crates_io_database::models::OwnerKind;
910
use crates_io_database::models::trustpub::NewGitHubConfig;
@@ -15,6 +16,7 @@ use crates_io_trustpub::github::validation::{
1516
use diesel::prelude::*;
1617
use diesel_async::RunQueryDsl;
1718
use http::request::Parts;
19+
use minijinja::context;
1820
use oauth2::AccessToken;
1921
use secrecy::ExposeSecret;
2022

@@ -105,18 +107,18 @@ pub async fn create_trustpub_github_config(
105107
.collect::<Vec<_>>();
106108

107109
for (recipient, email_address) in &recipients {
108-
let email = ConfigCreatedEmail {
109-
recipient,
110-
user: &auth_user.gh_login,
111-
krate: &krate.name,
112-
repository_owner: &saved_config.repository_owner,
113-
repository_name: &saved_config.repository_name,
114-
workflow_filename: &saved_config.workflow_filename,
115-
environment: saved_config.environment.as_deref().unwrap_or("(not set)"),
110+
let context = context! {
111+
recipient => recipient,
112+
user => auth_user.gh_login,
113+
krate => krate.name,
114+
repository_owner => saved_config.repository_owner,
115+
repository_name => saved_config.repository_name,
116+
workflow_filename => saved_config.workflow_filename,
117+
environment => saved_config.environment
116118
};
117119

118-
if let Err(err) = state.emails.send(email_address, email).await {
119-
warn!("Failed to send trusted publishing notification to {email_address}: {err}")
120+
if let Err(err) = send_notification_email(&state, email_address, context).await {
121+
warn!("Failed to send trusted publishing notification to {email_address}: {err}");
120122
}
121123
}
122124

@@ -133,3 +135,18 @@ pub async fn create_trustpub_github_config(
133135

134136
Ok(Json(json::CreateResponse { github_config }))
135137
}
138+
139+
async fn send_notification_email(
140+
state: &AppState,
141+
email_address: &str,
142+
context: minijinja::Value,
143+
) -> anyhow::Result<()> {
144+
let email = EmailMessage::from_template("config_created", context)
145+
.context("Failed to render email template")?;
146+
147+
state
148+
.emails
149+
.send(email_address, email)
150+
.await
151+
.context("Failed to send email")
152+
}

src/controllers/trustpub/github_configs/delete/mod.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
3-
use crate::controllers::trustpub::github_configs::emails::ConfigDeletedEmail;
3+
use crate::email::EmailMessage;
44
use crate::util::errors::{AppResult, bad_request, not_found};
5+
use anyhow::Context;
56
use axum::extract::Path;
67
use crates_io_database::models::OwnerKind;
78
use crates_io_database::models::trustpub::GitHubConfig;
@@ -10,6 +11,8 @@ use diesel::prelude::*;
1011
use diesel_async::RunQueryDsl;
1112
use http::StatusCode;
1213
use http::request::Parts;
14+
use minijinja::context;
15+
use tracing::warn;
1316

1417
#[cfg(test)]
1518
mod tests;
@@ -76,20 +79,35 @@ pub async fn delete_trustpub_github_config(
7679
.collect::<Vec<_>>();
7780

7881
for (recipient, email_address) in &recipients {
79-
let email = ConfigDeletedEmail {
80-
recipient,
81-
user: &auth_user.gh_login,
82-
krate: &crate_name,
83-
repository_owner: &config.repository_owner,
84-
repository_name: &config.repository_name,
85-
workflow_filename: &config.workflow_filename,
86-
environment: config.environment.as_deref().unwrap_or("(not set)"),
82+
let context = context! {
83+
recipient => recipient,
84+
user => auth_user.gh_login,
85+
krate => crate_name,
86+
repository_owner => config.repository_owner,
87+
repository_name => config.repository_name,
88+
workflow_filename => config.workflow_filename,
89+
environment => config.environment
8790
};
8891

89-
if let Err(err) = state.emails.send(email_address, email).await {
90-
warn!("Failed to send trusted publishing notification to {email_address}: {err}")
92+
if let Err(err) = send_notification_email(&state, email_address, context).await {
93+
warn!("Failed to send trusted publishing notification to {email_address}: {err}");
9194
}
9295
}
9396

9497
Ok(StatusCode::NO_CONTENT)
9598
}
99+
100+
async fn send_notification_email(
101+
state: &AppState,
102+
email_address: &str,
103+
context: minijinja::Value,
104+
) -> anyhow::Result<()> {
105+
let email = EmailMessage::from_template("config_deleted", context)
106+
.context("Failed to render email template")?;
107+
108+
state
109+
.emails
110+
.send(email_address, email)
111+
.await
112+
.context("Failed to send email")
113+
}

src/controllers/trustpub/github_configs/emails.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub mod create;
22
pub mod delete;
3-
pub mod emails;
43
pub mod json;
54
pub mod list;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Hello {{ recipient }}!
2+
3+
crates.io user {{ user }} added a new "Trusted Publishing" configuration for GitHub Actions to a crate that you manage ({{ krate }}). Trusted publishers act as trusted users and can publish new versions of the crate automatically.
4+
5+
Trusted Publishing configuration:
6+
7+
- Repository owner: {{ repository_owner }}
8+
- Repository name: {{ repository_name }}
9+
- Workflow filename: {{ workflow_filename }}
10+
- Environment: {{ environment or "(not set)" }}
11+
12+
If you did not make this change and you think it was made maliciously, you can remove the configuration from the crate via the "Settings" tab on the crate's page.
13+
14+
If you are unable to revert the change and need to do so, you can email help@crates.io to communicate with the crates.io support team.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Trusted Publishing configuration added to {{ krate }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Hello {{ recipient }}!
2+
3+
crates.io user {{ user }} removed a "Trusted Publishing" configuration for GitHub Actions from a crate that you manage ({{ krate }}).
4+
5+
Trusted Publishing configuration:
6+
7+
- Repository owner: {{ repository_owner }}
8+
- Repository name: {{ repository_name }}
9+
- Workflow filename: {{ workflow_filename }}
10+
- Environment: {{ environment or "(not set)" }}
11+
12+
If you did not make this change and you think it was made maliciously, you can email help@crates.io to communicate with the crates.io support team.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Trusted Publishing configuration removed from {{ krate }}

0 commit comments

Comments
 (0)