Skip to content

Commit 69e801f

Browse files
committed
further work
1 parent fdf5a4a commit 69e801f

File tree

5 files changed

+61
-24
lines changed

5 files changed

+61
-24
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(Debug, PartialEq, Eq)]
15+
#[derive(Clone, Debug, PartialEq, Eq)]
1616
pub enum LabelDelta {
1717
Add(Label),
1818
Remove(Label),

parser/src/lib.rs

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

78
pub use ignore_block::replace_all_outside_ignore_blocks;
89
pub use mentions::get_mentions;

src/config.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,27 +260,33 @@ pub(crate) struct RelabelConfig {
260260

261261
impl RelabelConfig {
262262
pub(crate) fn retrieve_command_from_alias(&self, input: RelabelCommand) -> RelabelCommand {
263+
let mut deltas = vec![];
263264
match &self.configs {
264265
Some(configs) => {
265-
dbg!(&configs);
266-
// get only the first token from the command
267-
// extract the "alias" from the RelabelCommand
266+
// parse all tokens: if one matches an alias, extract the labels
267+
// else, it will assumed to be a valid label
268268
if input.0.len() > 0 {
269-
let name = input.0.get(0).unwrap();
270-
let name = name.label().as_str();
271-
// check if this alias matches any RelabelRuleConfig key in our config
272-
// extract the labels and build a new command
273-
if configs.contains_key(name) {
274-
let (_alias, cfg) = configs.get_key_value(name).unwrap();
275-
return cfg.to_command();
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+
}
276281
}
277282
}
278283
}
279284
None => {
285+
// nothing to do, return the original command
280286
return input;
281287
}
282288
};
283-
input
289+
RelabelCommand(deltas)
284290
}
285291
}
286292

@@ -807,11 +813,11 @@ mod tests {
807813
808814
[mentions."src/"]
809815
cc = ["@someone"]
810-
816+
811817
[mentions."target/"]
812818
message = "This is a message."
813819
cc = ["@someone"]
814-
820+
815821
[mentions."#[rustc_attr]"]
816822
type = "content"
817823
message = "This is a message."
@@ -1080,6 +1086,35 @@ mod tests {
10801086
);
10811087
}
10821088

1089+
#[test]
1090+
fn relabel_alias_config() {
1091+
let config = r#"
1092+
[relabel.to-stable]
1093+
add-labels = ["regression-from-stable-to-stable"]
1094+
rem-labels = ["regression-from-stable-to-beta", "regression-from-stable-to-nightly"]
1095+
"#;
1096+
let config = toml::from_str::<Config>(&config).unwrap();
1097+
1098+
let mut relabel_configs = HashMap::new();
1099+
relabel_configs.insert(
1100+
"to-stable".into(),
1101+
RelabelRuleConfig {
1102+
add_labels: vec!["regression-from-stable-to-stable".to_string()],
1103+
rem_labels: vec![
1104+
"regression-from-stable-to-beta".to_string(),
1105+
"regression-from-stable-to-nightly".to_string(),
1106+
],
1107+
},
1108+
);
1109+
1110+
let expected_cfg = RelabelConfig {
1111+
allow_unauthenticated: vec![],
1112+
configs: Some(relabel_configs),
1113+
};
1114+
1115+
assert_eq!(config.relabel, Some(expected_cfg));
1116+
}
1117+
10831118
#[test]
10841119
fn issue_links_uncanonicalized() {
10851120
let config = r#"

src/github.rs

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

1343-
#[derive(Debug, serde::Deserialize)]
1344-
struct PullRequestEventFields {}
1345-
13461343
#[derive(Debug, serde::Deserialize)]
13471344
pub struct WorkflowRunJob {
13481345
pub name: String,

src/handlers/relabel.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Purpose: Allow any user to modify issue labels on GitHub via comments.
1+
//! Purpose: Allow any user to modify labels on GitHub issues and pull requests via comments.
22
//!
3-
//! Labels are checked against the labels in the project; the bot does not support creating new
4-
//! labels.
3+
//! Labels are checked against the existing set in the git repository; the bot does not support
4+
//! creating new labels.
55
//!
66
//! Parsing is done in the `parser::command::relabel` module.
77
//!
@@ -27,11 +27,15 @@ 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 an issue");
30+
return user_error!("Can only add and remove labels on issues and pull requests");
3131
};
3232

33+
// 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.
35+
let new_input = config.retrieve_command_from_alias(input);
36+
3337
// Check label authorization for the current user
34-
for delta in &input.0 {
38+
for delta in &new_input.0 {
3539
let name = delta.label().as_str();
3640
let err = match check_filter(name, config, is_member(&event.user(), &ctx.team).await) {
3741
Ok(CheckFilterResult::Allow) => None,
@@ -53,7 +57,7 @@ pub(super) async fn handle_command(
5357
}
5458

5559
// Compute the labels to add and remove
56-
let (to_add, to_remove) = compute_label_deltas(&input.0);
60+
let (to_add, to_remove) = compute_label_deltas(&new_input.0);
5761

5862
// Add labels
5963
if let Err(e) = issue.add_labels(&ctx.github, to_add.clone()).await {

0 commit comments

Comments
 (0)