From 24668d2f369aa3bf883dd29e689668ffb0415a80 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sat, 12 Nov 2022 17:10:01 -0800 Subject: [PATCH] Don't ping everyone on rebase mistakes --- src/config.rs | 44 +++++++++++++++++++++++++++++++ src/github.rs | 1 + src/handlers/mentions.rs | 57 +++++++++++++++++++++++++++------------- 3 files changed, 84 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 053aa6821..11aca6b68 100644 --- a/src/config.rs +++ b/src/config.rs @@ -105,7 +105,9 @@ pub(crate) struct NoteConfig { } #[derive(PartialEq, Eq, Debug, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] pub(crate) struct MentionsConfig { + pub(crate) bors_commit_message: Option, #[serde(flatten)] pub(crate) paths: HashMap, } @@ -398,4 +400,46 @@ mod tests { } ); } + + #[test] + fn mentions_config() { + let config = r#" + [mentions."some_other_path"] + message = "foo" + cc = ["@someone"] + + [mentions] + bors-commit-message = "has bors commit" + "#; + let config = toml::from_str::(config).unwrap().mentions.unwrap(); + assert!(config.paths.len() == 1); + assert_eq!( + config.bors_commit_message.as_deref(), + Some("has bors commit") + ); + + let config = r#" + [mentions] + bors-commit-message = "has bors commit" + + [mentions."some_other_path"] + message = "foo" + cc = ["@someone"] + "#; + let config = toml::from_str::(config).unwrap().mentions.unwrap(); + assert!(config.paths.len() == 1); + assert_eq!( + config.bors_commit_message.as_deref(), + Some("has bors commit") + ); + + let config = r#" + [mentions."some_other_path"] + message = "foo" + cc = ["@someone"] + "#; + let config = toml::from_str::(config).unwrap().mentions.unwrap(); + assert!(config.paths.len() == 1); + assert_eq!(config.bors_commit_message, None); + } } diff --git a/src/github.rs b/src/github.rs index 295db3728..41cd5d464 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1594,6 +1594,7 @@ pub struct GitCommit { #[derive(Debug, serde::Deserialize)] pub struct GitUser { + pub name: String, pub date: DateTime, } diff --git a/src/handlers/mentions.rs b/src/handlers/mentions.rs index 1c888beae..bb3f84f54 100644 --- a/src/handlers/mentions.rs +++ b/src/handlers/mentions.rs @@ -16,8 +16,9 @@ use tracing as log; const MENTIONS_KEY: &str = "mentions"; -pub(super) struct MentionsInput { - paths: Vec, +pub(super) enum MentionsInput { + HasBorsCommit, + Paths(Vec), } #[derive(Debug, Default, Deserialize, Serialize)] @@ -50,6 +51,19 @@ pub(super) async fn parse_input( return Ok(None); } + // Don't ping if a bors commit is included - send a warning message instead + if event + .issue + .commits(&ctx.github) + .await + .map_err(|e| log::error!("failed to fetch commits: {:?}", e)) + .unwrap_or_default() + .into_iter() + .any(|commit| commit.commit.author.name == "bors") + { + return Ok(Some(MentionsInput::HasBorsCommit)); + } + if let Some(diff) = event .issue .diff(&ctx.github) @@ -78,7 +92,7 @@ pub(super) async fn parse_input( .map(|(key, _mention)| key.to_string()) .collect(); if !to_mention.is_empty() { - return Ok(Some(MentionsInput { paths: to_mention })); + return Ok(Some(MentionsInput::Paths(to_mention))); } } Ok(None) @@ -95,23 +109,30 @@ pub(super) async fn handle_input( IssueData::load(&mut client, &event.issue, MENTIONS_KEY).await?; // Build the message to post to the issue. let mut result = String::new(); - for to_mention in &input.paths { - if state.data.paths.iter().any(|p| p == to_mention) { - // Avoid duplicate mentions. - continue; - } - let MentionsPathConfig { message, cc } = &config.paths[to_mention]; - if !result.is_empty() { - result.push_str("\n\n"); - } - match message { - Some(m) => result.push_str(m), - None => write!(result, "Some changes occurred in {to_mention}").unwrap(), + match input { + MentionsInput::HasBorsCommit => { + result = config.bors_commit_message.clone().unwrap_or_default(); } - if !cc.is_empty() { - write!(result, "\n\ncc {}", cc.join(", ")).unwrap(); + MentionsInput::Paths(paths) => { + for to_mention in &paths { + if state.data.paths.iter().any(|p| p == to_mention) { + // Avoid duplicate mentions. + continue; + } + let MentionsPathConfig { message, cc } = &config.paths[to_mention]; + if !result.is_empty() { + result.push_str("\n\n"); + } + match message { + Some(m) => result.push_str(m), + None => write!(result, "Some changes occurred in {to_mention}").unwrap(), + } + if !cc.is_empty() { + write!(result, "\n\ncc {}", cc.join(", ")).unwrap(); + } + state.data.paths.push(to_mention.to_string()); + } } - state.data.paths.push(to_mention.to_string()); } if !result.is_empty() { event