|
1 | 1 | //! Purpose: When opening a PR, or pushing new changes, check for github mentions |
2 | 2 | //! in commits and notify the user of our no-mentions in commits policy. |
3 | 3 |
|
4 | | -use std::fmt::Write; |
5 | | - |
6 | 4 | use crate::{config::NoMentionsConfig, github::GithubCommit}; |
7 | 5 |
|
8 | 6 | pub(super) fn mentions_in_commits( |
9 | 7 | _conf: &NoMentionsConfig, |
10 | 8 | commits: &[GithubCommit], |
11 | 9 | ) -> Option<String> { |
12 | | - let mut mentions_commits = Vec::new(); |
13 | | - |
14 | | - for commit in commits { |
15 | | - if !parser::get_mentions(&commit.commit.message).is_empty() { |
16 | | - mentions_commits.push(&*commit.sha); |
17 | | - } |
18 | | - } |
| 10 | + let mentions_commits = commits |
| 11 | + .into_iter() |
| 12 | + .filter(|c| !parser::get_mentions(&c.commit.message).is_empty()) |
| 13 | + .map(|c| format!(" - {}\n", c.sha)) |
| 14 | + .collect::<String>(); |
19 | 15 |
|
20 | 16 | if mentions_commits.is_empty() { |
21 | 17 | None |
22 | 18 | } else { |
23 | | - Some(mentions_in_commits_warn(mentions_commits)) |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -fn mentions_in_commits_warn(commits: Vec<&str>) -> String { |
28 | | - 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"); |
29 | | - |
30 | | - for commit in commits { |
31 | | - let _ = writeln!(warning, " - {commit}"); |
| 19 | + Some( |
| 20 | + String::from( |
| 21 | + r"There are username mentions (such as `@user`) in the commit messages of the following commits. |
| 22 | + *Please remove the mentions to avoid spamming these users.* |
| 23 | +", |
| 24 | + ) + &mentions_commits, |
| 25 | + ) |
32 | 26 | } |
33 | | - |
34 | | - warning |
35 | 27 | } |
36 | 28 |
|
37 | 29 | #[test] |
@@ -69,10 +61,10 @@ fn test_mentions_in_commits() { |
69 | 61 | assert_eq!( |
70 | 62 | mentions_in_commits(&NoMentionsConfig {}, &commits), |
71 | 63 | Some( |
72 | | - r#"There are username mentions (such as `@user`) in the commit messages of the following commits. |
| 64 | + r"There are username mentions (such as `@user`) in the commit messages of the following commits. |
73 | 65 | *Please remove the mentions to avoid spamming these users.* |
74 | 66 | - d7daa17bc97df9377640b0d33cbd0bbeed703c3a |
75 | | -"#.to_string() |
| 67 | +".to_string() |
76 | 68 | ) |
77 | 69 | ); |
78 | 70 | } |
0 commit comments