-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathbackport.rs
More file actions
276 lines (250 loc) · 9.42 KB
/
Copy pathbackport.rs
File metadata and controls
276 lines (250 loc) · 9.42 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Handles stable, beta backports for PRs fixing P-high/critical regressions
//!
//! Add proper labels, opens a poll on Zulip to gauge interest about a backport.
//! Posts a closing messages on Zulip when the PR has been backport accepted.
//!
//! Configuration is done with the `[backport]` table.
//!
use std::collections::HashMap;
use std::sync::LazyLock;
use crate::config::BackportConfig;
use crate::github::{IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;
use anyhow::Context as AnyhowContext;
use futures::future::join_all;
use regex::Regex;
use tracing as log;
// See https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-issues/linking-a-pull-request-to-an-issue
// See tests to see what matches
static CLOSES_ISSUE_REGEXP: LazyLock<Regex> = LazyLock::new(|| {
Regex::new("(?i)(?P<action>close[sd]*|fix([e]*[sd]*)?|resolve[sd]*)(?P<spaces>:? +)(?P<org_repo>[a-zA-Z0-9_-]*/[a-zA-Z0-9_-]*)?#(?P<issue_num>[0-9]+)").unwrap()
});
const REGRESSION_LABELS: [&str; 3] = [
"regression-from-stable-to-nightly",
"regression-from-stable-to-beta",
"regression-from-stable-to-stable",
];
// auto-nominate for backport only patches fixing high/critical regressions
// For `P-{medium,low}` regressions, let the author decide
const PRIORITY_LABELS: [&str; 2] = ["P-high", "P-critical"];
#[derive(Default)]
pub(crate) struct BackportInput {
// Issue(s) fixed by this PR
ids: Vec<u64>,
// Handler configuration, it's a compound value of (required_issue_label -> add_labels)
labels: HashMap<String, Vec<String>>,
}
pub(super) async fn parse_input(
ctx: &Context,
event: &IssuesEvent,
config: Option<&BackportConfig>,
) -> Result<Option<BackportInput>, String> {
let Some(config) = config else {
return Ok(None);
};
// Only handle the event when the PR:
// - is opened (and not a draft)
// - is converted from draft to ready for review
// - when the first comment is edited
let skip_check = !matches!(
event.action,
IssuesAction::Opened | IssuesAction::Edited | IssuesAction::ReadyForReview
);
if skip_check || !event.issue.is_pr() || event.issue.draft {
log::debug!(
"Skipping backport event because: IssuesAction = {:?}, issue.is_pr() {}, draft = {}",
event.action,
event.issue.is_pr(),
event.issue.draft
);
return Ok(None);
}
let pr = &event.issue;
let pr_labels: Vec<&str> = pr.labels.iter().map(|l| l.name.as_str()).collect();
if let IssuesAction::Labeled { label } = &event.action {
if (label.name == "beta-nominated" && contains_any(&pr_labels, &["beta-accepted"]))
|| (label.name == "stable-nominated" && contains_any(&pr_labels, &["stable-accepted"]))
{
log::debug!(
"Will not nominate for backport, PR #{} is already backport accepted (found labels: {:?})",
pr.number,
pr_labels
);
let label_to_remove = vec![Label {
name: label.name.clone(),
}];
let _ = event
.issue
.remove_labels(&ctx.github, label_to_remove)
.await
.context("failed to remove labels from the issue");
return Ok(None);
}
}
// Retrieve backport config for this PR, based on its team label(s)
// If the PR has no team label matching any [backport.*.required-pr-labels] config, the backport labelling will be skipped
let mut input = BackportInput::default();
let valid_configs: Vec<_> = config
.configs
.iter()
.clone()
.filter(|(_cfg_name, cfg)| {
let required_pr_labels: Vec<&str> =
cfg.required_pr_labels.iter().map(String::as_str).collect();
if !contains_any(&pr_labels, &required_pr_labels) {
log::warn!(
"Skipping backport nomination: PR is missing the required labels. Labels found: {:?}",
pr_labels
);
return false;
}
input
.labels
.insert(cfg.required_issue_label.clone(), cfg.add_labels.clone());
true
})
.collect();
if valid_configs.is_empty() {
log::warn!(
"Skipping backport nomination: could not find a suitable backport config. Please ensure the triagebot.toml has a `[backport.*.required-pr-labels]` section matching the team label(s) for PR #{}.",
pr.number
);
return Ok(None);
}
// Check marker text in the opening comment of the PR to retrieve the issue(s) being fixed
for caps in CLOSES_ISSUE_REGEXP.captures_iter(&event.issue.body) {
let id = caps
.name("issue_num")
.ok_or_else(|| format!("failed to get issue_num from {caps:?}"))?
.as_str();
let id = match id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
return Err(format!("Failed to parse issue id `{id}`, error: {err}"));
}
};
if let Some(org_repo) = caps.name("org_repo")
&& org_repo.as_str() != event.repository.full_name
{
log::info!(
"Skipping backport nomination: Ignoring issue#{id} pointing to a different git repository: Expected {0}, found {org_repo:?}",
event.repository.full_name
);
continue;
}
input.ids.push(id);
}
if input.ids.is_empty() || input.labels.is_empty() {
return Ok(None);
}
log::debug!(
"Will handle event action {:?} in backport. Regression IDs found {:?}",
event.action,
input.ids
);
Ok(Some(input))
}
pub(super) async fn handle_input(
ctx: &Context,
_config: &BackportConfig,
event: &IssuesEvent,
input: BackportInput,
) -> anyhow::Result<()> {
let pr = &event.issue;
// Retrieve the issue(s) this pull request closes
let issues = input
.ids
.iter()
.copied()
.map(|id| async move { event.repository.get_issue(&ctx.github, id).await });
let issues = join_all(issues).await;
// Add backport nomination label to the pull request
for issue in issues {
if let Err(ref err) = issue {
log::warn!("Failed to get issue: {err:?}");
continue;
}
let issue = issue.context("failed to get issue")?;
let issue_labels: Vec<&str> = issue.labels.iter().map(|l| l.name.as_str()).collect();
// Check issue for a prerequisite priority label
// If none, skip this issue
if !contains_any(&issue_labels, &PRIORITY_LABELS) {
continue;
}
// Get the labels to be added the PR according to the matching (required) regression label
// that is found in the configuration that this handler has received
// If no regression label is found, skip this issue
let add_labels = issue_labels.iter().find_map(|l| input.labels.get(*l));
if add_labels.is_none() {
log::warn!(
"Skipping backport nomination: nothing to do for issue #{}. No config found for regression label ({:?})",
issue.number,
REGRESSION_LABELS
);
continue;
}
// Add backport nomination label(s) to PR
let mut new_labels = pr.labels().to_owned();
new_labels.extend(
add_labels
.expect("failed to unwrap add_labels")
.iter()
.cloned()
.map(|name| Label { name }),
);
log::debug!(
"PR#{} adding labels for backport {:?}",
pr.number,
add_labels
);
let _ = pr
.add_labels(&ctx.github, new_labels)
.await
.context("failed to add backport labels to the PR");
}
Ok(())
}
fn contains_any(haystack: &[&str], needles: &[&str]) -> bool {
needles.iter().any(|needle| haystack.contains(needle))
}
#[cfg(test)]
mod tests {
use crate::handlers::backport::CLOSES_ISSUE_REGEXP;
#[tokio::test]
async fn backport_match_comment() {
let test_strings = vec![
("close #10", vec![10]),
("closes #10", vec![10]),
("closed #10", vec![10]),
("Closes #10", vec![10]),
("close #10", vec![10]),
("close rust-lang/rust#10", vec![10]),
("cLose: rust-lang/rust#10", vec![10]),
("fix #10", vec![10]),
("fixes #10", vec![10]),
("fixed #10", vec![10]),
("resolve #10", vec![10]),
("resolves #10", vec![10]),
("resolved #10", vec![10]),
(
"Fixes #20, Resolves #21, closed #22, LOL #23",
vec![20, 21, 22],
),
("Resolved #10", vec![10]),
("Fixes #10", vec![10]),
("Closes #10", vec![10]),
];
for test_case in test_strings {
let mut ids: Vec<u64> = vec![];
let test_str = test_case.0;
let expected = test_case.1;
for caps in CLOSES_ISSUE_REGEXP.captures_iter(test_str) {
// eprintln!("caps {:?}", caps);
let id = &caps["issue_num"];
ids.push(id.parse::<u64>().unwrap());
}
// eprintln!("ids={:?}", ids);
assert_eq!(ids, expected);
}
}
}