Skip to content

Commit

Permalink
imp(config): Use change type abbreviations instead of patterns in con…
Browse files Browse the repository at this point in the history
…fig (#55)

* rework change type configuration to use abbreviations instead of patterns

* add changelog entry
  • Loading branch information
MalteHerrmann committed Jul 27, 2024
1 parent b53eeef commit b277408
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .clconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions src/change_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ pub fn parse(config: config::Config, line: &str) -> Result<ChangeType, ChangeTyp
let mut problems: Vec<String> = 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()
Expand Down
28 changes: 21 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct Config {
pub categories: Vec<String>,
/// 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<String, String>,
/// The map of expected spellings.
///
Expand Down Expand Up @@ -49,6 +49,23 @@ impl fmt::Display for Config {
}
}

impl Default for Config {
fn default() -> Config {
let mut default_change_types: BTreeMap<String, String> = 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<Config, ConfigError> {
let config: Config = serde_json::from_str(contents)?;
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 2 additions & 18 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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<String, String> = 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 {
[
Expand Down
6 changes: 3 additions & 3 deletions src/testdata/example_config.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/testdata/example_config_without_optionals.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 5 additions & 5 deletions tests/testdata/evmos_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b277408

Please sign in to comment.