Skip to content

refactor: reduce allocations in no_mentions #1931

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 11, 2025
Merged
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
34 changes: 12 additions & 22 deletions src/handlers/check_commits/no_mentions.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
//! Purpose: When opening a PR, or pushing new changes, check for github mentions
//! in commits and notify the user of our no-mentions in commits policy.

use std::fmt::Write;

use crate::{config::NoMentionsConfig, github::GithubCommit};

pub(super) fn mentions_in_commits(
_conf: &NoMentionsConfig,
commits: &[GithubCommit],
) -> Option<String> {
let mut mentions_commits = Vec::new();

for commit in commits {
if !parser::get_mentions(&commit.commit.message).is_empty() {
mentions_commits.push(&*commit.sha);
}
}
let mentions_commits = commits
.into_iter()
.filter(|c| !parser::get_mentions(&c.commit.message).is_empty())
.map(|c| format!(" - {}\n", c.sha))
.collect::<String>();

if mentions_commits.is_empty() {
None
} else {
Some(mentions_in_commits_warn(mentions_commits))
}
}

fn mentions_in_commits_warn(commits: Vec<&str>) -> String {
let mut warning = format!("There are username mentions (such as `@user`) in the commit messages of the following commits.\n *Please remove the mentions to avoid spamming these users.*\n");

for commit in commits {
let _ = writeln!(warning, " - {commit}");
Some(format!(
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
*Please remove the mentions to avoid spamming these users.*
{mentions_commits}",
))
}

warning
}

#[test]
Expand Down Expand Up @@ -69,10 +59,10 @@ fn test_mentions_in_commits() {
assert_eq!(
mentions_in_commits(&NoMentionsConfig {}, &commits),
Some(
r#"There are username mentions (such as `@user`) in the commit messages of the following commits.
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
*Please remove the mentions to avoid spamming these users.*
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
"#.to_string()
".to_string()
)
);
}