Skip to content

Commit 042a6be

Browse files
committed
Add exclude-titles option to no-mentions
Allow ignoring some pull requests such as subtree updates, as they don't benefit from a no-mentions warning.
1 parent 6991b23 commit 042a6be

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

src/config.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,12 @@ pub(crate) struct IssueLinksConfig {
515515
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
516516
#[serde(rename_all = "kebab-case")]
517517
#[serde(deny_unknown_fields)]
518-
pub(crate) struct NoMentionsConfig {}
518+
pub(crate) struct NoMentionsConfig {
519+
/// The check will not be performed on titles that include any of these substrings (case
520+
/// insensitive)
521+
#[serde(default)]
522+
pub(crate) exclude_titles: Vec<String>,
523+
}
519524

520525
/// Configuration for PR behind commits checks
521526
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -681,6 +686,7 @@ mod tests {
681686
trigger-files = ["posts/"]
682687
683688
[no-mentions]
689+
exclude-titles = ["subtree update"]
684690
685691
[behind-upstream]
686692
days-threshold = 14
@@ -769,7 +775,9 @@ mod tests {
769775
issue_links: Some(IssueLinksConfig {
770776
check_commits: true,
771777
}),
772-
no_mentions: Some(NoMentionsConfig {}),
778+
no_mentions: Some(NoMentionsConfig {
779+
exclude_titles: vec!["subtree update".into()],
780+
}),
773781
behind_upstream: Some(BehindUpstreamConfig {
774782
days_threshold: Some(14),
775783
}),

src/handlers/check_commits.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> any
106106
}
107107

108108
if let Some(no_mentions) = &config.no_mentions {
109-
warnings.extend(no_mentions::mentions_in_commits(no_mentions, &commits));
109+
warnings.extend(no_mentions::mentions_in_commits(
110+
&event.issue.title,
111+
no_mentions,
112+
&commits,
113+
));
110114
}
111115

112116
if let Some(issue_links) = &config.issue_links {

src/handlers/check_commits/no_mentions.rs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
use crate::{config::NoMentionsConfig, github::GithubCommit};
55

66
pub(super) fn mentions_in_commits(
7-
_conf: &NoMentionsConfig,
7+
pr_title: &str,
8+
conf: &NoMentionsConfig,
89
commits: &[GithubCommit],
910
) -> Option<String> {
11+
if conf.exclude_titles.iter().any(|s| pr_title.contains(s)) {
12+
return None;
13+
}
14+
1015
let mentions_commits = commits
1116
.into_iter()
12-
.filter(|c| !parser::get_mentions(&c.commit.message).is_empty())
17+
.filter(|c| {
18+
let mentions = parser::get_mentions(&c.commit.message);
19+
!mentions.is_empty() && mentions.iter().any(|m| *m != "rustbot")
20+
})
1321
.map(|c| format!("- {}\n", c.sha))
1422
.collect::<String>();
1523

@@ -33,7 +41,14 @@ fn test_mentions_in_commits() {
3341
"This is simple without mentions!",
3442
)];
3543

36-
assert_eq!(mentions_in_commits(&NoMentionsConfig {}, &commits), None);
44+
let default_conf = NoMentionsConfig {
45+
exclude_titles: vec![],
46+
};
47+
48+
assert_eq!(
49+
mentions_in_commits("any title", &default_conf, &commits),
50+
None
51+
);
3752

3853
commits.push(dummy_commit_from_body(
3954
"10b96a74c484cae79164cbbcdfcd412109e0e4cf",
@@ -42,20 +57,51 @@ Signed-off-by: Foo Bar <[email protected]>
4257
Co-authored-by: Baz Qux <[email protected]>",
4358
));
4459

45-
assert_eq!(mentions_in_commits(&NoMentionsConfig {}, &commits), None);
60+
assert_eq!(
61+
mentions_in_commits("any title", &default_conf, &commits,),
62+
None
63+
);
4664

4765
commits.push(dummy_commit_from_body(
48-
"d7daa17bc97df9377640b0d33cbd0bbeed703c3a",
49-
"This is a body with a @mention!",
66+
"6565ffdd8af4ca0ec7c8faceee59c582edcd83b2",
67+
"This is a body that only mentions @rustbot for a command!",
68+
));
69+
70+
assert_eq!(
71+
mentions_in_commits("any title", &default_conf, &commits),
72+
None
73+
);
74+
75+
commits.push(dummy_commit_from_body(
76+
"6565ffdd8af4ca0ec7c8faceee59c582edcd83b2",
77+
"This is a body that mentions @rustbot for a command! And then a user @mention",
5078
));
5179

5280
assert_eq!(
53-
mentions_in_commits(&NoMentionsConfig {}, &commits),
81+
mentions_in_commits("any title", &default_conf, &commits, ),
5482
Some(
5583
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
5684
*Please remove the mentions to avoid spamming these users.*
57-
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
85+
- 6565ffdd8af4ca0ec7c8faceee59c582edcd83b2
5886
".to_string()
5987
)
6088
);
89+
90+
let _ = commits.pop(); // Remove that @rustbot & @mention case
91+
92+
commits.push(dummy_commit_from_body(
93+
"d7daa17bc97df9377640b0d33cbd0bbeed703c3a",
94+
"This is a body with a @mention!",
95+
));
96+
97+
assert_eq!(
98+
mentions_in_commits(
99+
"exclude this pull from checking ",
100+
&NoMentionsConfig {
101+
exclude_titles: vec![String::from("exclude this")]
102+
},
103+
&commits
104+
),
105+
None
106+
);
61107
}

0 commit comments

Comments
 (0)