Skip to content

Commit 60ff73d

Browse files
committed
Move no_mentions handler to being a sub-handler of check_commits
1 parent d1992a3 commit 60ff73d

File tree

4 files changed

+62
-160
lines changed

4 files changed

+62
-160
lines changed

src/handlers.rs

-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ mod major_change;
3737
mod mentions;
3838
mod merge_conflicts;
3939
mod milestone_prs;
40-
mod no_mentions;
4140
mod no_merges;
4241
mod nominate;
4342
mod note;
@@ -234,7 +233,6 @@ issue_handlers! {
234233
review_requested,
235234
pr_tracking,
236235
validate_config,
237-
no_mentions,
238236
}
239237

240238
macro_rules! command_handlers {

src/handlers/check_commits.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
};
99

1010
mod modified_submodule;
11+
mod no_mentions;
1112
mod non_default_branch;
1213

1314
/// Key for the state in the database
@@ -41,6 +42,7 @@ pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> any
4142
event.issue.number
4243
)
4344
};
45+
let commits = event.issue.commits(&ctx.github).await?;
4446

4547
let mut warnings = Vec::new();
4648

@@ -58,6 +60,10 @@ pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> any
5860
warnings.extend(modified_submodule::modifies_submodule(diff));
5961
}
6062

63+
if let Some(no_mentions) = &config.no_mentions {
64+
warnings.extend(no_mentions::mentions_in_commits(no_mentions, &commits));
65+
}
66+
6167
handle_warnings(ctx, event, warnings).await
6268
}
6369

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Purpose: When opening a PR, or pushing new changes, check for github mentions
2+
//! in commits and notify the user of our no-mentions in commits policy.
3+
4+
use std::fmt::Write;
5+
6+
use crate::{config::NoMentionsConfig, github::GithubCommit};
7+
8+
pub(super) fn mentions_in_commits(
9+
_conf: &NoMentionsConfig,
10+
commits: &[GithubCommit],
11+
) -> Option<String> {
12+
let mut mentions_commits = Vec::new();
13+
14+
for commit in commits {
15+
if !parser::get_mentions(&commit.commit.message).is_empty() {
16+
mentions_commits.push(&*commit.sha);
17+
}
18+
}
19+
20+
if mentions_commits.is_empty() {
21+
None
22+
} else {
23+
Some(mentions_in_commits_warn(mentions_commits))
24+
}
25+
}
26+
27+
fn mentions_in_commits_warn(commits: Vec<&str>) -> String {
28+
let mut warning =
29+
format!("There are mentions (`@user`) in the following commits, please remove them.\n");
30+
31+
for commit in commits {
32+
let _ = writeln!(warning, " - {commit}");
33+
}
34+
35+
warning
36+
}
37+
38+
#[test]
39+
fn test_warning_printing() {
40+
let commits_to_warn = vec![
41+
"4d6ze57403udfrzefrfe6574",
42+
"f54efz57405u46z6ef465z4f6ze57",
43+
"404u57403uzf5fe5f4f5e57405u4zf",
44+
];
45+
46+
let msg = mentions_in_commits_warn(commits_to_warn);
47+
48+
assert_eq!(
49+
msg,
50+
r#"There are mentions (`@user`) in the following commits, please remove them.
51+
- 4d6ze57403udfrzefrfe6574
52+
- f54efz57405u46z6ef465z4f6ze57
53+
- 404u57403uzf5fe5f4f5e57405u4zf
54+
"#
55+
);
56+
}

src/handlers/no_mentions.rs

-158
This file was deleted.

0 commit comments

Comments
 (0)