Skip to content

Commit

Permalink
Merge pull request #66 from mainmatter/dedupe-config
Browse files Browse the repository at this point in the history
Dedupe config
  • Loading branch information
BobrImperator authored Mar 2, 2024
2 parents 5061dff + c290f12 commit 2bec675
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 13 deletions.
142 changes: 129 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::path::Path;
pub enum CliContext {
TWIOS,
COMMENT,
UTILITY,
}

#[derive(Debug)]
Expand All @@ -26,9 +27,10 @@ pub struct Args {
pub date: String,
pub date_sign: String,
pub config_path: String,
pub context: String,
pub context: CliContext,
pub comment_body: String,
pub edit: bool,
pub dedupe: bool,
}

#[cfg_attr(test, derive(PartialEq))]
Expand Down Expand Up @@ -73,6 +75,7 @@ pub struct AppParams {
pub output_path: String,
pub context: CliContext,
pub comment_body: String,
pub dedupe: bool,
}

#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -104,12 +107,6 @@ impl AppParams {
pub fn args() -> (AppParams, Option<FileConfig>) {
let args = process_args(read_args());

let cli_context = if args.context == "twios_comment" {
CliContext::COMMENT
} else {
CliContext::TWIOS
};

let now = chrono::offset::Utc::now();
let last_week = chrono::offset::Utc::now()
.checked_sub_days(Days::new(7))
Expand Down Expand Up @@ -141,9 +138,10 @@ pub fn args() -> (AppParams, Option<FileConfig>) {
date_sign: args.date_sign,
config_path: args.config_path,
output_path: file_config.output_path.clone(),
context: cli_context,
context: args.context,
comment_body: args.comment_body,
query_type: file_config.query_type.clone(),
dedupe: args.dedupe,
},
Some(file_config),
)
Expand Down Expand Up @@ -174,9 +172,10 @@ pub fn args() -> (AppParams, Option<FileConfig>) {
date_sign: args.date_sign,
config_path: args.config_path,
output_path: "".to_string(),
context: cli_context,
context: args.context,
comment_body: "".to_string(),
query_type: PullRequestQueryType::default(),
dedupe: args.dedupe,
},
None,
)
Expand Down Expand Up @@ -208,15 +207,19 @@ fn process_args(pairs: Vec<Arg>) -> Args {
date: String::from(""),
date_sign: String::from(""),
config_path: String::from(""),
context: String::from(""),
context: CliContext::TWIOS,
comment_body: String::from(""),
edit: false,
dedupe: false,
};

for pair in pairs {
match (pair.0.as_str(), pair.1.as_str()) {
("comment", _value) => {
args.context = "twios_comment".to_string();
args.context = CliContext::COMMENT;
}
("utility", _value) => {
args.context = CliContext::UTILITY;
}
("--comment", value) => {
args.comment_body = value.to_string();
Expand All @@ -235,6 +238,7 @@ fn process_args(pairs: Vec<Arg>) -> Args {
("-before", _) => args.date_sign = String::from("<"),
("-after", _) => args.date_sign = String::from(">"),
("-edit", _) => args.edit = true,
("-dedupe", _) => args.dedupe = true,
("--config-path", value) => args.config_path = value.to_string(),
(name, value) => println!("Could not handle argument {} with value {}", name, value),
}
Expand Down Expand Up @@ -334,18 +338,34 @@ pub fn merge_with_file_config(

match in_file {
Some(config) => {
config.repos.append(&mut label.repos);
for repo in &label.repos {
if !config.repos.contains(repo) {
config.repos.push(repo.to_string());
}
}
}
None => new_config.labels.push(label.clone()),
}
}

new_config.exclude.append(&mut comment_output.excluded);
for repo in &comment_output.excluded {
if !new_config.exclude.contains(repo) {
new_config.exclude.push(repo.to_string());
}
}
new_config.last_date = comment_output.date.clone();

new_config
}

pub fn dedupe_file_config(file_config: &mut FileConfig) {
for label in file_config.labels.iter_mut() {
label.repos.dedup();
}

file_config.exclude.dedup();
}

impl TwiosComment {
pub fn read(&self) -> TwiosCommentOutput {
let mut output = TwiosCommentOutput::new();
Expand Down Expand Up @@ -490,6 +510,7 @@ mod tests {
date_sign: "".to_string(),
exclude: vec![],
query_type: PullRequestQueryType::Created,
dedupe: false,
};
assert_eq!("2022-06-30.md", app_params.file_name());
}
Expand All @@ -509,6 +530,7 @@ mod tests {
date_sign: "".to_string(),
exclude: vec![],
query_type: PullRequestQueryType::Created,
dedupe: false,
};
assert_eq!("2022-06-30.md", app_params.file_name());
}
Expand All @@ -528,6 +550,7 @@ mod tests {
date_sign: "".to_string(),
exclude: vec![],
query_type: PullRequestQueryType::Created,
dedupe: false,
};
assert_eq!("src/twios/2022-06-30.md", app_params.file_name());
}
Expand Down Expand Up @@ -660,4 +683,97 @@ Available categories
merge_with_file_config(&mut expected.read(), file_config),
);
}

