@@ -2,20 +2,25 @@ use std::sync::LazyLock;
2
2
3
3
use regex:: Regex ;
4
4
5
- use crate :: { config:: IssueLinksConfig , github:: GithubCommit } ;
5
+ use crate :: {
6
+ config:: { IssueLinksCheckCommitsConfig , IssueLinksConfig } ,
7
+ github:: GithubCommit ,
8
+ } ;
6
9
7
10
static 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 ( ) ) ;
9
12
10
13
const MERGE_IGNORE_LIST : [ & str ; 3 ] = [ "Rollup merge of " , "Auto merge of " , "Merge pull request " ] ;
11
14
12
15
pub ( super ) fn issue_links_in_commits (
13
16
conf : & IssueLinksConfig ,
14
17
commits : & [ GithubCommit ] ,
15
18
) -> 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
+ } ;
19
24
20
25
let issue_links_commits = commits
21
26
. into_iter ( )
@@ -24,12 +29,21 @@ pub(super) fn issue_links_in_commits(
24
29
. iter ( )
25
30
. any ( |i| c. commit . message . starts_with ( i) )
26
31
} )
27
- . filter ( |c| LINKED_RE . is_match ( & c. commit . message ) )
32
+ . filter ( |c| does_match ( & c. commit . message ) )
28
33
. map ( |c| format ! ( "- {}\n " , c. sha) )
29
34
. collect :: < String > ( ) ;
30
35
31
36
if issue_links_commits. is_empty ( ) {
32
37
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
+ ) )
33
47
} else {
34
48
Some ( format ! (
35
49
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(
39
53
}
40
54
}
41
55
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
+
42
67
#[ test]
43
68
fn test_mentions_in_commits ( ) {
44
69
use super :: dummy_commit_from_body;
45
70
46
71
let config = IssueLinksConfig {
47
- check_commits : true ,
72
+ check_commits : IssueLinksCheckCommitsConfig :: All ,
48
73
} ;
49
74
50
75
let mut commits = vec ! [ dummy_commit_from_body(
@@ -87,7 +112,7 @@ fn test_mentions_in_commits() {
87
112
assert_eq ! (
88
113
issue_links_in_commits(
89
114
& IssueLinksConfig {
90
- check_commits: false ,
115
+ check_commits: IssueLinksCheckCommitsConfig :: Off ,
91
116
} ,
92
117
& commits
93
118
) ,
@@ -110,3 +135,41 @@ fn test_mentions_in_commits() {
110
135
)
111
136
) ;
112
137
}
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