Skip to content

Commit a38ad42

Browse files
committed
fixup
1 parent 69e801f commit a38ad42

File tree

4 files changed

+72
-35
lines changed

4 files changed

+72
-35
lines changed

parser/src/command/relabel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt;
1212
#[derive(Debug, PartialEq, Eq)]
1313
pub struct RelabelCommand(pub Vec<LabelDelta>);
1414

15-
#[derive(Clone, Debug, PartialEq, Eq)]
15+
#[derive(Debug, PartialEq, Eq)]
1616
pub enum LabelDelta {
1717
Add(Label),
1818
Remove(Label),

parser/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub mod command;
22
pub mod error;
33
mod ignore_block;
44
mod mentions;
5-
// XXX: is this necessary?
65
pub mod token;
76

87
pub use ignore_block::replace_all_outside_ignore_blocks;

src/config.rs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -255,53 +255,41 @@ pub(crate) struct RelabelConfig {
255255
pub(crate) allow_unauthenticated: Vec<String>,
256256
// alias identifier -> labels
257257
#[serde(flatten)]
258-
pub(crate) configs: Option<HashMap<String, RelabelRuleConfig>>,
258+
pub(crate) aliases: HashMap<String, RelabelAliasConfig>,
259259
}
260260

261261
impl RelabelConfig {
262262
pub(crate) fn retrieve_command_from_alias(&self, input: RelabelCommand) -> RelabelCommand {
263263
let mut deltas = vec![];
264-
match &self.configs {
265-
Some(configs) => {
266-
// parse all tokens: if one matches an alias, extract the labels
267-
// else, it will assumed to be a valid label
268-
if input.0.len() > 0 {
269-
for tk in input.0.iter() {
270-
let name = tk.label().as_str();
271-
if configs.contains_key(name) {
272-
if let Some((_, cfg)) = configs.get_key_value(name) {
273-
let cmd = cfg.to_command();
274-
for d in cmd.0 {
275-
deltas.push(d);
276-
}
277-
}
278-
} else {
279-
deltas.push(tk.clone());
280-
}
281-
}
264+
if !self.aliases.is_empty() {
265+
// parse all tokens: if one matches an alias, extract the labels
266+
// else, it will assumed to be a valid label
267+
for tk in input.0.into_iter() {
268+
let name = tk.label().as_str();
269+
if let Some(alias) = self.aliases.get(name) {
270+
let cmd = alias.to_command();
271+
deltas.extend(cmd.0);
272+
} else {
273+
deltas.push(tk);
282274
}
283275
}
284-
None => {
285-
// nothing to do, return the original command
286-
return input;
287-
}
288-
};
276+
}
289277
RelabelCommand(deltas)
290278
}
291279
}
292280

293281
#[derive(Default, PartialEq, Eq, Debug, serde::Deserialize)]
294282
#[serde(rename_all = "kebab-case")]
295283
#[serde(deny_unknown_fields)]
296-
pub(crate) struct RelabelRuleConfig {
284+
pub(crate) struct RelabelAliasConfig {
297285
/// Labels to be added
298286
pub(crate) add_labels: Vec<String>,
299287
/// Labels to be removed
300288
pub(crate) rem_labels: Vec<String>,
301289
}
302290

303-
impl RelabelRuleConfig {
304-
/// Translate a RelabelRuleConfig into a RelabelCommand for GitHub consumption
291+
impl RelabelAliasConfig {
292+
/// Translate a RelabelAliasConfig into a RelabelCommand for GitHub consumption
305293
pub fn to_command(&self) -> RelabelCommand {
306294
let mut deltas = Vec::new();
307295
for l in self.add_labels.iter() {
@@ -777,6 +765,8 @@ where
777765
#[cfg(test)]
778766
mod tests {
779767
use super::*;
768+
use parser::error::Error;
769+
use parser::token::Tokenizer;
780770

781771
#[test]
782772
fn sample() {
@@ -887,7 +877,7 @@ mod tests {
887877
Config {
888878
relabel: Some(RelabelConfig {
889879
allow_unauthenticated: vec!["C-*".into()],
890-
configs: Some(HashMap::new())
880+
aliases: HashMap::new()
891881
}),
892882
assign: Some(AssignConfig {
893883
warn_non_default_branch: WarnNonDefaultBranchConfig::Simple(false),
@@ -1098,7 +1088,7 @@ mod tests {
10981088
let mut relabel_configs = HashMap::new();
10991089
relabel_configs.insert(
11001090
"to-stable".into(),
1101-
RelabelRuleConfig {
1091+
RelabelAliasConfig {
11021092
add_labels: vec!["regression-from-stable-to-stable".to_string()],
11031093
rem_labels: vec![
11041094
"regression-from-stable-to-beta".to_string(),
@@ -1109,12 +1099,59 @@ mod tests {
11091099

11101100
let expected_cfg = RelabelConfig {
11111101
allow_unauthenticated: vec![],
1112-
configs: Some(relabel_configs),
1102+
aliases: relabel_configs,
11131103
};
11141104

11151105
assert_eq!(config.relabel, Some(expected_cfg));
11161106
}
11171107

1108+
#[cfg(test)]
1109+
fn parse<'a>(input: &'a str) -> Result<Option<Vec<LabelDelta>>, Error<'a>> {
1110+
let mut toks = Tokenizer::new(input);
1111+
Ok(RelabelCommand::parse(&mut toks)?.map(|c| c.0))
1112+
}
1113+
1114+
#[test]
1115+
fn relabel_alias() {
1116+
// [relabel.my-alias]
1117+
// add-labels = ["Alpha"]
1118+
// rem-labels = ["Bravo", "Charlie"]
1119+
let relabel_cfg = RelabelConfig {
1120+
allow_unauthenticated: vec![],
1121+
aliases: HashMap::from([(
1122+
"my-alias".to_string(),
1123+
RelabelAliasConfig {
1124+
add_labels: vec!["Alpha".to_string()],
1125+
rem_labels: vec!["Bravo".to_string(), "Charlie".to_string()],
1126+
},
1127+
)]),
1128+
};
1129+
1130+
// @triagebot label my-alias
1131+
let deltas = parse("label my-alias").unwrap().unwrap();
1132+
let new_input = relabel_cfg.retrieve_command_from_alias(RelabelCommand(deltas));
1133+
assert_eq!(
1134+
new_input,
1135+
RelabelCommand(vec![
1136+
LabelDelta::Add(Label("Alpha".into())),
1137+
LabelDelta::Remove(Label("Bravo".into())),
1138+
LabelDelta::Remove(Label("Charlie".into())),
1139+
])
1140+
);
1141+
1142+
// @triagebot label -my-alias
1143+
let deltas = parse("label -my-alias").unwrap().unwrap();
1144+
let new_input = relabel_cfg.retrieve_command_from_alias(RelabelCommand(deltas));
1145+
assert_eq!(
1146+
new_input,
1147+
RelabelCommand(vec![
1148+
LabelDelta::Remove(Label("Alpha".into())),
1149+
LabelDelta::Add(Label("Bravo".into())),
1150+
LabelDelta::Add(Label("Charlie".into())),
1151+
])
1152+
);
1153+
}
1154+
11181155
#[test]
11191156
fn issue_links_uncanonicalized() {
11201157
let config = r#"

src/handlers/relabel.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ pub(super) async fn handle_command(
3131
};
3232

3333
// If the input matches a valid alias, read the [relabel] config.
34-
// if any alias matches, extract the alias config (RelabelRuleConfig) and build a new RelabelCommand.
34+
// if any alias matches, extract the alias config (RelabelAliasConfig) and build a new RelabelCommand.
3535
let new_input = config.retrieve_command_from_alias(input);
3636

3737
// Check label authorization for the current user
3838
for delta in &new_input.0 {
39-
let name = delta.label().as_str();
39+
let name = delta.label() as &str;
4040
let err = match check_filter(name, config, is_member(&event.user(), &ctx.team).await) {
4141
Ok(CheckFilterResult::Allow) => None,
4242
Ok(CheckFilterResult::Deny) => Some(format!(
@@ -204,6 +204,7 @@ fn compute_label_deltas(deltas: &[LabelDelta]) -> (Vec<Label>, Vec<Label>) {
204204
#[cfg(test)]
205205
mod tests {
206206
use parser::command::relabel::{Label, LabelDelta};
207+
use std::collections::HashMap;
207208

208209
use super::{
209210
CheckFilterResult, MatchPatternResult, TeamMembership, check_filter, compute_label_deltas,
@@ -242,7 +243,7 @@ mod tests {
242243
($($member:ident { $($label:expr => $res:ident,)* })*) => {
243244
let config = RelabelConfig {
244245
allow_unauthenticated: vec!["T-*".into(), "I-*".into(), "!I-*nominated".into()],
245-
configs: None
246+
aliases: HashMap::new()
246247
};
247248
$($(assert_eq!(
248249
check_filter($label, &config, TeamMembership::$member),

0 commit comments

Comments
 (0)