Explorar o código

Support aliasing a pingable team in configuration

Mark Rousskov %!s(int64=5) %!d(string=hai) anos
pai
achega
fff46112f0
Modificáronse 2 ficheiros con 37 adicións e 16 borrados
  1. 20 2
      src/config.rs
  2. 17 14
      src/handlers/ping.rs

+ 20 - 2
src/config.rs

@@ -1,5 +1,5 @@
 use crate::github::GithubClient;
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 use std::fmt;
 use std::sync::{Arc, RwLock};
 use std::time::{Duration, Instant};
@@ -32,12 +32,30 @@ pub(crate) struct PingConfig {
     // team name -> message
     // message will have the cc string appended
     #[serde(flatten)]
-    pub(crate) teams: HashMap<String, PingTeamConfig>,
+    teams: HashMap<String, PingTeamConfig>,
+}
+
+impl PingConfig {
+    pub(crate) fn get_by_name(&self, team: &str) -> Option<(&str, &PingTeamConfig)> {
+        if let Some((team, cfg)) = self.teams.get_key_value(team) {
+            return Some((team, cfg));
+        }
+
+        for (name, cfg) in self.teams.iter() {
+            if cfg.alias.contains(team) {
+                return Some((name, cfg));
+            }
+        }
+
+        None
+    }
 }
 
 #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
 pub(crate) struct PingTeamConfig {
     pub(crate) message: String,
+    #[serde(default)]
+    pub(crate) alias: HashSet<String>,
     pub(crate) label: Option<String>,
 }
 

+ 17 - 14
src/handlers/ping.rs

@@ -83,19 +83,22 @@ async fn handle_input(
         return Ok(());
     }
 
-    if !config.teams.contains_key(&team_name) {
-        let cmnt = ErrorComment::new(
-            &event.issue().unwrap(),
-            format!(
-                "This team (`{}`) cannot be pinged via this command;\
+    let (gh_team, config) = match config.get_by_name(&team_name) {
+        Some(v) => v,
+        None => {
+            let cmnt = ErrorComment::new(
+                &event.issue().unwrap(),
+                format!(
+                    "This team (`{}`) cannot be pinged via this command;\
                  it may need to be added to `triagebot.toml` on the master branch.",
-                team_name,
-            ),
-        );
-        cmnt.post(&ctx.github).await?;
-        return Ok(());
-    }
-    let team = github::get_team(&ctx.github, &team_name).await?;
+                    team_name,
+                ),
+            );
+            cmnt.post(&ctx.github).await?;
+            return Ok(());
+        }
+    };
+    let team = github::get_team(&ctx.github, &gh_team).await?;
     let team = match team {
         Some(team) => team,
         None => {
@@ -111,7 +114,7 @@ async fn handle_input(
         }
     };
 
-    if let Some(label) = config.teams[&team_name].label.clone() {
+    if let Some(label) = config.label.clone() {
         let issue_labels = event.issue().unwrap().labels();
         if !issue_labels.iter().any(|l| l.name == label) {
             let mut issue_labels = issue_labels.to_owned();
@@ -145,7 +148,7 @@ async fn handle_input(
     } else {
         format!("cc {}", users.join(" "))
     };
-    let comment = format!("{}\n\n{}", config.teams[&team_name].message, ping_msg);
+    let comment = format!("{}\n\n{}", config.message, ping_msg);
     event
         .issue()
         .expect("issue")