|
| 1 | +use std::sync::LazyLock; |
| 2 | + |
| 3 | +use regex::Regex; |
| 4 | + |
| 5 | +use crate::{config::CanonicalizeIssueLinksConfig, github::GithubCommit}; |
| 6 | + |
| 7 | +static LINKED_RE: LazyLock<Regex> = |
| 8 | + LazyLock::new(|| Regex::new(r"( |^)([a-zA-Z-_]+/[a-zA-Z-_]+)?(#[0-9]+)\b").unwrap()); |
| 9 | + |
| 10 | +pub(super) fn issue_links_in_commits( |
| 11 | + _conf: &CanonicalizeIssueLinksConfig, |
| 12 | + commits: &[GithubCommit], |
| 13 | +) -> Option<String> { |
| 14 | + let issue_links_commits = commits |
| 15 | + .into_iter() |
| 16 | + .filter(|c| LINKED_RE.is_match(&c.commit.message)) |
| 17 | + .map(|c| format!(" - {}\n", c.sha)) |
| 18 | + .collect::<String>(); |
| 19 | + |
| 20 | + if issue_links_commits.is_empty() { |
| 21 | + None |
| 22 | + } else { |
| 23 | + Some(format!( |
| 24 | + r"There are issue links (such as `#123`) in the commit messages of the following commits. |
| 25 | + *Please remove them as they will spam the issue with references to the commit.* |
| 26 | +{issue_links_commits}", |
| 27 | + )) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn test_mentions_in_commits() { |
| 33 | + use super::dummy_commit_from_body; |
| 34 | + |
| 35 | + let config = CanonicalizeIssueLinksConfig {}; |
| 36 | + |
| 37 | + let mut commits = vec![dummy_commit_from_body( |
| 38 | + "d1992a392617dfb10518c3e56446b6c9efae38b0", |
| 39 | + "This is simple without issue links!", |
| 40 | + )]; |
| 41 | + |
| 42 | + assert_eq!(issue_links_in_commits(&config, &commits), None); |
| 43 | + |
| 44 | + commits.push(dummy_commit_from_body( |
| 45 | + "d7daa17bc97df9377640b0d33cbd0bbeed703c3a", |
| 46 | + "This is a body with a issue link #123.", |
| 47 | + )); |
| 48 | + |
| 49 | + assert_eq!( |
| 50 | + issue_links_in_commits(&config, &commits), |
| 51 | + Some( |
| 52 | + r"There are issue links (such as `#123`) in the commit messages of the following commits. |
| 53 | + *Please remove them as they will spam the issue with references to the commit.* |
| 54 | + - d7daa17bc97df9377640b0d33cbd0bbeed703c3a |
| 55 | +".to_string() |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + commits.push(dummy_commit_from_body( |
| 60 | + "891f0916a07c215ae8173f782251422f1fea6acb", |
| 61 | + "This is a body with a issue link rust-lang/rust#123.", |
| 62 | + )); |
| 63 | + |
| 64 | + assert_eq!( |
| 65 | + issue_links_in_commits(&config, &commits), |
| 66 | + Some( |
| 67 | + r"There are issue links (such as `#123`) in the commit messages of the following commits. |
| 68 | + *Please remove them as they will spam the issue with references to the commit.* |
| 69 | + - d7daa17bc97df9377640b0d33cbd0bbeed703c3a |
| 70 | + - 891f0916a07c215ae8173f782251422f1fea6acb |
| 71 | +".to_string() |
| 72 | + ) |
| 73 | + ); |
| 74 | +} |
0 commit comments