@@ -2,20 +2,25 @@ use std::sync::LazyLock;
22
33use regex:: Regex ;
44
5- use crate :: { config:: IssueLinksConfig , github:: GithubCommit } ;
5+ use crate :: {
6+ config:: { IssueLinksCheckCommitsConfig , IssueLinksConfig } ,
7+ github:: GithubCommit ,
8+ } ;
69
710static LINKED_RE : LazyLock < Regex > =
8- LazyLock :: new ( || Regex :: new ( r"\B([a-zA-Z-_]+/[a-zA-Z-_]+)?(#[0-9]+)\b" ) . unwrap ( ) ) ;
11+ LazyLock :: new ( || Regex :: new ( r"\B(?P<org> [a-zA-Z-_]+/[a-zA-Z-_]+)?(#[0-9]+)\b" ) . unwrap ( ) ) ;
912
1013const MERGE_IGNORE_LIST : [ & str ; 3 ] = [ "Rollup merge of " , "Auto merge of " , "Merge pull request " ] ;
1114
1215pub ( super ) fn issue_links_in_commits (
1316 conf : & IssueLinksConfig ,
1417 commits : & [ GithubCommit ] ,
1518) -> Option < String > {
16- if !conf. check_commits {
17- return None ;
18- }
19+ let does_match = match conf. check_commits {
20+ IssueLinksCheckCommitsConfig :: Off => return None ,
21+ IssueLinksCheckCommitsConfig :: All => has_issue_link,
22+ IssueLinksCheckCommitsConfig :: Uncanonicalized => has_uncanonicalized_issue_link,
23+ } ;
1924
2025 let issue_links_commits = commits
2126 . into_iter ( )
@@ -24,12 +29,21 @@ pub(super) fn issue_links_in_commits(
2429 . iter ( )
2530 . any ( |i| c. commit . message . starts_with ( i) )
2631 } )
27- . filter ( |c| LINKED_RE . is_match ( & c. commit . message ) )
32+ . filter ( |c| does_match ( & c. commit . message ) )
2833 . map ( |c| format ! ( "- {}\n " , c. sha) )
2934 . collect :: < String > ( ) ;
3035
3136 if issue_links_commits. is_empty ( ) {
3237 None
38+ } else if matches ! (
39+ conf. check_commits,
40+ IssueLinksCheckCommitsConfig :: Uncanonicalized
41+ ) {
42+ Some ( format ! (
43+ r"There are uncanonicalized issue links (such as `#123`) in the commit messages of the following commits.
44+ *Please add the organization and repository before the issue number (like so `rust-lang/rust#123`) to avoid issues with subtree.*
45+ {issue_links_commits}" ,
46+ ) )
3347 } else {
3448 Some ( format ! (
3549 r"There are issue links (such as `#123`) in the commit messages of the following commits.
@@ -39,12 +53,23 @@ pub(super) fn issue_links_in_commits(
3953 }
4054}
4155
56+ fn has_issue_link ( text : & str ) -> bool {
57+ LINKED_RE . is_match ( text)
58+ }
59+
60+ fn has_uncanonicalized_issue_link ( text : & str ) -> bool {
61+ let Some ( caps) = LINKED_RE . captures ( text) else {
62+ return false ;
63+ } ;
64+ caps. name ( "org" ) . is_none ( )
65+ }
66+
4267#[ test]
4368fn test_mentions_in_commits ( ) {
4469 use super :: dummy_commit_from_body;
4570
4671 let config = IssueLinksConfig {
47- check_commits : true ,
72+ check_commits : IssueLinksCheckCommitsConfig :: All ,
4873 } ;
4974
5075 let mut commits = vec ! [ dummy_commit_from_body(
@@ -87,7 +112,7 @@ fn test_mentions_in_commits() {
87112 assert_eq ! (
88113 issue_links_in_commits(
89114 & IssueLinksConfig {
90- check_commits: false ,
115+ check_commits: IssueLinksCheckCommitsConfig :: Off ,
91116 } ,
92117 & commits
93118 ) ,
@@ -110,3 +135,41 @@ fn test_mentions_in_commits() {
110135 )
111136 ) ;
112137}
138+
139+ #[ test]
140+ fn uncanonicalized ( ) {
141+ use super :: dummy_commit_from_body;
142+
143+ let config = IssueLinksConfig {
144+ check_commits : IssueLinksCheckCommitsConfig :: Uncanonicalized ,
145+ } ;
146+
147+ let mut commits = vec ! [ dummy_commit_from_body(
148+ "d1992a392617dfb10518c3e56446b6c9efae38b0" ,
149+ "This is simple without issue links!" ,
150+ ) ] ;
151+
152+ assert_eq ! ( issue_links_in_commits( & config, & commits) , None ) ;
153+
154+ commits. push ( dummy_commit_from_body (
155+ "86176475acda9c775f844f5ad2470f05aebd4249" ,
156+ "Test for canonicalized rust-lang/rust#123" ,
157+ ) ) ;
158+
159+ assert_eq ! ( issue_links_in_commits( & config, & commits) , None ) ;
160+
161+ commits. push ( dummy_commit_from_body (
162+ "fererfe5acda9c775f844f5ad2470f05aebd4249" ,
163+ "Test for uncanonicalized #123" ,
164+ ) ) ;
165+
166+ assert_eq ! (
167+ issue_links_in_commits( & config, & commits) ,
168+ Some (
169+ r"There are uncanonicalized issue links (such as `#123`) in the commit messages of the following commits.
170+ *Please add the organization and repository before the issue number (like so `rust-lang/rust#123`) to avoid issues with subtree.*
171+ - fererfe5acda9c775f844f5ad2470f05aebd4249
172+ " . to_string( )
173+ )
174+ ) ;
175+ }
0 commit comments