Skip to content

Commit 0413959

Browse files
committed
Automatically add beta nominated to issues fixing regressions
... from stable to beta
1 parent dbbcb0f commit 0413959

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/github.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,20 @@ pub struct Repository {
749749
impl Repository {
750750
const GITHUB_API_URL: &'static str = "https://api.github.com";
751751

752+
pub async fn get_issue(&self, client: &GithubClient, id: u64) -> anyhow::Result<Issue> {
753+
let url = format!(
754+
"{}/repos/{}/issues/{}",
755+
Repository::GITHUB_API_URL,
756+
self.full_name,
757+
id
758+
);
759+
let result = client.get(&url);
760+
client
761+
.json(result)
762+
.await
763+
.with_context(|| format!("failed to get issue from {}", url))
764+
}
765+
752766
pub async fn get_issues<'a>(
753767
&self,
754768
client: &GithubClient,

src/handlers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl fmt::Display for HandlerError {
2525

2626
mod assign;
2727
mod autolabel;
28+
mod beta_backport;
2829
mod close;
2930
mod github_releases;
3031
mod glacier;
@@ -74,6 +75,14 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
7475
);
7576
}
7677

78+
if let Err(e) = beta_backport::handle(ctx, event).await {
79+
log::error!(
80+
"failed to process event {:?} with beta_backport handler: {:?}",
81+
event,
82+
e
83+
);
84+
}
85+
7786
if let Some(ghr_config) = config
7887
.as_ref()
7988
.ok()

src/handlers/beta_backport.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::github::{Event, IssuesAction, Label};
2+
use crate::handlers::Context;
3+
use regex::Regex;
4+
5+
lazy_static! {
6+
// See https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-issues/linking-a-pull-request-to-an-issue
7+
// Max 19 digits long to prevent u64 overflow
8+
static ref CLOSES_ISSUE: Regex = Regex::new("(close[sd]|fix(e[sd])?|resolve[sd]) #(\\d{1,19})").unwrap();
9+
}
10+
11+
pub(crate) async fn handle(
12+
ctx: &Context,
13+
event: &Event,
14+
) -> anyhow::Result<()> {
15+
let issue_event = if let Event::Issue(event) = event {
16+
event
17+
} else {
18+
return Ok(());
19+
};
20+
21+
if issue_event.action != IssuesAction::Opened {
22+
return Ok(());
23+
}
24+
25+
if issue_event.issue.pull_request.is_none() {
26+
return Ok(());
27+
}
28+
29+
for caps in CLOSES_ISSUE.captures_iter(&issue_event.issue.body) {
30+
// Should never fail due to the regex
31+
let issue_id = caps.get(1).unwrap().as_str().parse::<u64>().unwrap();
32+
let issue = issue_event
33+
.repository
34+
.get_issue(&ctx.github, issue_id)
35+
.await?;
36+
if issue.labels.contains(&Label {
37+
name: "regression-from-stable-to-beta".to_string(),
38+
}) {
39+
let mut labels = issue_event.issue.labels().to_owned();
40+
labels.push(Label {
41+
name: "beta-nominated".to_string(),
42+
});
43+
issue_event
44+
.issue
45+
.set_labels(&ctx.github, labels)
46+
.await?;
47+
break;
48+
}
49+
}
50+
51+
Ok(())
52+
}

0 commit comments

Comments
 (0)