Skip to content

Commit

Permalink
Support aliasing a pingable team in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Mar 12, 2020
1 parent 63d4ba4 commit fff4611
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
22 changes: 20 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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>,
}

Expand Down
31 changes: 17 additions & 14 deletions src/handlers/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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();
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit fff4611

Please sign in to comment.