Skip to content

Commit

Permalink
Rework beta_backport handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mibac138 committed Jul 11, 2021
1 parent f07a5db commit 3e4588a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) struct Config {
pub(crate) notify_zulip: Option<NotifyZulipConfig>,
pub(crate) github_releases: Option<GitHubReleasesConfig>,
pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
pub(crate) beta_backport: Option<BetaBackportConfig>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
Expand Down Expand Up @@ -112,6 +113,13 @@ pub(crate) struct AutolabelLabelConfig {
pub(crate) exclude_labels: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
pub(crate) struct BetaBackportConfig {
pub(crate) trigger_labels: Vec<String>,
#[serde(default)]
pub(crate) labels_to_add: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
pub(crate) struct NotifyZulipConfig {
#[serde(flatten)]
Expand Down Expand Up @@ -298,6 +306,7 @@ mod tests {
notify_zulip: None,
github_releases: None,
review_submitted: None,
beta_backport: None,
}
);
}
Expand Down
1 change: 1 addition & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ macro_rules! issue_handlers {
// Each module in the list must contain the functions `parse_input` and `handle_input`.
issue_handlers! {
autolabel,
beta_backport,
major_change,
notify_zulip,
}
Expand Down
105 changes: 69 additions & 36 deletions src/handlers/beta_backport.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
use crate::github::{Event, IssuesAction, Label};
use crate::config::BetaBackportConfig;
use crate::github::{IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;
use regex::Regex;

lazy_static! {
// See https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-issues/linking-a-pull-request-to-an-issue
// Max 19 digits long to prevent u64 overflow
static ref CLOSES_ISSUE: Regex = Regex::new("(close[sd]|fix(e[sd])?|resolve[sd]) #(\\d{1,19})").unwrap();
static ref CLOSES_ISSUE: Regex = Regex::new("(close[sd]|fix(e[sd])?|resolve[sd]) #(\\d+)").unwrap();
}

pub(crate) async fn handle(
ctx: &Context,
event: &Event,
) -> anyhow::Result<()> {
let issue_event = if let Event::Issue(event) = event {
event
} else {
return Ok(());
};

if issue_event.action != IssuesAction::Opened {
return Ok(());
pub(crate) struct BetaBackportInput {
ids: Vec<u64>,
}

pub(crate) fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
config: Option<&BetaBackportConfig>,
) -> Result<Option<BetaBackportInput>, String> {
if config.is_none() {
return Ok(None);
}

if issue_event.issue.pull_request.is_none() {
return Ok(());
if event.action != IssuesAction::Opened {
return Ok(None);
}

for caps in CLOSES_ISSUE.captures_iter(&issue_event.issue.body) {
// Should never fail due to the regex
let issue_id = caps.get(1).unwrap().as_str().parse::<u64>().unwrap();
let issue = issue_event
.repository
.get_issue(&ctx.github, issue_id)
.await?;
if issue.labels.contains(&Label {
name: "regression-from-stable-to-beta".to_string(),
}) {
let mut labels = issue_event.issue.labels().to_owned();
labels.push(Label {
name: "beta-nominated".to_string(),
});
issue_event
.issue
.set_labels(&ctx.github, labels)
.await?;
break;
if event.issue.pull_request.is_none() {
return Ok(None);
}

let mut ids = vec![];
for caps in CLOSES_ISSUE.captures_iter(&event.issue.body) {
let id = caps.get(1).unwrap().as_str();
let id = match id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
return Err(format!("Failed to parse issue id `{}`, error: {}", id, err));
}
};
ids.push(id);
}

return Ok(Some(BetaBackportInput { ids }));
}

pub(super) async fn handle_input(
ctx: &Context,
config: &BetaBackportConfig,
event: &IssuesEvent,
input: BetaBackportInput,
) -> anyhow::Result<()> {
let mut issues = input
.ids
.iter()
.copied()
.map(|id| async move { event.repository.get_issue(&ctx.github, id).await });

let trigger_labels: Vec<_> = config
.trigger_labels
.iter()
.cloned()
.map(|name| Label { name })
.collect();
while let Some(issue) = issues.next() {
let issue = issue.await.unwrap();
if issue
.labels
.iter()
.any(|issue_label| trigger_labels.contains(issue_label))
{
let mut new_labels = event.issue.labels().to_owned();
new_labels.extend(
config
.labels_to_add
.iter()
.cloned()
.map(|name| Label { name }),
);
return event.issue.set_labels(&ctx.github, new_labels).await;
}
}

Expand Down

0 comments on commit 3e4588a

Please sign in to comment.