-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathissue_links.rs
More file actions
174 lines (147 loc) · 5.62 KB
/
Copy pathissue_links.rs
File metadata and controls
174 lines (147 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::sync::LazyLock;
use regex::Regex;
use crate::{
config::{IssueLinksCheckCommitsConfig, IssueLinksConfig},
github::GithubCommit,
handlers::check_commits::MERGE_IGNORE_LIST,
};
static LINKED_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\B(?P<org>[a-zA-Z-_]+/[a-zA-Z-_]+)?(#[0-9]+)\b").unwrap());
pub(super) fn issue_links_in_commits(
conf: &IssueLinksConfig,
commits: &[GithubCommit],
) -> Option<String> {
let does_match = match conf.check_commits {
IssueLinksCheckCommitsConfig::Off => return None,
IssueLinksCheckCommitsConfig::All => has_issue_link,
IssueLinksCheckCommitsConfig::Uncanonicalized => has_uncanonicalized_issue_link,
};
let issue_links_commits = commits
.iter()
.filter(|c| {
!MERGE_IGNORE_LIST
.iter()
.any(|i| c.commit.message.starts_with(i))
})
.filter(|c| does_match(&c.commit.message))
.map(|c| format!("- {}\n", c.sha))
.collect::<String>();
if issue_links_commits.is_empty() {
None
} else if matches!(
conf.check_commits,
IssueLinksCheckCommitsConfig::Uncanonicalized
) {
Some(format!(
r"There are uncanonicalized issue links (such as `#123`) in the commit messages of the following commits.
*Please add the organization and repository before the issue number (like so `rust-lang/rust#123`) to avoid issues with subtree.*
{issue_links_commits}",
))
} else {
Some(format!(
r"There are issue links (such as `#123`) in the commit messages of the following commits.
*Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.*
{issue_links_commits}",
))
}
}
fn has_issue_link(text: &str) -> bool {
LINKED_RE.is_match(text)
}
fn has_uncanonicalized_issue_link(text: &str) -> bool {
let Some(caps) = LINKED_RE.captures(text) else {
return false;
};
caps.name("org").is_none()
}
#[test]
fn test_mentions_in_commits() {
use super::dummy_commit_from_body;
let config = IssueLinksConfig {
check_commits: IssueLinksCheckCommitsConfig::All,
};
let mut commits = vec![dummy_commit_from_body(
"d1992a392617dfb10518c3e56446b6c9efae38b0",
"This is simple without issue links!",
)];
assert_eq!(issue_links_in_commits(&config, &commits), None);
commits.push(dummy_commit_from_body(
"86176475acda9c775f844f5ad2470f05aebd4249",
"Rollup merge of #123\n\nWe ignore the issue link for Rollup merge of",
));
commits.push(dummy_commit_from_body(
"8009423d53d30b56d8cf0fec08f9852329a1a9a4",
"Auto merge of #123\n\nWe ignore the issue link for Auto merge of",
));
commits.push(dummy_commit_from_body(
"1eeacf822f6c11cd10713ddcb54a72352cacb2c2",
"Merge pull request #2236 from rust-lang/rustc-pull",
));
assert_eq!(issue_links_in_commits(&config, &commits), None);
commits.push(dummy_commit_from_body(
"d7daa17bc97df9377640b0d33cbd0bbeed703c3a",
"This is a body with a issue link #123.",
));
assert_eq!(
issue_links_in_commits(&config, &commits),
Some(
r"There are issue links (such as `#123`) in the commit messages of the following commits.
*Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.*
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
".to_string()
)
);
assert_eq!(
issue_links_in_commits(
&IssueLinksConfig {
check_commits: IssueLinksCheckCommitsConfig::Off,
},
&commits
),
None
);
commits.push(dummy_commit_from_body(
"891f0916a07c215ae8173f782251422f1fea6acb",
"This is a body with a issue link (rust-lang/rust#123).",
));
assert_eq!(
issue_links_in_commits(&config, &commits),
Some(
r"There are issue links (such as `#123`) in the commit messages of the following commits.
*Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.*
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
- 891f0916a07c215ae8173f782251422f1fea6acb
".to_string()
)
);
}
#[test]
fn uncanonicalized() {
use super::dummy_commit_from_body;
let config = IssueLinksConfig {
check_commits: IssueLinksCheckCommitsConfig::Uncanonicalized,
};
let mut commits = vec![dummy_commit_from_body(
"d1992a392617dfb10518c3e56446b6c9efae38b0",
"This is simple without issue links!",
)];
assert_eq!(issue_links_in_commits(&config, &commits), None);
commits.push(dummy_commit_from_body(
"86176475acda9c775f844f5ad2470f05aebd4249",
"Test for canonicalized rust-lang/rust#123",
));
assert_eq!(issue_links_in_commits(&config, &commits), None);
commits.push(dummy_commit_from_body(
"fererfe5acda9c775f844f5ad2470f05aebd4249",
"Test for uncanonicalized #123",
));
assert_eq!(
issue_links_in_commits(&config, &commits),
Some(
r"There are uncanonicalized issue links (such as `#123`) in the commit messages of the following commits.
*Please add the organization and repository before the issue number (like so `rust-lang/rust#123`) to avoid issues with subtree.*
- fererfe5acda9c775f844f5ad2470f05aebd4249
".to_string()
)
);
}