From b27740867169ce043255a2acd5f65e1083f1f955 Mon Sep 17 00:00:00 2001 From: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Date: Sat, 27 Jul 2024 13:05:04 +0200 Subject: [PATCH] imp(config): Use change type abbreviations instead of patterns in config (#55) * rework change type configuration to use abbreviations instead of patterns * add changelog entry --- .clconfig.json | 6 ++-- CHANGELOG.md | 1 + src/change_type.rs | 13 +++++++-- src/config.rs | 28 ++++++++++++++----- src/init.rs | 20 ++----------- src/testdata/example_config.json | 6 ++-- .../example_config_without_optionals.json | 6 ++-- tests/testdata/evmos_config.json | 10 +++---- 8 files changed, 49 insertions(+), 41 deletions(-) diff --git a/.clconfig.json b/.clconfig.json index 2a1ede0..012a10c 100644 --- a/.clconfig.json +++ b/.clconfig.json @@ -10,9 +10,9 @@ "test" ], "change_types": { - "Bug Fixes": "bug\\s*fixes", - "Features": "features", - "Improvements": "improvements" + "Bug Fixes": "fix", + "Features": "feat", + "Improvements": "imp" }, "expected_spellings": { "CLI": "cli" diff --git a/CHANGELOG.md b/CHANGELOG.md index 460e8d2..83a4cd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This changelog was created using the `clu` binary ### Improvements +- (config) [#55](https://github.com/MalteHerrmann/changelog-utils/pull/55) Use change type abbreviations instead of patterns in config. - (all) [#53](https://github.com/MalteHerrmann/changelog-utils/pull/53) Minor codebase improvements. - (crud) [#48](https://github.com/MalteHerrmann/changelog-utils/pull/48) Use authenticated requests when checking open PRs. - (config) [#51](https://github.com/MalteHerrmann/changelog-utils/pull/51) Get available configuration from existing changelog during initialization. diff --git a/src/change_type.rs b/src/change_type.rs index c7f5f55..9573464 100644 --- a/src/change_type.rs +++ b/src/change_type.rs @@ -35,8 +35,17 @@ pub fn parse(config: config::Config, line: &str) -> Result = Vec::new(); // Check if the correctness of the current change type. - if !config.change_types.iter().any(|(change_type, pattern)| { - if !RegexBuilder::new(pattern) + if !config.change_types.iter().any(|(change_type, _)| { + // derive the generalized pattern with case insensitivity and whitespace + // matching from the given change type + let pattern = RegexBuilder::new(r"\s+") + .case_insensitive(true) + .build() + .unwrap() + .replace_all(change_type, r"\s*") + .into_owned(); + + if !RegexBuilder::new(pattern.as_str()) .case_insensitive(true) .build() .unwrap() diff --git a/src/config.rs b/src/config.rs index c5da67d..7c95bff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,9 +12,9 @@ pub struct Config { pub categories: Vec, /// The map of allowed change types. /// - /// Note: The key is the correct spelling and the value is - /// a regular expression matching all possible (mis-)spellings - /// of the given category. + /// Note: The key is the full spelling and the value is + /// an abbreviation that is to be used as a short form + /// in pull request titles. pub change_types: BTreeMap, /// The map of expected spellings. /// @@ -49,6 +49,23 @@ impl fmt::Display for Config { } } +impl Default for Config { + fn default() -> Config { + let mut default_change_types: BTreeMap = BTreeMap::new(); + default_change_types.insert("Bug Fixes".into(), "fix".into()); + default_change_types.insert("Features".into(), "feat".into()); + default_change_types.insert("Improvements".into(), "imp".into()); + + Config { + categories: Vec::default(), + change_types: default_change_types, + expected_spellings: BTreeMap::default(), + legacy_version: None, + target_repo: String::default(), + } + } +} + // Unpacks the configuration from a given raw string. pub fn unpack_config(contents: &str) -> Result { let config: Config = serde_json::from_str(contents)?; @@ -145,10 +162,7 @@ mod config_tests { config.change_types.len() > 0, "expected non-zero length of change types in example config" ); - assert_eq!( - config.change_types.get("Bug Fixes").unwrap(), - "bug\\s*fixes" - ); + assert_eq!(config.change_types.get("Bug Fixes").unwrap(), "fix"); assert!( config.categories.len() > 0, diff --git a/src/init.rs b/src/init.rs index 2bb33b4..02e459d 100644 --- a/src/init.rs +++ b/src/init.rs @@ -2,7 +2,7 @@ use crate::{ changelog::get_settings_from_existing_changelog, config::Config, errors::InitError, github::get_origin, }; -use std::{collections::BTreeMap, fs, path::PathBuf}; +use std::{fs, path::PathBuf}; /// Runs the logic to initialize the changelog utilities /// in the current working directory. @@ -18,7 +18,7 @@ pub fn init_in_folder(target: PathBuf) -> Result<(), InitError> { return Err(InitError::ConfigAlreadyFound); }; - let mut config = create_default_config(); + let mut config = Config::default(); if let Ok(origin) = get_origin() { config.target_repo.clone_from(&origin); @@ -46,22 +46,6 @@ pub fn init_in_folder(target: PathBuf) -> Result<(), InitError> { Ok(config.export(config_path.as_path())?) } -/// Creates a new default configuration file for the tool. -fn create_default_config() -> Config { - let mut default_change_types: BTreeMap = BTreeMap::new(); - default_change_types.insert("Bug Fixes".into(), "bug\\s*fixes".into()); - default_change_types.insert("Features".into(), "features".into()); - default_change_types.insert("Improvements".into(), "improvements".into()); - - Config { - categories: Vec::new(), - change_types: default_change_types, - expected_spellings: BTreeMap::new(), - legacy_version: None, - target_repo: "".to_string(), - } -} - /// Creates an empty skeleton for a changelog. pub fn create_empty_changelog() -> String { [ diff --git a/src/testdata/example_config.json b/src/testdata/example_config.json index 9ce31c9..8feab30 100644 --- a/src/testdata/example_config.json +++ b/src/testdata/example_config.json @@ -1,9 +1,9 @@ { "categories": ["cli", "test"], "change_types": { - "Bug Fixes": "bug\\s*fixes", - "Improvements": "improvements", - "Features": "features" + "Bug Fixes": "fix", + "Improvements": "imp", + "Features": "feat" }, "expected_spellings": { "API": "api", diff --git a/src/testdata/example_config_without_optionals.json b/src/testdata/example_config_without_optionals.json index f185373..48df93c 100644 --- a/src/testdata/example_config_without_optionals.json +++ b/src/testdata/example_config_without_optionals.json @@ -1,9 +1,9 @@ { "categories": ["cli", "test"], "change_types": { - "Bug Fixes": "bug\\s*fixes", - "Improvements": "improvements", - "Features": "features" + "Bug Fixes": "fix", + "Improvements": "imp", + "Features": "feat" }, "expected_spellings": { "API": "api", diff --git a/tests/testdata/evmos_config.json b/tests/testdata/evmos_config.json index 39310c9..6055a59 100644 --- a/tests/testdata/evmos_config.json +++ b/tests/testdata/evmos_config.json @@ -15,11 +15,11 @@ "all" ], "change_types": { - "Bug Fixes": "bug\\s*fixes", - "Improvements": "improvements", - "Features": "features", - "State Machine Breaking": "state\\s*machine\\s*breaking", - "API Breaking": "api\\s*breaking" + "Bug Fixes": "fix", + "Improvements": "imp", + "Features": "feat", + "State Machine Breaking": "imp", + "API Breaking": "imp" }, "expected_spellings": { "ABI": "abi",