|
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(format!( |
| 20 | + r"There are username mentions (such as `@user`) in the commit messages of the following commits. |
| 21 | + *Please remove the mentions to avoid spamming these users.* |
| 22 | +{mentions_commits}", |
| 23 | + )) |
32 | 24 | }
|
33 |
| - |
34 |
| - warning |
35 | 25 | }
|
36 | 26 |
|
37 | 27 | #[test]
|
@@ -69,10 +59,10 @@ fn test_mentions_in_commits() {
|
69 | 59 | assert_eq!(
|
70 | 60 | mentions_in_commits(&NoMentionsConfig {}, &commits),
|
71 | 61 | Some(
|
72 |
| - r#"There are username mentions (such as `@user`) in the commit messages of the following commits. |
| 62 | + r"There are username mentions (such as `@user`) in the commit messages of the following commits. |
73 | 63 | *Please remove the mentions to avoid spamming these users.*
|
74 | 64 | - d7daa17bc97df9377640b0d33cbd0bbeed703c3a
|
75 |
| -"#.to_string() |
| 65 | +".to_string() |
76 | 66 | )
|
77 | 67 | );
|
78 | 68 | }
|
0 commit comments