From b24bd9d87583448cf0ebdbd90ce1ce14bfcd6777 Mon Sep 17 00:00:00 2001 From: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Date: Fri, 31 May 2024 13:47:58 +0200 Subject: [PATCH] feat(lint): Implement auto-fix functionality (#5) * implement fixing changelog contents * remove unused fields in entry and address unused import --- CHANGELOG.md | 3 ++- src/change_type.rs | 10 ++-------- src/changelog.rs | 19 ++++++++++++++++--- src/cli.rs | 11 ++++++++--- src/entry.rs | 42 +----------------------------------------- src/lint.rs | 21 +++++++++++++++------ src/main.rs | 7 ++++--- 7 files changed, 48 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dcebe9..2f9dc44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,5 +4,6 @@ ### Features +- (lint) [#5](https://github.com/MalteHerrmann/changelog-utils/pull/5) Implement fix flag for linter CLI. - (lint) [#4](https://github.com/MalteHerrmann/changelog-utils/pull/4) Rewrite linter implementation in Rust. -- (lint) [#1](https://github.com/MalteHerrmann/changelog-utils/pull/1) Add initial implementation for linter in Python. +- (lint) [#1](https://github.com/MalteHerrmann/changelog-utils/pull/1) Add initial implementation for linter in Python. \ No newline at end of file diff --git a/src/change_type.rs b/src/change_type.rs index e250342..270fcfe 100644 --- a/src/change_type.rs +++ b/src/change_type.rs @@ -1,22 +1,18 @@ -use crate::{config, entry::Entry, errors::ChangeTypeError}; +use crate::{config, errors::ChangeTypeError}; use regex::{Regex, RegexBuilder}; #[derive(Clone, Debug)] pub struct ChangeType { pub name: String, - line: String, - fixed: String, + pub fixed: String, pub problems: Vec, - entries: Vec, } pub fn new_empty_change_type() -> ChangeType { ChangeType{ name: "".to_string(), - line: "".to_string(), fixed: "".to_string(), problems: Vec::new(), - entries: Vec::new(), } } @@ -65,10 +61,8 @@ pub fn parse(config: config::Config, line: &str) -> Result, pub releases: HashMap, pub problems: Vec } @@ -13,6 +14,7 @@ pub struct Changelog { /// /// TODO: implement fix functionality pub fn parse_changelog(config: Config, contents: &str) -> Result { + let mut fixed: Vec = Vec::new(); let mut releases: HashMap = HashMap::new(); let mut problems: Vec = Vec::new(); @@ -33,15 +35,18 @@ pub fn parse_changelog(config: Config, contents: &str) -> Result Result Result e, - Err(e) => { - problems.push(e.to_string()); + Err(ee) => { + problems.push(ee.to_string()); + fixed.push(line.to_string()); continue } }; @@ -122,7 +133,9 @@ pub fn parse_changelog(config: Config, contents: &str) -> Result? @@ -81,11 +73,7 @@ pub fn parse(config: config::Config, line: &str) -> Result { ); Ok(Entry { - line: line.to_string(), fixed, // TODO: why is it not possible to have this as &'a str too? - category: category.to_string(), - description: description.to_string(), - link: link.to_string(), pr_number, // TODO: implement describing problems in line problems, @@ -288,15 +276,8 @@ mod entry_tests { let entry_res = parse(load_test_config(), example); assert!(entry_res.is_ok()); let entry = entry_res.unwrap(); - assert_eq!(entry.line, example); assert_eq!(entry.fixed, example); // NOTE: since line is okay there are no changes to it in the fixed version - assert_eq!(entry.category, "cli"); assert_eq!(entry.pr_number, 1); - assert_eq!( - entry.link, - "https://github.com/MalteHerrmann/changelog-utils/pull/1" - ); - assert_eq!(entry.description, "Add initial Python implementation."); assert!(entry.problems.is_empty()); } @@ -308,15 +289,8 @@ mod entry_tests { // TODO: should this actually return an error? Not really, because parsing has worked?? assert!(entry_res.is_ok()); let entry = entry_res.unwrap(); - assert_eq!(entry.line, example); assert_eq!(entry.fixed, example.replace(r"\", "")); - assert_eq!(entry.category, "cli"); assert_eq!(entry.pr_number, 1); - assert_eq!( - entry.link, - "https://github.com/MalteHerrmann/changelog-utils/pull/1" - ); - assert_eq!(entry.description, "Test."); assert_eq!(entry.problems.len(), 1); assert_eq!( entry.problems[0], @@ -331,15 +305,8 @@ mod entry_tests { let entry_res = parse(load_test_config(), example); assert!(entry_res.is_ok()); let entry = entry_res.unwrap(); - assert_eq!(entry.line, example); assert_eq!(entry.fixed, fixed); - assert_eq!(entry.category, "cli"); assert_eq!(entry.pr_number, 2); - assert_eq!( - entry.link, - "https://github.com/MalteHerrmann/changelog-utils/pull/1" - ); - assert_eq!(entry.description, "Test"); assert_eq!(entry.problems.len(), 2); assert_eq!( entry.problems, @@ -369,15 +336,8 @@ mod entry_tests { let entry_res = parse(load_test_config(), example); assert!(entry_res.is_ok()); let entry = entry_res.unwrap(); - assert_eq!(entry.line, example); assert_eq!(entry.fixed, expected); - assert_eq!(entry.category, "cli"); assert_eq!(entry.pr_number, 1); - assert_eq!( - entry.link, - "https://github.com/MalteHerrmann/changelog-utils/pull/1" - ); - assert_eq!(entry.description, "Run test."); assert_eq!(entry.problems.len(), 2); assert_eq!( entry.problems, diff --git a/src/lint.rs b/src/lint.rs index 1345636..9ef25a6 100644 --- a/src/lint.rs +++ b/src/lint.rs @@ -7,7 +7,7 @@ use std::{fs, path::Path}; /// Runs the main logic for the linter, by searching for the changelog file in the /// current directory and then executing the linting on the found file. -pub fn run() -> Result<(), LintError> { +pub fn run(fix: bool) -> Result<(), LintError> { let changelog_file = match fs::read_dir(Path::new("./"))?.find(|e| { e.as_ref() .is_ok_and(|e| e.file_name().to_ascii_lowercase() == "changelog.md") @@ -27,15 +27,24 @@ pub fn run() -> Result<(), LintError> { let changelog = lint(config, &changelog_file.path())?; match changelog.problems.is_empty() { true => { - println!("changelog has no problems"); + println ! ("changelog has no problems"); Ok(()) }, false => { - println!("found problems in changelog:"); - for problem in changelog.problems { - println!("{}", problem); + match fix { + false => { + println!("found problems in changelog:"); + for problem in changelog.problems { + println!("{}", problem); + } + Err(LintError::ProblemsInChangelog) + }, + true => { + fs::write(changelog_file.path(), changelog.fixed.join("\n"))?; + println!("automated fixes were applied to {}", changelog_file.path().to_string_lossy()); + Ok(()) + } } - Err(LintError::ProblemsInChangelog) } } } diff --git a/src/main.rs b/src/main.rs index 4c92290..90630f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,10 @@ use clu::{cli::ChangelogCLI, errors::CLIError, lint}; use clap::Parser; fn main() -> Result<(), CLIError>{ - match ChangelogCLI::parse() { - ChangelogCLI::Lint => { - Ok(lint::run()?) + let cli = ChangelogCLI::parse(); + match cli { + ChangelogCLI::Lint(args) => { + Ok(lint::run(args.fix)?) } } }