Skip to content

Commit 8a37c5f

Browse files
committed
trustpub/emails: Add support for GitLab configs
1 parent bc99b78 commit 8a37c5f

9 files changed

+202
-21
lines changed

src/controllers/trustpub/emails.rs

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
use crate::email::EmailMessage;
2-
use crates_io_database::models::trustpub::GitHubConfig;
2+
use crates_io_database::models::trustpub::{GitHubConfig, GitLabConfig};
33
use crates_io_database::models::{Crate, User};
44

5+
#[derive(Debug, Clone, Copy, serde::Serialize)]
6+
#[serde(tag = "type")]
7+
pub enum ConfigType<'a> {
8+
GitHub(&'a GitHubConfig),
9+
GitLab(&'a GitLabConfig),
10+
}
11+
512
#[derive(serde::Serialize)]
613
pub struct ConfigCreatedEmail<'a> {
714
/// The GitHub login of the email recipient.
@@ -11,7 +18,7 @@ pub struct ConfigCreatedEmail<'a> {
1118
/// The crate for which the trusted publishing configuration was created.
1219
pub krate: &'a Crate,
1320
/// The trusted publishing configuration that was created.
14-
pub saved_config: &'a GitHubConfig,
21+
pub saved_config: ConfigType<'a>,
1522
}
1623

1724
impl ConfigCreatedEmail<'_> {
@@ -29,7 +36,7 @@ pub struct ConfigDeletedEmail<'a> {
2936
/// The crate for which the trusted publishing configuration was deleted.
3037
pub krate: &'a Crate,
3138
/// The trusted publishing configuration that was deleted.
32-
pub config: &'a GitHubConfig,
39+
pub config: ConfigType<'a>,
3340
}
3441

3542
impl ConfigDeletedEmail<'_> {
@@ -88,13 +95,25 @@ mod tests {
8895
}
8996
}
9097

