Skip to content

Commit abcc0c0

Browse files
committed
Add warning against issue links in commits
1 parent 891f091 commit abcc0c0

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/handlers/check_commits.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
#[cfg(test)]
1111
use crate::github::GithubCommit;
1212

13+
mod canonicalize_issue_links;
1314
mod modified_submodule;
1415
mod no_mentions;
1516
mod non_default_branch;
@@ -67,6 +68,13 @@ pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> any
6768
warnings.extend(no_mentions::mentions_in_commits(no_mentions, &commits));
6869
}
6970

71+
if let Some(canonicalize_issue_links) = &config.canonicalize_issue_links {
72+
warnings.extend(canonicalize_issue_links::issue_links_in_commits(
73+
canonicalize_issue_links,
74+
&commits,
75+
));
76+
}
77+
7078
handle_warnings(ctx, event, warnings).await
7179
}
7280

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)