Skip to content

Commit 7cf67bc

Browse files
committed
fixup
1 parent 69e801f commit 7cf67bc

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ pub mod command;
22
pub mod error;
33
mod ignore_block;
44
mod mentions;
5-
// XXX: is this necessary?
6-
pub mod token;
5+
mod token;
76

87
pub use ignore_block::replace_all_outside_ignore_blocks;
98
pub use mentions::get_mentions;

src/config.rs

Lines changed: 18 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() {
@@ -887,7 +875,7 @@ mod tests {
887875
Config {
888876
relabel: Some(RelabelConfig {
889877
allow_unauthenticated: vec!["C-*".into()],
890-
configs: Some(HashMap::new())
878+
aliases: HashMap::new()
891879
}),
892880
assign: Some(AssignConfig {
893881
warn_non_default_branch: WarnNonDefaultBranchConfig::Simple(false),
@@ -1098,7 +1086,7 @@ mod tests {
10981086
let mut relabel_configs = HashMap::new();
10991087
relabel_configs.insert(
11001088
"to-stable".into(),
1101-
RelabelRuleConfig {
1089+
RelabelAliasConfig {
11021090
add_labels: vec!["regression-from-stable-to-stable".to_string()],
11031091
rem_labels: vec![
11041092
"regression-from-stable-to-beta".to_string(),
@@ -1109,7 +1097,7 @@ mod tests {
11091097

11101098
let expected_cfg = RelabelConfig {
11111099
allow_unauthenticated: vec![],
1112-
configs: Some(relabel_configs),
1100+
aliases: relabel_configs,
11131101
};
11141102

11151103
assert_eq!(config.relabel, Some(expected_cfg));

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)