Skip to content

Commit 1f068bf

Browse files
committed
feat: Prevent spam from GitHub mentions in merge commits
1 parent 680a300 commit 1f068bf

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ chrono = "0.4"
5454

5555
itertools = "0.14"
5656

57+
# Text processing
58+
regex = "1"
59+
5760
[dev-dependencies]
5861
insta = "1.26"
5962
wiremock = "0.6"

src/bors/handlers/trybuild.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::github::{
2020
CommitSha, GithubUser, LabelTrigger, MergeError, PullRequest, PullRequestNumber,
2121
};
2222
use crate::permissions::PermissionType;
23+
use crate::utils::text::suppress_github_mentions;
2324

2425
use super::deny_request;
2526
use super::has_permission;
@@ -286,7 +287,7 @@ fn auto_merge_commit_message(
286287
{pr_message}"#,
287288
pr_label = pr.head_label,
288289
pr_title = pr.title,
289-
pr_message = pr.message,
290+
pr_message = suppress_github_mentions(&pr.message),
290291
repo_owner = name.owner(),
291292
repo_name = name.name()
292293
);

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod logging;
2+
pub mod text;
23
pub mod timing;

src/utils/text.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use regex::{Captures, Regex};
2+
3+
/// Replaces github @mentions with backticks to prevent accidental pings
4+
pub fn suppress_github_mentions(text: &str) -> String {
5+
if !text.contains('@') {
6+
return text.to_string();
7+
}
8+
9+
let pattern = r"\B(@\S+)";
10+
11+
let re = Regex::new(pattern).unwrap();
12+
re.replace_all(text, |caps: &Captures| format!("`{}`", &caps[1]))
13+
.to_string()
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn test_suppress_github_mentions() {
22+
assert_eq!(suppress_github_mentions("r? @matklad\n"), "r? `@matklad`\n");
23+
assert_eq!(suppress_github_mentions("@bors r+\n"), "`@bors` r+\n");
24+
assert_eq!(
25+
suppress_github_mentions("[email protected]"),
26+
27+
)
28+
}
29+
}

0 commit comments

Comments
 (0)