|
1 | 1 | use crate::changelogs::ChangelogFormat;
|
2 | 2 | use crate::github::{GithubClient, Repository};
|
| 3 | +use parser::command::relabel::{Label, LabelDelta, RelabelCommand}; |
3 | 4 | use std::collections::{HashMap, HashSet};
|
4 | 5 | use std::fmt;
|
5 | 6 | use std::sync::{Arc, LazyLock, RwLock};
|
@@ -238,10 +239,62 @@ pub(crate) struct MentionsPathConfig {
|
238 | 239 |
|
239 | 240 | #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
|
240 | 241 | #[serde(rename_all = "kebab-case")]
|
241 |
| -#[serde(deny_unknown_fields)] |
242 | 242 | pub(crate) struct RelabelConfig {
|
243 | 243 | #[serde(default)]
|
244 | 244 | pub(crate) allow_unauthenticated: Vec<String>,
|
| 245 | + // alias identifier -> labels |
| 246 | + #[serde(flatten)] |
| 247 | + pub(crate) configs: Option<HashMap<String, RelabelRuleConfig>>, |
| 248 | +} |
| 249 | + |
| 250 | +impl RelabelConfig { |
| 251 | + pub(crate) fn retrieve_command_from_alias(&self, input: RelabelCommand) -> RelabelCommand { |
| 252 | + match &self.configs { |
| 253 | + Some(configs) => { |
| 254 | + dbg!(&configs); |
| 255 | + // get only the first token from the command |
| 256 | + // extract the "alias" from the RelabelCommand |
| 257 | + if input.0.len() > 0 { |
| 258 | + let name = input.0.get(0).unwrap(); |
| 259 | + let name = name.label().as_str(); |
| 260 | + // check if this alias matches any RelabelRuleConfig key in our config |
| 261 | + // extract the labels and build a new command |
| 262 | + if configs.contains_key(name) { |
| 263 | + let (_alias, cfg) = configs.get_key_value(name).unwrap(); |
| 264 | + return cfg.to_command(); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + None => { |
| 269 | + return input; |
| 270 | + } |
| 271 | + }; |
| 272 | + input |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +#[derive(Default, PartialEq, Eq, Debug, serde::Deserialize)] |
| 277 | +#[serde(rename_all = "kebab-case")] |
| 278 | +#[serde(deny_unknown_fields)] |
| 279 | +pub(crate) struct RelabelRuleConfig { |
| 280 | + /// Labels to be added |
| 281 | + pub(crate) add_labels: Vec<String>, |
| 282 | + /// Labels to be removed |
| 283 | + pub(crate) rem_labels: Vec<String>, |
| 284 | +} |
| 285 | + |
| 286 | +impl RelabelRuleConfig { |
| 287 | + /// Translate a RelabelRuleConfig into a RelabelCommand for GitHub consumption |
| 288 | + pub fn to_command(&self) -> RelabelCommand { |
| 289 | + let mut deltas = Vec::new(); |
| 290 | + for l in self.add_labels.iter() { |
| 291 | + deltas.push(LabelDelta::Add(Label(l.into()))); |
| 292 | + } |
| 293 | + for l in self.rem_labels.iter() { |
| 294 | + deltas.push(LabelDelta::Remove(Label(l.into()))); |
| 295 | + } |
| 296 | + RelabelCommand(deltas) |
| 297 | + } |
245 | 298 | }
|
246 | 299 |
|
247 | 300 | #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
|
@@ -756,6 +809,7 @@ mod tests {
|
756 | 809 | Config {
|
757 | 810 | relabel: Some(RelabelConfig {
|
758 | 811 | allow_unauthenticated: vec!["C-*".into()],
|
| 812 | + configs: Some(HashMap::new()) |
759 | 813 | }),
|
760 | 814 | assign: Some(AssignConfig {
|
761 | 815 | warn_non_default_branch: WarnNonDefaultBranchConfig::Simple(false),
|
@@ -926,4 +980,36 @@ mod tests {
|
926 | 980 | })
|
927 | 981 | );
|
928 | 982 | }
|
| 983 | + |
| 984 | + #[test] |
| 985 | + fn relabel_new_config() { |
| 986 | + let config = r#" |
| 987 | + [relabel] |
| 988 | + allow-unauthenticated = ["ABCD-*"] |
| 989 | +
|
| 990 | + [relabel.to-stable] |
| 991 | + add-labels = ["regression-from-stable-to-stable"] |
| 992 | + rem-labels = ["regression-from-stable-to-beta", "regression-from-stable-to-nightly"] |
| 993 | + "#; |
| 994 | + let config = toml::from_str::<Config>(&config).unwrap(); |
| 995 | + |
| 996 | + let mut relabel_configs = HashMap::new(); |
| 997 | + relabel_configs.insert( |
| 998 | + "to-stable".into(), |
| 999 | + RelabelRuleConfig { |
| 1000 | + add_labels: vec!["regression-from-stable-to-stable".to_string()], |
| 1001 | + rem_labels: vec![ |
| 1002 | + "regression-from-stable-to-beta".to_string(), |
| 1003 | + "regression-from-stable-to-nightly".to_string(), |
| 1004 | + ], |
| 1005 | + }, |
| 1006 | + ); |
| 1007 | + |
| 1008 | + let expected_cfg = RelabelConfig { |
| 1009 | + allow_unauthenticated: vec!["ABCD-*".to_string()], |
| 1010 | + configs: Some(relabel_configs), |
| 1011 | + }; |
| 1012 | + |
| 1013 | + assert_eq!(config.relabel, Some(expected_cfg)); |
| 1014 | + } |
929 | 1015 | }
|
0 commit comments