Skip to content

Commit 9cd62dc

Browse files
authored
Merge pull request #11575 from Turbo87/trustpub-emails
trustpub: Improve email templates
2 parents c7572a0 + 7f25d16 commit 9cd62dc

File tree

12 files changed

+39
-41
lines changed

12 files changed

+39
-41
lines changed

crates/crates_io_database/src/models/krate.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use diesel::sql_types::{Bool, Integer, Text};
1212
use diesel_async::scoped_futures::ScopedFutureExt;
1313
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
1414
use secrecy::SecretString;
15+
use serde::Serialize;
1516
use thiserror::Error;
1617
use tracing::instrument;
1718

@@ -35,7 +36,9 @@ pub struct CrateName {
3536
pub name: String,
3637
}
3738

38-
#[derive(Debug, Clone, Queryable, Identifiable, AsChangeset, QueryableByName, Selectable)]
39+
#[derive(
40+
Debug, Clone, Queryable, Identifiable, AsChangeset, QueryableByName, Selectable, Serialize,
41+
)]
3942
#[diesel(table_name = crates, check_for_backend(diesel::pg::Pg))]
4043
pub struct Crate {
4144
pub id: i32,

crates/crates_io_database/src/models/trustpub/github_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::schema::trustpub_configs_github;
22
use chrono::{DateTime, Utc};
33
use diesel::prelude::*;
44
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
use serde::Serialize;
56

6-
#[derive(Debug, Identifiable, Queryable, Selectable)]
7+
#[derive(Debug, Identifiable, Queryable, Selectable, Serialize)]
78
#[diesel(table_name = trustpub_configs_github, check_for_backend(diesel::pg::Pg))]
89
pub struct GitHubConfig {
910
pub id: i32,

crates/crates_io_database/src/models/user.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use diesel::sql_types::Integer;
66
use diesel::upsert::excluded;
77
use diesel_async::{AsyncPgConnection, RunQueryDsl};
88
use secrecy::SecretString;
9+
use serde::Serialize;
910

1011
use crate::models::{Crate, CrateOwner, Email, Owner, OwnerKind};
1112
use crate::schema::{crate_owners, emails, users};
1213
use crates_io_diesel_helpers::lower;
1314

1415
/// The model representing a row in the `users` database table.
15-
#[derive(Clone, Debug, Queryable, Identifiable, Selectable)]
16+
#[derive(Clone, Debug, Queryable, Identifiable, Selectable, Serialize)]
1617
pub struct User {
1718
pub id: i32,
1819
#[diesel(deserialize_as = String)]
20+
#[serde(skip)]
1921
pub gh_access_token: SecretString,
2022
pub gh_login: String,
2123
pub name: Option<String>,

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,7 @@ pub async fn create_trustpub_github_config(
108108
.collect::<Vec<_>>();
109109

110110
for (recipient, email_address) in &recipients {
111-
let context = context! {
112-
recipient => recipient,
113-
user => auth_user.gh_login,
114-
krate => krate.name,
115-
repository_owner => saved_config.repository_owner,
116-
repository_name => saved_config.repository_name,
117-
workflow_filename => saved_config.workflow_filename,
118-
environment => saved_config.environment
119-
};
111+
let context = context! { recipient, auth_user, krate, saved_config };
120112

121113
if let Err(err) = send_notification_email(&state, email_address, context).await {
122114
warn!("Failed to send trusted publishing notification to {email_address}: {err}");

src/controllers/trustpub/github_configs/create/snapshots/crates_io__controllers__trustpub__github_configs__create__tests__happy_path-3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Content-Transfer-Encoding: quoted-printable
1111

1212
Hello foo!
1313

14-
crates.io user foo added a new "Trusted Publishing" configuration for GitHub Actions to a crate that you manage ("foo"). Trusted publishers act as trusted users and can publish new versions of the crate automatically.
14+
You added a new "Trusted Publishing" configuration for GitHub Actions to your crate "foo". Trusted publishers act as trusted users and can publish new versions of the crate automatically.
1515

1616
Trusted Publishing configuration:
1717

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::email::EmailMessage;
44
use crate::util::errors::{AppResult, bad_request, not_found};
55
use anyhow::Context;
66
use axum::extract::Path;
7-
use crates_io_database::models::OwnerKind;
87
use crates_io_database::models::trustpub::GitHubConfig;
8+
use crates_io_database::models::{Crate, OwnerKind};
99
use crates_io_database::schema::{crate_owners, crates, emails, trustpub_configs_github, users};
1010
use diesel::prelude::*;
1111
use diesel_async::RunQueryDsl;
@@ -39,12 +39,12 @@ pub async fn delete_trustpub_github_config(
3939
let auth_user = auth.user();
4040

4141
// Check that a trusted publishing config with the given ID exists,
42-
// and fetch the corresponding crate ID and name.
43-
let (config, crate_name) = trustpub_configs_github::table
42+
// and fetch the corresponding crate.
43+
let (config, krate) = trustpub_configs_github::table
4444
.inner_join(crates::table)
4545
.filter(trustpub_configs_github::id.eq(id))
46-
.select((GitHubConfig::as_select(), crates::name))
47-
.first::<(GitHubConfig, String)>(&mut conn)
46+
.select((GitHubConfig::as_select(), Crate::as_select()))
47+
.first::<(GitHubConfig, Crate)>(&mut conn)
4848
.await
4949
.optional()?
5050
.ok_or_else(not_found)?;
@@ -79,15 +79,7 @@ pub async fn delete_trustpub_github_config(
7979
.collect::<Vec<_>>();
8080

8181
for (recipient, email_address) in &recipients {
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
90-
};
82+
let context = context! { recipient, auth_user, krate, config };
9183

9284
if let Err(err) = send_notification_email(&state, email_address, context).await {
9385
warn!("Failed to send trusted publishing notification to {email_address}: {err}");

src/controllers/trustpub/github_configs/delete/snapshots/crates_io__controllers__trustpub__github_configs__delete__tests__happy_path-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Content-Transfer-Encoding: quoted-printable
1111

1212
Hello foo!
1313

14-
crates.io user foo removed a "Trusted Publishing" configuration for GitHub Actions from a crate that you manage ("foo").
14+
You removed a "Trusted Publishing" configuration for GitHub Actions from your crate "foo".
1515

1616
Trusted Publishing configuration:
1717

src/email/templates/config_created/body.txt.j2

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
{% block content %}
44
Hello {{ recipient }}!
55

6-
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.
6+
{% if recipient == auth_user.gh_login -%}
7+
You added a new "Trusted Publishing" configuration for GitHub Actions to your crate "{{ krate.name }}". Trusted publishers act as trusted users and can publish new versions of the crate automatically.
8+
{%- else -%}
9+
crates.io user {{ auth_user.gh_login }} added a new "Trusted Publishing" configuration for GitHub Actions to a crate that you manage ("{{ krate.name }}"). Trusted publishers act as trusted users and can publish new versions of the crate automatically.
10+
{%- endif %}
711

812
Trusted Publishing configuration:
913

10-
- Repository owner: {{ repository_owner }}
11-
- Repository name: {{ repository_name }}
12-
- Workflow filename: {{ workflow_filename }}
13-
- Environment: {{ environment or "(not set)" }}
14+
- Repository owner: {{ saved_config.repository_owner }}
15+
- Repository name: {{ saved_config.repository_name }}
16+
- Workflow filename: {{ saved_config.workflow_filename }}
17+
- Environment: {{ saved_config.environment or "(not set)" }}
1418

1519
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.
1620

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
crates.io: Trusted Publishing configuration added to {{ krate }}
1+
crates.io: Trusted Publishing configuration added to {{ krate.name }}

src/email/templates/config_deleted/body.txt.j2

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
{% block content %}
44
Hello {{ recipient }}!
55

6-
crates.io user {{ user }} removed a "Trusted Publishing" configuration for GitHub Actions from a crate that you manage ("{{ krate }}").
6+
{% if recipient == auth_user.gh_login -%}
7+
You removed a "Trusted Publishing" configuration for GitHub Actions from your crate "{{ krate.name }}".
8+
{%- else -%}
9+
crates.io user {{ auth_user.gh_login }} removed a "Trusted Publishing" configuration for GitHub Actions from a crate that you manage ("{{ krate.name }}").
10+
{%- endif %}
711

812
Trusted Publishing configuration:
913

10-
- Repository owner: {{ repository_owner }}
11-
- Repository name: {{ repository_name }}
12-
- Workflow filename: {{ workflow_filename }}
13-
- Environment: {{ environment or "(not set)" }}
14+
- Repository owner: {{ config.repository_owner }}
15+
- Repository name: {{ config.repository_name }}
16+
- Workflow filename: {{ config.workflow_filename }}
17+
- Environment: {{ config.environment or "(not set)" }}
1418

1519
If you did not make this change and you think it was made maliciously, you can email [email protected] for assistance.
1620
{% endblock %}

0 commit comments

Comments
 (0)