Skip to content

Commit 671d1fb

Browse files
authored
trustpub/emails: Add support for GitLab configs (#12048)
1 parent 3690d32 commit 671d1fb

9 files changed

+182
-19
lines changed

src/controllers/trustpub/emails.rs

Lines changed: 85 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,26 @@ 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_id: None,
104+
namespace: "rust-lang".into(),
105+
project: "my-crate".into(),
106+
workflow_filepath: ".gitlab-ci.yml".into(),
107+
environment: environment.map(String::from),
108+
}
109+
}
110+
91111
#[test]
92112
fn test_config_created_email() {
93113
let email = ConfigCreatedEmail {
94114
recipient: "octocat",
95115
auth_user: &test_user(),
96116
krate: &test_crate(),
97-
saved_config: &test_github_config(None),
117+
saved_config: ConfigType::GitHub(&test_github_config(None)),
98118
};
99119

100120
let rendered = assert_ok!(email.render());
@@ -108,7 +128,7 @@ mod tests {
108128
recipient: "octocat",
109129
auth_user: &test_user(),
110130
krate: &test_crate(),
111-
saved_config: &test_github_config(Some("production")),
131+
saved_config: ConfigType::GitHub(&test_github_config(Some("production"))),
112132
};
113133

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

128176
let rendered = assert_ok!(email.render());
@@ -136,7 +184,7 @@ mod tests {
136184
recipient: "octocat",
137185
auth_user: &test_user(),
138186
krate: &test_crate(),
139-
config: &test_github_config(None),
187+
config: ConfigType::GitHub(&test_github_config(None)),
140188
};
141189

142190
let rendered = assert_ok!(email.render());
@@ -150,7 +198,7 @@ mod tests {
150198
recipient: "octocat",
151199
auth_user: &test_user(),
152200
krate: &test_crate(),
153-
config: &test_github_config(Some("production")),
201+
config: ConfigType::GitHub(&test_github_config(Some("production"))),
154202
};
155203

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

170246
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
This configuration allows the workflow file at https://gitlab.com/rust-lang/my-crate/-/blob/HEAD/.gitlab-ci.yml to publish new versions of this crate.
10+
11+
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.
12+
13+
If you are unable to revert the change and need to do so, you can email help@crates.io for assistance.
14+
15+
--
16+
The crates.io Team
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
This configuration allows the workflow file at https://gitlab.com/rust-lang/my-crate/-/blob/HEAD/.gitlab-ci.yml to publish new versions of this crate. The workflow must use the `production` environment.
10+
11+
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.
12+
13+
If you are unable to revert the change and need to do so, you can email help@crates.io for assistance.
14+
15+
--
16+
The crates.io Team
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
The removed configuration was for the workflow file at https://gitlab.com/rust-lang/my-crate/-/blob/HEAD/.gitlab-ci.yml.
10+
11+
If you did not make this change and you think it was made maliciously, you can email help@crates.io for assistance.
12+
13+
--
14+
The crates.io Team
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
The removed configuration was for the workflow file at https://gitlab.com/rust-lang/my-crate/-/blob/HEAD/.gitlab-ci.yml using the `production` environment.
10+
11+
If you did not make this change and you think it was made maliciously, you can email help@crates.io for assistance.
12+
13+
--
14+
The crates.io Team

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
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

18+
{% if saved_config.type == "GitHub" -%}
1219
This configuration allows the workflow file at https://github.com/{{ saved_config.repository_owner }}/{{ saved_config.repository_name }}/blob/HEAD/.github/workflows/{{ saved_config.workflow_filename }} to publish new versions of this crate.
1320
{%- if saved_config.environment %} The workflow must use the `{{ saved_config.environment }}` environment (https://github.com/{{ saved_config.repository_owner }}/{{ saved_config.repository_name }}/deployments/{{ saved_config.environment }}).
1421
{%- endif %}
15-
22+
{% elif saved_config.type == "GitLab" -%}
23+
This configuration allows the workflow file at https://gitlab.com/{{ saved_config.namespace }}/{{ saved_config.project }}/-/blob/HEAD/{{ saved_config.workflow_filepath }} to publish new versions of this crate.
24+
{%- if saved_config.environment %} The workflow must use the `{{ saved_config.environment }}` environment.
25+
{%- endif %}
26+
{% endif %}
1627
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.
1728

1829
If you are unable to revert the change and need to do so, you can email [email protected] for assistance.
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
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

18+
{% if config.type == "GitHub" -%}
1219
The removed configuration was for the workflow file at https://github.com/{{ config.repository_owner }}/{{ config.repository_name }}/blob/HEAD/.github/workflows/{{ config.workflow_filename }}
1320
{%- if config.environment %} using the `{{ config.environment }}` environment
1421
{%- endif -%}
1522
.
16-
23+
{% elif config.type == "GitLab" -%}
24+
The removed configuration was for the workflow file at https://gitlab.com/{{ config.namespace }}/{{ config.project }}/-/blob/HEAD/{{ config.workflow_filepath }}
25+
{%- if config.environment %} using the `{{ config.environment }}` environment
26+
{%- endif -%}
27+
.
28+
{% endif %}
1729
If you did not make this change and you think it was made maliciously, you can email [email protected] for assistance.
1830
{% endblock %}

0 commit comments

Comments
 (0)