@@ -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
261261impl 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) ]
778766mod 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#"
0 commit comments