diff --git a/src/handlers/check_commits/no_mentions.rs b/src/handlers/check_commits/no_mentions.rs index cefa9d464..1c80d4c6c 100644 --- a/src/handlers/check_commits/no_mentions.rs +++ b/src/handlers/check_commits/no_mentions.rs @@ -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 { - 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::(); 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] @@ -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() ) ); }