Skip to content

Commit 842e2bd

Browse files
authored
Merge pull request #2206 from apiraino/revert-2172
Revert "Add alias for compound labels"
2 parents 1d62ded + 38f813e commit 842e2bd

File tree

4 files changed

+13
-178
lines changed

4 files changed

+13
-178
lines changed

parser/src/command/relabel.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ fn delta_empty() {
102102
}
103103

104104
impl RelabelCommand {
105-
/// Parse and validate command tokens
106105
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<Self>, Error<'a>> {
107106
let mut toks = input.clone();
108107

src/config.rs

Lines changed: 3 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::changelogs::ChangelogFormat;
22
use crate::github::{GithubClient, Repository};
3-
use parser::command::relabel::{Label, LabelDelta, RelabelCommand};
43
use std::collections::{HashMap, HashSet};
54
use std::fmt;
65
use std::sync::{Arc, LazyLock, RwLock};
@@ -251,64 +250,10 @@ pub(crate) struct MentionsEntryConfig {
251250

252251
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
253252
#[serde(rename_all = "kebab-case")]
253+
#[serde(deny_unknown_fields)]
254254
pub(crate) struct RelabelConfig {
255255
#[serde(default)]
256256
pub(crate) allow_unauthenticated: Vec<String>,
257-
// alias identifier -> labels
258-
#[serde(flatten)]
259-
pub(crate) aliases: HashMap<String, RelabelAliasConfig>,
260-
}
261-
262-
impl RelabelConfig {
263-
pub(crate) fn retrieve_command_from_alias(&self, input: RelabelCommand) -> RelabelCommand {
264-
let mut deltas = vec![];
265-
if !self.aliases.is_empty() {
266-
// parse all tokens: if one matches an alias, extract the labels
267-
// else, it will assumed to be a valid label
268-
for tk in input.0.into_iter() {
269-
let name = tk.label() as &str;
270-
if let Some(alias) = self.aliases.get(name) {
271-
let cmd = alias.to_command(matches!(tk, LabelDelta::Remove(_)));
272-
deltas.extend(cmd.0);
273-
} else {
274-
deltas.push(tk);
275-
}
276-
}
277-
}
278-
RelabelCommand(deltas)
279-
}
280-
}
281-
282-
#[derive(Default, PartialEq, Eq, Debug, serde::Deserialize)]
283-
#[serde(rename_all = "kebab-case")]
284-
#[serde(deny_unknown_fields)]
285-
pub(crate) struct RelabelAliasConfig {
286-
/// Labels to be added
287-
pub(crate) add_labels: Vec<String>,
288-
/// Labels to be removed
289-
pub(crate) rem_labels: Vec<String>,
290-
}
291-
292-
impl RelabelAliasConfig {
293-
/// Translate a RelabelAliasConfig into a RelabelCommand for GitHub consumption
294-
fn to_command(&self, inverted: bool) -> RelabelCommand {
295-
let mut deltas = Vec::new();
296-
let mut add_labels = &self.add_labels;
297-
let mut rem_labels = &self.rem_labels;
298-
299-
// if the polarity of the alias is inverted, swap labels before parsing the command
300-
if inverted {
301-
std::mem::swap(&mut add_labels, &mut rem_labels);
302-
}
303-
304-
for l in add_labels.iter() {
305-
deltas.push(LabelDelta::Add(Label(l.into())));
306-
}
307-
for l in rem_labels.iter() {
308-
deltas.push(LabelDelta::Remove(Label(l.into())));
309-
}
310-
RelabelCommand(deltas)
311-
}
312257
}
313258

314259
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -816,11 +761,11 @@ mod tests {
816761
817762
[mentions."src/"]
818763
cc = ["@someone"]
819-
764+
820765
[mentions."target/"]
821766
message = "This is a message."
822767
cc = ["@someone"]
823-
768+
824769
[mentions."#[rustc_attr]"]
825770
type = "content"
826771
message = "This is a message."
@@ -890,7 +835,6 @@ mod tests {
890835
Config {
891836
relabel: Some(RelabelConfig {
892837
allow_unauthenticated: vec!["C-*".into()],
893-
aliases: HashMap::new()
894838
}),
895839
assign: Some(AssignConfig {
896840
warn_non_default_branch: WarnNonDefaultBranchConfig::Simple(false),
@@ -1089,76 +1033,6 @@ mod tests {
10891033
);
10901034
}
10911035

1092-
#[test]
1093-
fn relabel_alias_config() {
1094-
let config = r#"
1095-
[relabel.to-stable]
1096-
add-labels = ["regression-from-stable-to-stable"]
1097-
rem-labels = ["regression-from-stable-to-beta", "regression-from-stable-to-nightly"]
1098-
"#;
1099-
let config = toml::from_str::<Config>(&config).unwrap();
1100-
1101-
let mut relabel_configs = HashMap::new();
1102-
relabel_configs.insert(
1103-
"to-stable".into(),
1104-
RelabelAliasConfig {
1105-
add_labels: vec!["regression-from-stable-to-stable".to_string()],
1106-
rem_labels: vec![
1107-
"regression-from-stable-to-beta".to_string(),
1108-
"regression-from-stable-to-nightly".to_string(),
1109-
],
1110-
},
1111-
);
1112-
1113-
let expected_cfg = RelabelConfig {
1114-
allow_unauthenticated: vec![],
1115-
aliases: relabel_configs,
1116-
};
1117-
1118-
assert_eq!(config.relabel, Some(expected_cfg));
1119-
}
1120-
1121-
#[test]
1122-
fn relabel_alias() {
1123-
// [relabel.my-alias]
1124-
// add-labels = ["Alpha"]
1125-
// rem-labels = ["Bravo", "Charlie"]
1126-
let relabel_cfg = RelabelConfig {
1127-
allow_unauthenticated: vec![],
1128-
aliases: HashMap::from([(
1129-
"my-alias".to_string(),
1130-
RelabelAliasConfig {
1131-
add_labels: vec!["Alpha".to_string()],
1132-
rem_labels: vec!["Bravo".to_string(), "Charlie".to_string()],
1133-
},
1134-
)]),
1135-
};
1136-
1137-
// @triagebot label my-alias
1138-
let deltas = vec![LabelDelta::Add(Label("my-alias".into()))];
1139-
let new_input = relabel_cfg.retrieve_command_from_alias(RelabelCommand(deltas));
1140-
assert_eq!(
1141-
new_input,
1142-
RelabelCommand(vec![
1143-
LabelDelta::Add(Label("Alpha".into())),
1144-
LabelDelta::Remove(Label("Bravo".into())),
1145-
LabelDelta::Remove(Label("Charlie".into())),
1146-
])
1147-
);
1148-
1149-
// @triagebot label -my-alias
1150-
let deltas = vec![LabelDelta::Remove(Label("my-alias".into()))];
1151-
let new_input = relabel_cfg.retrieve_command_from_alias(RelabelCommand(deltas));
1152-
assert_eq!(
1153-
new_input,
1154-
RelabelCommand(vec![
1155-
LabelDelta::Add(Label("Bravo".into())),
1156-
LabelDelta::Add(Label("Charlie".into())),
1157-
LabelDelta::Remove(Label("Alpha".into())),
1158-
])
1159-
);
1160-
}
1161-
11621036
#[test]
11631037
fn issue_links_uncanonicalized() {
11641038
let config = r#"
@@ -1219,36 +1093,4 @@ Multi text body with ${mcp_issue} and ${mcp_title}
12191093
})
12201094
);
12211095
}
1222-
1223-
#[test]
1224-
fn relabel_new_config() {
1225-
let config = r#"
1226-
[relabel]
1227-
allow-unauthenticated = ["ABCD-*"]
1228-
1229-
[relabel.to-stable]
1230-
add-labels = ["regression-from-stable-to-stable"]
1231-
rem-labels = ["regression-from-stable-to-beta", "regression-from-stable-to-nightly"]
1232-
"#;
1233-
let config = toml::from_str::<Config>(&config).unwrap();
1234-
1235-
let mut relabel_configs = HashMap::new();
1236-
relabel_configs.insert(
1237-
"to-stable".into(),
1238-
RelabelAliasConfig {
1239-
add_labels: vec!["regression-from-stable-to-stable".to_string()],
1240-
rem_labels: vec![
1241-
"regression-from-stable-to-beta".to_string(),
1242-
"regression-from-stable-to-nightly".to_string(),
1243-
],
1244-
},
1245-
);
1246-
1247-
let expected_cfg = RelabelConfig {
1248-
allow_unauthenticated: vec!["ABCD-*".to_string()],
1249-
aliases: relabel_configs,
1250-
};
1251-
1252-
assert_eq!(config.relabel, Some(expected_cfg));
1253-
}
12541096
}

src/github.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,9 @@ impl IssuesEvent {
13381338
}
13391339
}
13401340

1341+
#[derive(Debug, serde::Deserialize)]
1342+
struct PullRequestEventFields {}
1343+
13411344
#[derive(Debug, serde::Deserialize)]
13421345
pub struct WorkflowRunJob {
13431346
pub name: String,

src/handlers/relabel.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Purpose: Allow any user to modify labels on GitHub issues and pull requests via comments.
1+
//! Purpose: Allow any user to modify issue labels on GitHub via comments.
22
//!
3-
//! Labels are checked against the existing set in the git repository; the bot does not support
4-
//! creating new labels.
3+
//! Labels are checked against the labels in the project; the bot does not support creating new
4+
//! labels.
55
//!
66
//! Parsing is done in the `parser::command::relabel` module.
77
//!
@@ -27,17 +27,13 @@ pub(super) async fn handle_command(
2727
input: RelabelCommand,
2828
) -> anyhow::Result<()> {
2929
let Some(issue) = event.issue() else {
30-
return user_error!("Can only add and remove labels on issues and pull requests");
30+
return user_error!("Can only add and remove labels on an issue");
3131
};
3232

33-
// If the input matches a valid alias, read the [relabel] config.
34-
// if any alias matches, extract the alias config (RelabelAliasConfig) and build a new RelabelCommand.
35-
let new_input = config.retrieve_command_from_alias(input);
36-
3733
// Check label authorization for the current user
38-
for delta in &new_input.0 {
34+
for delta in &input.0 {
3935
let name = delta.label() as &str;
40-
let err = match check_filter(name, config, is_member(&event.user(), &ctx.team).await) {
36+
let err = match check_filter(name, config, is_member(event.user(), &ctx.team).await) {
4137
Ok(CheckFilterResult::Allow) => None,
4238
Ok(CheckFilterResult::Deny) => {
4339
Some(format!("Label {name} can only be set by Rust team members"))
@@ -48,15 +44,14 @@ pub(super) async fn handle_command(
4844
)),
4945
Err(err) => Some(err),
5046
};
51-
5247
if let Some(err) = err {
5348
// bail-out and inform the user why
5449
return user_error!(err);
5550
}
5651
}
5752

5853
// Compute the labels to add and remove
59-
let (to_add, to_remove) = compute_label_deltas(&new_input.0);
54+
let (to_add, to_remove) = compute_label_deltas(&input.0);
6055

6156
// Add labels
6257
if let Err(e) = issue.add_labels(&ctx.github, to_add.clone()).await {
@@ -108,8 +103,6 @@ enum CheckFilterResult {
108103
DenyUnknown,
109104
}
110105

111-
/// Check if the team member is allowed to apply labels
112-
/// configured in `allow_unauthenticated`
113106
fn check_filter(
114107
label: &str,
115108
config: &RelabelConfig,
@@ -201,7 +194,6 @@ fn compute_label_deltas(deltas: &[LabelDelta]) -> (Vec<Label>, Vec<Label>) {
201194
#[cfg(test)]
202195
mod tests {
203196
use parser::command::relabel::{Label, LabelDelta};
204-
use std::collections::HashMap;
205197

206198
use super::{
207199
CheckFilterResult, MatchPatternResult, TeamMembership, check_filter, compute_label_deltas,
@@ -240,7 +232,6 @@ mod tests {
240232
($($member:ident { $($label:expr => $res:ident,)* })*) => {
241233
let config = RelabelConfig {
242234
allow_unauthenticated: vec!["T-*".into(), "I-*".into(), "!I-*nominated".into()],
243-
aliases: HashMap::new()
244235
};
245236
$($(assert_eq!(
246237
check_filter($label, &config, TeamMembership::$member),

0 commit comments

Comments
 (0)