diff --git a/Cargo.toml b/Cargo.toml index 5519b49d..11593ac8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,9 @@ chrono = "0.4" itertools = "0.14" +# Text processing +regex = "1" + [dev-dependencies] insta = "1.26" wiremock = "0.6" diff --git a/src/bors/handlers/trybuild.rs b/src/bors/handlers/trybuild.rs index 501721de..4d010bfd 100644 --- a/src/bors/handlers/trybuild.rs +++ b/src/bors/handlers/trybuild.rs @@ -20,6 +20,7 @@ use crate::github::{ CommitSha, GithubUser, LabelTrigger, MergeError, PullRequest, PullRequestNumber, }; use crate::permissions::PermissionType; +use crate::utils::text::suppress_github_mentions; use super::deny_request; use super::has_permission; @@ -286,7 +287,7 @@ fn auto_merge_commit_message( {pr_message}"#, pr_label = pr.head_label, pr_title = pr.title, - pr_message = pr.message, + pr_message = suppress_github_mentions(&pr.message), repo_owner = name.owner(), repo_name = name.name() ); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e601d1fb..a33e94a2 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,3 @@ pub mod logging; +pub mod text; pub mod timing; diff --git a/src/utils/text.rs b/src/utils/text.rs new file mode 100644 index 00000000..b5b96e65 --- /dev/null +++ b/src/utils/text.rs @@ -0,0 +1,29 @@ +use regex::{Captures, Regex}; + +/// Replaces github @mentions with backticks to prevent accidental pings +pub fn suppress_github_mentions(text: &str) -> String { + if !text.contains('@') { + return text.to_string(); + } + + let pattern = r"\B(@\S+)"; + + let re = Regex::new(pattern).unwrap(); + re.replace_all(text, |caps: &Captures| format!("`{}`", &caps[1])) + .to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_suppress_github_mentions() { + assert_eq!(suppress_github_mentions("r? @matklad\n"), "r? `@matklad`\n"); + assert_eq!(suppress_github_mentions("@bors r+\n"), "`@bors` r+\n"); + assert_eq!( + suppress_github_mentions("mail@example.com"), + "mail@example.com" + ) + } +}