Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(cli): Apply auto fixes to new entries. #65

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog was created using the `clu` binary

### Improvements

- (cli) [#65](https://github.com/MalteHerrmann/changelog-utils/pull/65) Apply auto fixes to new entries.
- (ci) [#60](https://github.com/MalteHerrmann/changelog-utils/pull/60) Bump changelog linter to v0.2.0.

### Bug Fixes
Expand Down
12 changes: 7 additions & 5 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ pub fn add_entry(
};

let mut idx = 0;
let mut found = false;
let mut change_type_is_found = false;
for (i, ct) in unreleased.clone().change_types.into_iter().enumerate() {
if ct.name.eq(&change_type) {
idx = i;
found = true;
change_type_is_found = true;
}
}

let new_entry = entry::Entry::new(config, cat, desc, pr);
// NOTE: we're re-parsing the entry from the fixed version to incorporate all possible fixes
let new_fixed_entry = entry::parse(config, new_entry.fixed.as_str()).unwrap();

// Get the mutable change type to add the entry into.
// NOTE: If it's not found yet, we add a new section to the changelog.
match found {
match change_type_is_found {
false => {
let new_ct = change_type::new(change_type.to_owned(), Some(vec![new_entry]));
let new_ct = change_type::new(change_type.to_owned(), Some(vec![new_fixed_entry]));
unreleased.change_types.push(new_ct);
}
true => {
Expand All @@ -116,7 +118,7 @@ pub fn add_entry(
.get_mut(idx)
.expect("failed to get change type");

mut_ct.entries.insert(0, new_entry);
mut_ct.entries.insert(0, new_fixed_entry);
}
}
}
4 changes: 2 additions & 2 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ pub fn check_description(config: &config::Config, desc: &str) -> (String, Vec<St
))
}

let last_letter = desc
let last_letter = fixed
.chars()
.last()
.expect("no characters found in description");
if last_letter.to_string() != '.'.to_string() {
fixed = desc.to_string() + ".";
fixed = fixed.to_string() + ".";
problems.push(format!("PR description should end with a dot: '{}'", desc))
}

Expand Down
46 changes: 46 additions & 0 deletions tests/add_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use assert_fs::NamedTempFile;
use clu::{add, changelog, config};
use std::{borrow::BorrowMut, path::Path};

Expand Down Expand Up @@ -73,3 +74,48 @@ fn test_pass_add_with_no_unreleased_section() {
"- (test) [#15](https://github.com/evmos/evmos/pull/15) Test object."
);
}

#[test]
fn test_pass_add_new_with_auto_fix() {
let config = load_example_config();
let mut changelog = changelog::parse_changelog(
config.clone(),
Path::new("tests/testdata/changelog_new_category_after_add.md"),
)
.expect("failed to parse example changelog");
assert_eq!(changelog.releases.len(), 2);

add::add_entry(
&config,
&mut changelog,
"Bug Fixes",
"all",
"adding an entry that's auto-fixable",
15,
);

// export to temporary file
let tmp_path = NamedTempFile::new("tmp_changelog.md").expect("failed to save tmp changelog");
changelog
.write(tmp_path.path())
.expect("failed to write tmp changelog");

let updated_changelog = changelog::parse_changelog(config.clone(), tmp_path.path()).unwrap();
let added_entry = updated_changelog
.releases
.get(0)
.unwrap()
.change_types
.get(2)
.unwrap()
.entries
.get(0)
.unwrap();

// NOTE: we're expecting to have the first letter capitalized and the dot at the end added
let expected: Vec<String> = vec![];
assert_eq!(
added_entry.problems, expected,
"expected line to have been corrected before writing to changelog."
);
}
Loading