Skip to content

Prevent spam from GitHub mentions in merge commits #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ chrono = "0.4"

itertools = "0.14"

# Text processing
regex = "1"

[dev-dependencies]
insta = "1.26"
wiremock = "0.6"
Expand Down
3 changes: 2 additions & 1 deletion src/bors/handlers/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod logging;
pub mod text;
pub mod timing;
29 changes: 29 additions & 0 deletions src/utils/text.rs
Original file line number Diff line number Diff line change
@@ -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("[email protected]"),
"[email protected]"
)
}
}