98+
fn test_gitlab_config(environment: Option<&str>) -> GitLabConfig {
99+
GitLabConfig {
100+
id: 1,
101+
created_at: Utc::now(),
102+
crate_id: 1,
103+
namespace: "rust-lang".into(),
104+
project: "my-crate".into(),
105+
workflow_filepath: ".gitlab-ci.yml".into(),
106+
environment: environment.map(String::from),
107+
}
108+
}
109+
91110
#[test]
92111
fn test_config_created_email() {
93112
let email = ConfigCreatedEmail {
94113
recipient: "octocat",
95114
auth_user: &test_user(),
96115
krate: &test_crate(),
97-
saved_config: &test_github_config(None),
116+
saved_config: ConfigType::GitHub(&test_github_config(None)),
98117
};
99118

100119
let rendered = assert_ok!(email.render());
@@ -108,7 +127,7 @@ mod tests {
108127
recipient: "octocat",
109128
auth_user: &test_user(),
110129
krate: &test_crate(),
111-
saved_config: &test_github_config(Some("production")),
130+
saved_config: ConfigType::GitHub(&test_github_config(Some("production"))),
112131
};
113132

114133
let rendered = assert_ok!(email.render());
@@ -122,7 +141,35 @@ mod tests {
122141
recipient: "team-member",
123142
auth_user: &test_user(),
124143
krate: &test_crate(),
125-
saved_config: &test_github_config(None),
144+
saved_config: ConfigType::GitHub(&test_github_config(None)),
145+
};
146+
147+
let rendered = assert_ok!(email.render());
148+
assert_snapshot!(rendered.subject, @"crates.io: Trusted Publishing configuration added to my-crate");
149+
assert_snapshot!(rendered.body_text);
150+
}
151+
152+
#[test]
153+
fn test_config_created_email_gitlab() {
154+
let email = ConfigCreatedEmail {
155+
recipient: "octocat",
156+
auth_user: &test_user(),
157+
krate: &test_crate(),
158+
saved_config: ConfigType::GitLab(&test_gitlab_config(None)),
159+
};
160+
161+
let rendered = assert_ok!(email.render());
162+
assert_snapshot!(rendered.subject, @"crates.io: Trusted Publishing configuration added to my-crate");
163+
assert_snapshot!(rendered.body_text);
164+
}
165+
166+
#[test]
167+
fn test_config_created_email_gitlab_with_environment() {
168+
let email = ConfigCreatedEmail {
169+
recipient: "octocat",
170+
auth_user: &test_user(),
171+
krate: &test_crate(),
172+
saved_config: ConfigType::GitLab(&test_gitlab_config(Some("production"))),
126173
};
127174

128175
let rendered = assert_ok!(email.render());
@@ -136,7 +183,7 @@ mod tests {
136183
recipient: "octocat",
137184
auth_user: &test_user(),
138185
krate: &test_crate(),
139-
config: &test_github_config(None),
186+
config: ConfigType::GitHub(&test_github_config(None)),
140187
};
141188

142189
let rendered = assert_ok!(email.render());
@@ -150,7 +197,7 @@ mod tests {
150197
recipient: "octocat",
151198
auth_user: &test_user(),
152199
krate: &test_crate(),
153-
config: &test_github_config(Some("production")),
200+
config: ConfigType::GitHub(&test_github_config(Some("production"))),
154201
};
155202

156203
let rendered = assert_ok!(email.render());
@@ -164,7 +211,35 @@ mod tests {
164211
recipient: "team-member",
165212
auth_user: &test_user(),
166213
krate: &test_crate(),
167-
config: &test_github_config(None),
214+
config: ConfigType::GitHub(&test_github_config(None)),
215+
};
216+
217+
let rendered = assert_ok!(email.render());
218+
assert_snapshot!(rendered.subject, @"crates.io: Trusted Publishing configuration removed from my-crate");
219+
assert_snapshot!(rendered.body_text);
220+
}
221+
222+
#[test]
223+
fn test_config_deleted_email_gitlab() {
224+
let email = ConfigDeletedEmail {
225+
recipient: "octocat",
226+
auth_user: &test_user(),
227+
krate: &test_crate(),
228+
config: ConfigType::GitLab(&test_gitlab_config(None)),
229+
};
230+
231+
let rendered = assert_ok!(email.render());
232+
assert_snapshot!(rendered.subject, @"crates.io: Trusted Publishing configuration removed from my-crate");
233+
assert_snapshot!(rendered.body_text);
234+
}
235+
236+
#[test]
237+
fn test_config_deleted_email_gitlab_with_environment() {
238+
let email = ConfigDeletedEmail {
239+
recipient: "octocat",
240+
auth_user: &test_user(),
241+
krate: &test_crate(),
242+
config: ConfigType::GitLab(&test_gitlab_config(Some("production"))),
168243
};
169244

170245
let rendered = assert_ok!(email.render());

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
33
use crate::controllers::krate::load_crate;
4-
use crate::controllers::trustpub::emails::ConfigCreatedEmail;
4+
use crate::controllers::trustpub::emails::{ConfigCreatedEmail, ConfigType};
55
use crate::controllers::trustpub::github_configs::json;
66
use crate::util::errors::{AppResult, bad_request, forbidden, server_error};
77
use anyhow::Context;
@@ -117,11 +117,13 @@ pub async fn create_trustpub_github_config(
117117
.collect::<Vec<_>>();
118118

119119
for (recipient, email_address) in &recipients {
120+
let saved_config = ConfigType::GitHub(&saved_config);
121+
120122
let context = ConfigCreatedEmail {
121123
recipient,
122124
auth_user,
123125
krate: &krate,
124-
saved_config: &saved_config,
126+
saved_config,
125127
};
126128

127129
if let Err(err) = send_notification_email(&state, email_address, context).await {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
3-
use crate::controllers::trustpub::emails::ConfigDeletedEmail;
3+
use crate::controllers::trustpub::emails::{ConfigDeletedEmail, ConfigType};
44
use crate::util::errors::{AppResult, bad_request, not_found};
55
use anyhow::Context;
66
use axum::extract::Path;
@@ -82,11 +82,13 @@ pub async fn delete_trustpub_github_config(
8282
.collect::<Vec<_>>();
8383

8484
for (recipient, email_address) in &recipients {
85+
let config = ConfigType::GitHub(&config);
86+
8587
let context = ConfigDeletedEmail {
8688
recipient,
8789
auth_user,
8890
krate: &krate,
89-
config: &config,
91+
config,
9092
};
9193

9294
if let Err(err) = send_notification_email(&state, email_address, context).await {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: src/controllers/trustpub/emails.rs
3+
expression: rendered.body_text
4+
---
5+
Hello octocat!
6+
7+
You added a new "Trusted Publishing" configuration for GitLab CI to your crate "my-crate". Trusted publishers act as trusted users and can publish new versions of the crate automatically.
8+
9+
Trusted Publishing configuration:
10+
11+
- Namespace: rust-lang
12+
- Project: my-crate
13+
- Workflow filepath: .gitlab-ci.yml
14+
- Environment: (not set)
15+
16+
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.
17+
18+
If you are unable to revert the change and need to do so, you can email help@crates.io for assistance.
19+
20+
--
21+
The crates.io Team
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: src/controllers/trustpub/emails.rs
3+
expression: rendered.body_text
4+
---
5+
Hello octocat!
6+
7+
You added a new "Trusted Publishing" configuration for GitLab CI to your crate "my-crate". Trusted publishers act as trusted users and can publish new versions of the crate automatically.
8+
9+
Trusted Publishing configuration:
10+
11+
- Namespace: rust-lang
12+
- Project: my-crate
13+
- Workflow filepath: .gitlab-ci.yml
14+
- Environment: production
15+
16+
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.
17+
18+
If you are unable to revert the change and need to do so, you can email help@crates.io for assistance.
19+
20+
--
21+
The crates.io Team
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: src/controllers/trustpub/emails.rs
3+
expression: rendered.body_text
4+
---
5+
Hello octocat!
6+
7+
You removed a "Trusted Publishing" configuration for GitLab CI from your crate "my-crate".
8+
9+
Trusted Publishing configuration:
10+
11+
- Namespace: rust-lang
12+
- Project: my-crate
13+
- Workflow filepath: .gitlab-ci.yml
14+
- Environment: (not set)
15+
16+
If you did not make this change and you think it was made maliciously, you can email help@crates.io for assistance.
17+
18+
--
19+
The crates.io Team
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: src/controllers/trustpub/emails.rs
3+
expression: rendered.body_text
4+
---
5+
Hello octocat!
6+
7+
You removed a "Trusted Publishing" configuration for GitLab CI from your crate "my-crate".
8+
9+
Trusted Publishing configuration:
10+
11+
- Namespace: rust-lang
12+
- Project: my-crate
13+
- Workflow filepath: .gitlab-ci.yml
14+
- Environment: production
15+
16+
If you did not make this change and you think it was made maliciously, you can email help@crates.io for assistance.
17+
18+
--
19+
The crates.io Team

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
{% extends "base.txt.j2" %}
22

3+
{% if saved_config.type == "GitHub" %}
4+
{% set ci_provider = "GitHub Actions" %}
5+
{% elif saved_config.type == "GitLab" %}
6+
{% set ci_provider = "GitLab CI" %}
7+
{% endif %}
8+
39
{% block content %}
410
Hello {{ recipient }}!
511

612
{% 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.
13+
You added a new "Trusted Publishing" configuration for {{ ci_provider }} to your crate "{{ krate.name }}". Trusted publishers act as trusted users and can publish new versions of the crate automatically.
814
{%- 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.
15+
crates.io user {{ auth_user.gh_login }} added a new "Trusted Publishing" configuration for {{ ci_provider }} to a crate that you manage ("{{ krate.name }}"). Trusted publishers act as trusted users and can publish new versions of the crate automatically.
1016
{%- endif %}
1117

1218
Trusted Publishing configuration:
13-
19+
{% if saved_config.type == "GitHub" %}
1420
- Repository owner: {{ saved_config.repository_owner }}
1521
- Repository name: {{ saved_config.repository_name }}
1622
- Workflow filename: {{ saved_config.workflow_filename }}
1723
- Environment: {{ saved_config.environment or "(not set)" }}
18-
24+
{% elif saved_config.type == "GitLab" %}
25+
- Namespace: {{ saved_config.namespace }}
26+
- Project: {{ saved_config.project }}
27+
- Workflow filepath: {{ saved_config.workflow_filepath }}
28+
- Environment: {{ saved_config.environment or "(not set)" }}
29+
{% endif %}
1930
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.
2031

2132
If you are unable to revert the change and need to do so, you can email [email protected] for assistance.
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
{% extends "base.txt.j2" %}
22

3+
{% if config.type == "GitHub" %}
4+
{% set ci_provider = "GitHub Actions" %}
5+
{% elif config.type == "GitLab" %}
6+
{% set ci_provider = "GitLab CI" %}
7+
{% endif %}
8+
39
{% block content %}
410
Hello {{ recipient }}!
511

612
{% if recipient == auth_user.gh_login -%}
7-
You removed a "Trusted Publishing" configuration for GitHub Actions from your crate "{{ krate.name }}".
13+
You removed a "Trusted Publishing" configuration for {{ ci_provider }} from your crate "{{ krate.name }}".
814
{%- 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 }}").
15+
crates.io user {{ auth_user.gh_login }} removed a "Trusted Publishing" configuration for {{ ci_provider }} from a crate that you manage ("{{ krate.name }}").
1016
{%- endif %}
1117

1218
Trusted Publishing configuration:
13-
19+
{% if config.type == "GitHub" %}
1420
- Repository owner: {{ config.repository_owner }}
1521
- Repository name: {{ config.repository_name }}
1622
- Workflow filename: {{ config.workflow_filename }}
1723
- Environment: {{ config.environment or "(not set)" }}
18-
24+
{% elif config.type == "GitLab" %}
25+
- Namespace: {{ config.namespace }}
26+
- Project: {{ config.project }}
27+
- Workflow filepath: {{ config.workflow_filepath }}
28+
- Environment: {{ config.environment or "(not set)" }}
29+
{% endif %}
1930
If you did not make this change and you think it was made maliciously, you can email [email protected] for assistance.
2031
{% endblock %}

0 commit comments

Comments
 (0)