#[test]
fn it_doesnt_push_already_exisiting_records() {
let expected = TwiosComment {
body: r#"
Post's file path
- TWIOS_PATH /twios/
Post's date
- TWIOS_DATE >2021-11-28
Available categories
- TWIOS_CATEGORIES Ember,Javascript,Typescript
- TWIOS_UNLABELLED
- [EmbarkStudios/spdx] UNKNOWN @SomeOne
- [mainmatter/ember-simple-auth] Ember @SomeTwo
- [simplabs/ember-error-route] EXCLUDED @SomeThree
- Doesn't catch this
"#
.to_string(),
};

let file_config = FileConfig {
exclude_closed_not_merged: false,
header: vec![],
output_path: "".to_string(),
exclude: vec!["simplabs/ember-error-route".to_string()],
users: vec![],
labels: vec![LabelConfig {
name: "Ember".to_string(),
repos: vec!["mainmatter/ember-simple-auth".to_string()],
}],
last_date: "".to_string(),
query_type: PullRequestQueryType::Created,
};

assert_eq!(
FileConfig {
exclude_closed_not_merged: false,
header: vec![],
output_path: "".to_string(),
exclude: vec!["simplabs/ember-error-route".to_string()],
users: vec![],
labels: vec![LabelConfig {
name: "Ember".to_string(),
repos: vec!["mainmatter/ember-simple-auth".to_string()]
}],
last_date: ">2021-11-28".to_string(),
query_type: PullRequestQueryType::Created,
},
merge_with_file_config(&mut expected.read(), file_config),
);
}

#[test]
fn it_dedupes_config() {
let mut file_config = FileConfig {
exclude_closed_not_merged: false,
header: vec![],
output_path: "".to_string(),
exclude: vec![
"simplabs/ember-error-route".to_string(),
"simplabs/ember-error-route".to_string(),
],
users: vec![],
labels: vec![LabelConfig {
name: "Ember".to_string(),
repos: vec![
"mainmatter/ember-simple-auth".to_string(),
"mainmatter/ember-simple-auth".to_string(),
],
}],
last_date: "".to_string(),
query_type: PullRequestQueryType::Created,
};

dedupe_file_config(&mut file_config);

assert_eq!(
FileConfig {
exclude_closed_not_merged: false,
header: vec![],
output_path: "".to_string(),
exclude: vec!["simplabs/ember-error-route".to_string()],
users: vec![],
labels: vec![LabelConfig {
name: "Ember".to_string(),
repos: vec!["mainmatter/ember-simple-auth".to_string()]
}],
last_date: "".to_string(),
query_type: PullRequestQueryType::Created,
},
file_config,
);
}
}
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ async fn main() -> octocrab::Result<()> {
.write_all(comment_content.join("\n").as_bytes())
.unwrap();
}
cli::CliContext::UTILITY => {
if app_params.dedupe {
let mut config = file_config
.expect("Configuration file doesn't exist")
.clone();
cli::dedupe_file_config(&mut config);
cli::write_config_to_file(app_params.config_path.clone(), &config)
.expect("Couldn't write to file");
}
}
}

Ok(())
Expand Down

0 comments on commit 2bec675

Please sign in to comment.