diff --git a/.github/workflows/publish-binary.yaml b/.github/workflows/publish-binary.yaml index e2347a0..d324b14 100644 --- a/.github/workflows/publish-binary.yaml +++ b/.github/workflows/publish-binary.yaml @@ -11,7 +11,7 @@ env: RUST_BACKTRACE: 1 jobs: - test: + build: name: ${{ matrix.platform.os_name }} with Rust ${{ matrix.toolchain }} on target ${{ matrix.platform.target }} runs-on: ${{ matrix.platform.os }} strategy: @@ -174,10 +174,19 @@ jobs: with: key: "v2" - - name: Configure Git - run: | - git config --global user.email "jdoe@example.com" - git config --global user.name "J. Doe" + - name: Generate New Release Ghangelog + uses: addnab/docker-run-action@v3 + with: + image: ghcr.io/coenraadhuman/gib:latest + shell: /bin/bash + options: -v ${{ github.workspace }}:/app + run : | + echo "==================" + echo "Generate Changelog" + echo "==================" + gib changelog -p /app > RELEASE_CHANGELOG.md + echo "Updated CHANGELOG.md" + - name: Install musl-tools on Linux run: sudo apt-get update --yes && sudo apt-get install --yes musl-tools @@ -234,4 +243,4 @@ jobs: with: draft: true files: "gib*" - body_path: CHANGELOG.md \ No newline at end of file + body_path: RELEASE_CHANGELOG.md \ No newline at end of file diff --git a/src/args.rs b/src/args.rs index da74449..b3fb8b7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -50,5 +50,9 @@ pub enum Commands { /// Scope regex filter; provide mechanism for generating a changelog for a specific project within a monorepo based of a regular expression #[arg(short, long, value_name = "SCOPE_REGEX_FILTER")] scope_filter: Option, + + /// Specify whether changelog generated is for latest tag + #[arg(short, long)] + release: bool } -} +} \ No newline at end of file diff --git a/src/commands/changelog.rs b/src/commands/changelog.rs index 152ba13..24f9211 100644 --- a/src/commands/changelog.rs +++ b/src/commands/changelog.rs @@ -7,7 +7,12 @@ use crate::semantic::{add_commit_to_version, Version}; // Todo: find a nice template engine for Rust to create the changelog document: // Todo: use map for tag associated with commits on current branch: -pub fn run(path: Option, commit_git_hook: Option, scope_filter: Option) { +pub fn run( + path: Option, + commit_git_hook: Option, + scope_filter: Option, + _release: bool, +) { let commits = retrieve_branch_commits(path.clone()); let optional_oid_commit_tag_map = retrieve_commit_tag_map(path); @@ -19,80 +24,148 @@ pub fn run(path: Option, commit_git_hook: Option, scope_filter: simple_changelog.insert_str(0, " \n"); for commit in commits.iter().rev() { - match commit.message { - Some(ref message) => { - version = add_commit_to_version(&version, create_conventional_commit(message), scope_filter.clone()); + if let Some(ref message) = commit.message { + version = add_commit_to_version( + &version, + create_conventional_commit(message), + scope_filter.clone(), + ); - match create_conventional_commit(message) { - Some(conventional_commit) => { - if scope_filter_check(scope_filter.clone(), conventional_commit.scope) { - // Add commit to log: - simple_changelog.insert_str(0, " \n"); - simple_changelog.insert_str(0, &format!(" {}\n", commit.committer.to_changelog_string())); - simple_changelog.insert_str(0, &format!(" {}\n", commit.author.to_changelog_string())); - simple_changelog.insert_str(0, &format!(" {}\n", if conventional_commit.is_deprecrated { 'X' } else { ' ' })); - simple_changelog.insert_str(0, &format!(" {}\n", if conventional_commit.is_breaking { 'X' } else { ' ' })); - simple_changelog.insert_str(0, &format!(" {}.\n", conventional_commit.commit_description.to_sentence_case())); - simple_changelog.insert_str(0, &format!(" {}\n", conventional_commit.commit_type)); - simple_changelog.insert_str(0, &format!(" {}\n", version.format())); - simple_changelog.insert_str(0, " \n"); + if let Some(conventional_commit) = create_conventional_commit(message) { + if scope_filter_check(scope_filter.clone(), conventional_commit.scope) { + // Add commit to log: + simple_changelog.insert_str(0, " \n"); + simple_changelog.insert_str( + 0, + &format!( + " {}\n", + commit.committer.to_changelog_string() + ), + ); + simple_changelog.insert_str( + 0, + &format!(" {}\n", commit.author.to_changelog_string()), + ); + simple_changelog.insert_str( + 0, + &format!( + " {}\n", + if conventional_commit.is_deprecrated { + 'X' + } else { + ' ' + } + ), + ); + simple_changelog.insert_str( + 0, + &format!( + " {}\n", + if conventional_commit.is_breaking { + 'X' + } else { + ' ' + } + ), + ); + simple_changelog.insert_str( + 0, + &format!( + " {}.\n", + conventional_commit.commit_description.to_sentence_case() + ), + ); + simple_changelog.insert_str( + 0, + &format!(" {}\n", conventional_commit.commit_type), + ); + simple_changelog + .insert_str(0, &format!(" {}\n", version.format())); + simple_changelog.insert_str(0, " \n"); - match optional_oid_commit_tag_map { - Some(ref map) => { - // Add release entry to log: - match map.get(&commit.oid) { - Some(found_tag) => { - simple_changelog.insert_str(0, " \n"); - simple_changelog.insert_str(0, &format!(" Release: {}\n", found_tag)); - simple_changelog.insert_str(0, " \n"); - unreleased_count = 0; - }, - // No release associated with commit, add to counter to determine no release header: - None => unreleased_count = unreleased_count + 1, - } - }, + match optional_oid_commit_tag_map { + Some(ref map) => { + // Add release entry to log: + match map.get(&commit.oid) { + Some(found_tag) => { + simple_changelog.insert_str(0, " \n"); + simple_changelog.insert_str(0, &format!(" Release: {}\n", found_tag)); + simple_changelog.insert_str(0, " \n"); + unreleased_count = 0; + } // No release associated with commit, add to counter to determine no release header: - None => unreleased_count = unreleased_count + 1, - }; + None => unreleased_count += 1, + } } - }, - None => {}, + // No release associated with commit, add to counter to determine no release header: + None => unreleased_count += 1, + }; } - }, - None => {}, + } } } - match commit_git_hook { - Some(ref user_commit) => { - match create_conventional_commit(user_commit) { - Some(user_conventional_commit) => { - version = add_commit_to_version(&version, create_conventional_commit(user_commit), scope_filter.clone()); + if let Some(ref user_commit) = commit_git_hook { + if let Some(user_conventional_commit) = create_conventional_commit(user_commit) { + version = add_commit_to_version( + &version, + create_conventional_commit(user_commit), + scope_filter.clone(), + ); - // Add commit to log: - simple_changelog.insert_str(0, " \n"); - simple_changelog.insert_str(0, &format!(" {}\n", "Unknown")); - simple_changelog.insert_str(0, &format!(" {}\n", "Unknown")); - simple_changelog.insert_str(0, &format!(" {}\n", if user_conventional_commit.is_deprecrated { 'X' } else { ' ' })); - simple_changelog.insert_str(0, &format!(" {}\n", if user_conventional_commit.is_breaking { 'X' } else { ' ' })); - simple_changelog.insert_str(0, &format!(" {}.\n", user_conventional_commit.commit_description.to_sentence_case())); - simple_changelog.insert_str(0, &format!(" {}\n", user_conventional_commit.commit_type)); - simple_changelog.insert_str(0, &format!(" {}\n", version.format())); - simple_changelog.insert_str(0, " \n"); + // Add commit to log: + simple_changelog.insert_str(0, " \n"); + simple_changelog.insert_str(0, &format!(" {}\n", "Unknown")); + simple_changelog.insert_str(0, &format!(" {}\n", "Unknown")); + simple_changelog.insert_str( + 0, + &format!( + " {}\n", + if user_conventional_commit.is_deprecrated { + 'X' + } else { + ' ' + } + ), + ); + simple_changelog.insert_str( + 0, + &format!( + " {}\n", + if user_conventional_commit.is_breaking { + 'X' + } else { + ' ' + } + ), + ); + simple_changelog.insert_str( + 0, + &format!( + " {}.\n", + user_conventional_commit + .commit_description + .to_sentence_case() + ), + ); + simple_changelog.insert_str( + 0, + &format!(" {}\n", user_conventional_commit.commit_type), + ); + simple_changelog.insert_str(0, &format!(" {}\n", version.format())); + simple_changelog.insert_str(0, " \n"); - // Will always be unreleased: - unreleased_count = unreleased_count + 1; - }, - // Commit invalid ignore: - None => {}, - } - }, - None => {}, + // Will always be unreleased: + unreleased_count += 1; + } } if unreleased_count != 0 { simple_changelog.insert_str(0, " \n"); - simple_changelog.insert_str(0, " Unreleased\n"); + simple_changelog.insert_str( + 0, + " Unreleased\n", + ); simple_changelog.insert_str(0, " \n"); } @@ -112,4 +185,4 @@ pub fn run(path: Option, commit_git_hook: Option, scope_filter: simple_changelog.insert_str(0, "# Changelog\n\n"); print!("{}", simple_changelog); -} \ No newline at end of file +} diff --git a/src/commands/version.rs b/src/commands/version.rs index 6789109..34f4d58 100644 --- a/src/commands/version.rs +++ b/src/commands/version.rs @@ -1,9 +1,19 @@ use crate::conventional::create_conventional_commit; use crate::git::{retrieve_branch_commits, Commit}; -use crate::semantic::{add_commit_to_version, add_impact_to_version, Version, Impact}; - -pub fn run(path: Option, major:bool, minor: bool, patch: bool, commit_git_hook: Option, scope_filter: Option) { - let flag_count: u8 = vec![major, minor, patch].into_iter().map(|flag| if flag { 1 } else { 0 }).sum(); +use crate::semantic::{add_commit_to_version, add_impact_to_version, Impact, Version}; + +pub fn run( + path: Option, + major: bool, + minor: bool, + patch: bool, + commit_git_hook: Option, + scope_filter: Option, +) { + let flag_count: u8 = vec![major, minor, patch] + .into_iter() + .map(|flag| if flag { 1 } else { 0 }) + .sum(); if flag_count > 1 { panic!("Only one of the following flags major, minor or patch are allowed at a time") @@ -14,32 +24,34 @@ pub fn run(path: Option, major:bool, minor: bool, patch: bool, commit_gi let mut version = Version::new(0, 0, 0); for commit in commits.iter().rev() { - match commit.message { - Some(ref message) => { - version = add_commit_to_version(&version, create_conventional_commit(message), scope_filter.clone()) - }, - None => {}, + if let Some(ref message) = commit.message { + version = add_commit_to_version( + &version, + create_conventional_commit(message), + scope_filter.clone(), + ) } } - match commit_git_hook { - Some(message) => { - version = add_commit_to_version(&version, create_conventional_commit(message.as_str()), scope_filter.clone()) - }, - None => {}, + if let Some(message) = commit_git_hook { + version = add_commit_to_version( + &version, + create_conventional_commit(message.as_str()), + scope_filter.clone(), + ) } if major { - version = add_impact_to_version(&version, Impact::MAJOR); + version = add_impact_to_version(&version, Impact::Major); } if minor { - version = add_impact_to_version(&version, Impact::MINOR); + version = add_impact_to_version(&version, Impact::Minor); } if patch { - version = add_impact_to_version(&version, Impact::PATCH); + version = add_impact_to_version(&version, Impact::Patch); } version.print(); -} \ No newline at end of file +} diff --git a/src/conventional.rs b/src/conventional.rs index 7d01d7c..a442c46 100644 --- a/src/conventional.rs +++ b/src/conventional.rs @@ -4,7 +4,6 @@ use fancy_regex::Regex; use lazy_static::lazy_static; pub struct ConventionalCommit { - pub commit_type: Type, pub commit_description: String, pub scope: Option, @@ -13,11 +12,9 @@ pub struct ConventionalCommit { pub is_deprecrated: bool, pub commit_body: Option, pub commit_footer: Option, - } impl ConventionalCommit { - pub fn new( commit_type: Type, commit_description: String, @@ -26,7 +23,7 @@ impl ConventionalCommit { is_breaking: bool, is_deprecrated: bool, commit_body: Option, - commit_footer: Option + commit_footer: Option, ) -> ConventionalCommit { ConventionalCommit { commit_type, @@ -36,43 +33,40 @@ impl ConventionalCommit { is_breaking, is_deprecrated, commit_body, - commit_footer + commit_footer, } } - } -#[derive(Debug)] -#[derive(PartialEq)] +#[derive(Debug, PartialEq)] pub enum Type { - FEATURE, - REFACTOR, - PERFORMANCE, - FIX, - CHORE, - REVERT, - DOCS, - STYLE, - TEST, - BUILD, - CI + Feature, + Refactor, + Performance, + Fix, + Chore, + Revert, + Docs, + Style, + Test, + Build, + Ci, } impl FromStr for Type { - fn from_str(input: &str) -> Result { match input { - "feat" => Ok(Type::FEATURE), - "refactor" => Ok(Type::REFACTOR), - "perf" => Ok(Type::PERFORMANCE), - "fix" => Ok(Type::FIX), - "chore" => Ok(Type::CHORE), - "revert" => Ok(Type::REVERT), - "docs" => Ok(Type::DOCS), - "style" => Ok(Type::STYLE), - "test" => Ok(Type::TEST), - "build" => Ok(Type::BUILD), - "ci" => Ok(Type::CI), + "feat" => Ok(Type::Feature), + "refactor" => Ok(Type::Refactor), + "perf" => Ok(Type::Performance), + "fix" => Ok(Type::Fix), + "chore" => Ok(Type::Chore), + "revert" => Ok(Type::Revert), + "docs" => Ok(Type::Docs), + "style" => Ok(Type::Style), + "test" => Ok(Type::Test), + "build" => Ok(Type::Build), + "ci" => Ok(Type::Ci), _ => Err(()), } } @@ -81,48 +75,39 @@ impl FromStr for Type { } impl Display for Type { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Type::FEATURE=> write!(f, "Feature"), - Type::REFACTOR=> write!(f, "Refactor"), - Type::PERFORMANCE=> write!(f, "Performance"), - Type::FIX=> write!(f, "Fix"), - Type::CHORE=> write!(f, "Chore"), - Type::REVERT=> write!(f, "Revert"), - Type::DOCS=> write!(f, "Documentation"), - Type::STYLE=> write!(f, "Styling"), - Type::TEST=> write!(f, "Testing"), - Type::BUILD=> write!(f, "Builds"), - Type::CI=> write!(f, "CI"), + Type::Feature => write!(f, "Feature"), + Type::Refactor => write!(f, "Refactor"), + Type::Performance => write!(f, "Performance"), + Type::Fix => write!(f, "Fix"), + Type::Chore => write!(f, "Chore"), + Type::Revert => write!(f, "Revert"), + Type::Docs => write!(f, "Documentation"), + Type::Style => write!(f, "Styling"), + Type::Test => write!(f, "Testing"), + Type::Build => write!(f, "Builds"), + Type::Ci => write!(f, "CI"), } } - } pub fn scope_filter_check(scope_regex: Option, scope_value: Option) -> bool { match scope_regex { - // If supplied it needs to match to include commit in calculation; - Some(ref found_scope_regex) => { - - match Regex::new(found_scope_regex) { - Ok(scope_filter_regex) => { - match scope_value { - Some(value) => scope_filter_regex.is_match(&value).unwrap(), - None => false, - } - }, - Err(_) => panic!("Invalid regex supplied on scope filter"), - } - + // If supplied it needs to match to include commit in calculation; + Some(ref found_scope_regex) => match Regex::new(found_scope_regex) { + Ok(scope_filter_regex) => match scope_value { + Some(value) => scope_filter_regex.is_match(&value).unwrap(), + None => false, + }, + Err(_) => panic!("Invalid regex supplied on scope filter"), }, None => { // If scope regex is not supplied then include commit in calculation: true - }, + } } - } - +} pub fn create_conventional_commit(commit: &str) -> Option { lazy_static! { @@ -130,57 +115,59 @@ pub fn create_conventional_commit(commit: &str) -> Option { } if CONVENTIONAL_COMMIT_REGEX.is_match(commit).unwrap() { - match CONVENTIONAL_COMMIT_REGEX.captures(commit) { - Ok(optinionl_captures) => { - - match optinionl_captures { - Some(captures) => { - let commit_type = captures.name("type")?.as_str(); - let commit_scope = captures.name("scope"); - let commit_breaking = captures.name("breaking"); - let commit_description = captures.name("description")?.as_str().trim(); - let commit_body = captures.name("body"); - let commit_footer = captures.name("footers"); - - let is_breaking = match commit_breaking { - Some(_) => true, - None => match commit_body { - Some(value) => value.as_str().contains("BREAKING_CHANGE") || value.as_str().contains("BREAKING-CHANGE") || value.as_str().contains("BREAKING CHANGE"), - None => match commit_footer { - Some(value) => value.as_str().contains("BREAKING_CHANGE") || value.as_str().contains("BREAKING-CHANGE") || value.as_str().contains("BREAKING CHANGE"), - None => false, - }, + Ok(optinionl_captures) => match optinionl_captures { + Some(captures) => { + let commit_type = captures.name("type")?.as_str(); + let commit_scope = captures.name("scope"); + let commit_breaking = captures.name("breaking"); + let commit_description = captures.name("description")?.as_str().trim(); + let commit_body = captures.name("body"); + let commit_footer = captures.name("footers"); + + let is_breaking = match commit_breaking { + Some(_) => true, + None => match commit_body { + Some(value) => { + value.as_str().contains("BREAKING_CHANGE") + || value.as_str().contains("BREAKING-CHANGE") + || value.as_str().contains("BREAKING CHANGE") } - }; - - let is_deprecated = match commit_body { - Some(value) => value.as_str().contains("DEPRECATED"), None => match commit_footer { - Some(value) => value.as_str().contains("DEPRECATED"), + Some(value) => { + value.as_str().contains("BREAKING_CHANGE") + || value.as_str().contains("BREAKING-CHANGE") + || value.as_str().contains("BREAKING CHANGE") + } None => false, }, - }; + }, + }; - return Some(ConventionalCommit::new( - Type::from_str(commit_type).unwrap(), - commit_description.to_owned(), - match commit_scope { Some(value) => Some(value.as_str().to_owned()), None => None }, - commit.to_owned(), - is_breaking, - is_deprecated, - match commit_body { Some(value) => Some(value.as_str().to_owned()), None => None }, - match commit_footer { Some(value) => Some(value.as_str().to_owned()), None => None } - )); - }, - None => panic!("Could not retrieve regex captures for valid commit"), + let is_deprecated = match commit_body { + Some(value) => value.as_str().contains("DEPRECATED"), + None => match commit_footer { + Some(value) => value.as_str().contains("DEPRECATED"), + None => false, + }, + }; + + return Some(ConventionalCommit::new( + Type::from_str(commit_type).unwrap(), + commit_description.to_owned(), + commit_scope.map(|value| value.as_str().to_owned()), + commit.to_owned(), + is_breaking, + is_deprecated, + commit_body.map(|value| value.as_str().to_owned()), + commit_footer.map(|value| value.as_str().to_owned()), + )); } - + None => panic!("Could not retrieve regex captures for valid commit"), }, Err(_) => panic!("Could not retrieve regex captures for valid commit"), } - } - return Option::None; -} \ No newline at end of file + Option::None +} diff --git a/src/git.rs b/src/git.rs index a30afca..3984235 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,16 +1,13 @@ -use std::env; use git2::Repository; use indexmap::IndexMap; +use std::env; pub struct Author { - pub name: Option, - pub email: Option - + pub email: Option, } impl Author { - pub fn to_changelog_string(&self) -> String { let name = match self.name { Some(ref value) => value, @@ -19,23 +16,19 @@ impl Author { let email = match self.email { Some(ref value) => value, - None => return format!("{}", name), + None => return name.to_string(), }; format!("{}", email, name) } - } pub struct Committer { - pub name: Option, - pub email: Option - + pub email: Option, } impl Committer { - pub fn to_changelog_string(&self) -> String { let name = match self.name { Some(ref value) => value, @@ -44,26 +37,21 @@ impl Committer { let email = match self.email { Some(ref value) => value, - None => return format!("{}", name), + None => return name.to_string(), }; format!("{}", email, name) } - } pub struct Commit { - pub message: Option, pub author: Author, pub committer: Committer, - pub oid: String - + pub oid: String, } -impl Commit { - -} +impl Commit {} fn retrieve_git_repository(path: String) -> Repository { match Repository::open(path) { @@ -75,14 +63,12 @@ fn retrieve_git_repository(path: String) -> Repository { fn determine_path(path: Option) -> String { match path { Some(path) => path, - None => { - match env::current_dir() { - Ok(path) => match path.into_os_string().into_string() { - Ok(auto_path) => auto_path, - Err(_) => panic!("Couldn't determine current directory for repository"), - }, - Err(_) => panic!("System can't find current directory"), - } + None => match env::current_dir() { + Ok(path) => match path.into_os_string().into_string() { + Ok(auto_path) => auto_path, + Err(_) => panic!("Couldn't determine current directory for repository"), + }, + Err(_) => panic!("System can't find current directory"), }, } } @@ -103,7 +89,7 @@ pub fn retrieve_commit_tag_map(path: Option) -> Option panic!("Could not retrieve tag names"), }; - if tag_names.len() == 0 { + if tag_names.is_empty() { return None; } @@ -120,21 +106,24 @@ pub fn retrieve_commit_tag_map(path: Option) -> Option) -> Vec { let found_path = determine_path(path); let repo = retrieve_git_repository(found_path); @@ -145,48 +134,38 @@ pub fn retrieve_branch_commits(path: Option) -> Vec { }; match revwalk.push_head() { - Ok(_) => {}, + Ok(_) => {} Err(_) => panic!("Couldn't push head"), }; - return revwalk.map(|step| -> Commit { - match step { - Ok(oid) => { - match repo.find_commit(oid) { + return revwalk + .map(|step| -> Commit { + match step { + Ok(oid) => match repo.find_commit(oid) { Ok(commit) => { - let message = match commit.message() { - Some(value) => Option::Some(value.to_string()), - None => Option::None, - }; + let message = commit.message().map(|value| value.to_string()); let author = Author { - name: match commit.author().name() { - Some(value) => Some(value.to_string()), - None => None, - }, - email: match commit.author().email() { - Some(value) => Some(value.to_string()), - None => None, - }, + name: commit.author().name().map(|value| value.to_string()), + email: commit.author().email().map(|value| value.to_string()), }; let committer = Committer { - name: match commit.committer().name() { - Some(value) => Some(value.to_string()), - None => None, - }, - email: match commit.committer().email() { - Some(value) => Some(value.to_string()), - None => None, - }, + name: commit.author().name().map(|value| value.to_string()), + email: commit.author().email().map(|value| value.to_string()), }; - Commit { message, author, committer, oid: oid.to_string() } - }, + Commit { + message, + author, + committer, + oid: oid.to_string(), + } + } Err(_) => panic!("Could not retrieve oid of commit"), - } - }, - Err(_) => panic!("Could not retrieve commit from oid"), - } - }).collect(); -} \ No newline at end of file + }, + Err(_) => panic!("Could not retrieve commit from oid"), + } + }) + .collect(); +} diff --git a/src/main.rs b/src/main.rs index fe1a0fc..38c50e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,8 @@ fn main() { args::Commands::Version { path, major, minor, patch, commit_git_hook, scope_filter } => { commands::version::run(path, major, minor, patch, commit_git_hook, scope_filter); }, - args::Commands::Changelog { path, commit_git_hook, scope_filter } => { - commands::changelog::run(path, commit_git_hook, scope_filter); + args::Commands::Changelog { path, commit_git_hook, scope_filter, release } => { + commands::changelog::run(path, commit_git_hook, scope_filter, release); }, } diff --git a/src/semantic.rs b/src/semantic.rs index 68cafaa..397ab84 100644 --- a/src/semantic.rs +++ b/src/semantic.rs @@ -1,16 +1,18 @@ use crate::conventional::{scope_filter_check, ConventionalCommit, Type}; pub struct Version { - pub major: u8, pub minor: u8, - pub patch: u8 - + pub patch: u8, } impl Version { pub fn new(major: u8, minor: u8, patch: u8) -> Version { - Version { major: major, minor: minor, patch: patch } + Version { + major, + minor, + patch, + } } pub fn print(&self) { @@ -20,45 +22,52 @@ impl Version { pub fn format(&self) -> String { format!("{}.{}.{}", self.major, self.minor, self.patch) } - } pub enum Impact { - MAJOR, - MINOR, - PATCH, - NONE + Major, + Minor, + Patch, + None, } -pub fn add_commit_to_version(version: &Version, optional_commit: Option, scope_filter: Option) -> Version { +pub fn add_commit_to_version( + version: &Version, + optional_commit: Option, + scope_filter: Option, +) -> Version { match optional_commit { Some(commit) => { - if scope_filter_check(scope_filter, commit.scope) { if commit.is_breaking { - return add_impact_to_version(version, Impact::MAJOR); + return add_impact_to_version(version, Impact::Major); } let impact = match commit.commit_type { - Type::FEATURE => Impact::MINOR, - Type::REFACTOR | Type::PERFORMANCE | Type::FIX | Type::CHORE | Type::REVERT | Type::DOCS | Type::BUILD => Impact::PATCH, - Type::STYLE | Type::TEST | Type::CI => Impact::NONE, + Type::Feature => Impact::Minor, + Type::Refactor + | Type::Performance + | Type::Fix + | Type::Chore + | Type::Revert + | Type::Docs + | Type::Build => Impact::Patch, + Type::Style | Type::Test | Type::Ci => Impact::None, }; - return add_impact_to_version(version, impact) + return add_impact_to_version(version, impact); } - return add_impact_to_version(version, Impact::NONE); - - }, - None => add_impact_to_version(version, Impact::NONE), + add_impact_to_version(version, Impact::None) + } + None => add_impact_to_version(version, Impact::None), } } pub fn add_impact_to_version(version: &Version, impact: Impact) -> Version { match impact { - Impact::MAJOR => Version::new(version.major + 1, 0, 0), - Impact::MINOR => Version::new(version.major, version.minor + 1, 0), - Impact::PATCH => Version::new(version.major, version.minor , version.patch + 1), - Impact::NONE => Version::new(version.major, version.minor, version.patch), + Impact::Major => Version::new(version.major + 1, 0, 0), + Impact::Minor => Version::new(version.major, version.minor + 1, 0), + Impact::Patch => Version::new(version.major, version.minor, version.patch + 1), + Impact::None => Version::new(version.major, version.minor, version.patch), } -} \ No newline at end of file +} diff --git a/src/tests/conventional_tests.rs b/src/tests/conventional_tests.rs index 708331e..f57ee16 100644 --- a/src/tests/conventional_tests.rs +++ b/src/tests/conventional_tests.rs @@ -1,16 +1,17 @@ #[cfg(test)] - use crate::conventional::{create_conventional_commit, Type}; #[test] fn feat_with_scope_and_breaking() { - let result = create_conventional_commit(r"feat(foo)!: add a nice RegEx to check and parse a commit message that folows https://www.conventionalcommits.org/en/v1.0.0"); + let result = create_conventional_commit( + r"feat(foo)!: add a nice RegEx to check and parse a commit message that folows https://www.conventionalcommits.org/en/v1.0.0", + ); assert_eq!(result.is_some(), true); let unwrapped_result = result.unwrap(); - assert_eq!(unwrapped_result.commit_type, Type::FEATURE); + assert_eq!(unwrapped_result.commit_type, Type::Feature); assert_eq!(unwrapped_result.scope.is_some(), true); @@ -31,7 +32,8 @@ fn feat_with_scope_and_breaking() { #[test] fn feat_with_scope_body_and_breaking() { - let result = create_conventional_commit(r"feat(sdf): add a nice RegEx to check and parse a commit message that folows https://www.conventionalcommits.org/en/v1.0.0 + let result = create_conventional_commit( + r"feat(sdf): add a nice RegEx to check and parse a commit message that folows https://www.conventionalcommits.org/en/v1.0.0 Hello, @@ -46,13 +48,14 @@ Example: foo Refs: #42 DEPRECATED BREAKING-CHANGE: Brakes wood -something #mentions a commit"); +something #mentions a commit", + ); assert_eq!(result.is_some(), true); let unwrapped_result = result.unwrap(); - assert_eq!(unwrapped_result.commit_type, Type::FEATURE); + assert_eq!(unwrapped_result.commit_type, Type::Feature); assert_eq!(unwrapped_result.scope.is_some(), true); @@ -80,11 +83,10 @@ fn simple_chore() { let unwrapped_result = result.unwrap(); - assert_eq!(unwrapped_result.commit_type, Type::CHORE); + assert_eq!(unwrapped_result.commit_type, Type::Chore); assert_eq!(unwrapped_result.scope.is_some(), false); - assert_eq!(unwrapped_result.is_breaking, false); assert_eq!(unwrapped_result.is_deprecrated, false); @@ -104,7 +106,7 @@ fn simple_refactor() { let unwrapped_result = result.unwrap(); - assert_eq!(unwrapped_result.commit_type, Type::REFACTOR); + assert_eq!(unwrapped_result.commit_type, Type::Refactor); assert_eq!(unwrapped_result.scope.is_some(), false); @@ -117,4 +119,4 @@ fn simple_refactor() { assert_eq!(unwrapped_result.commit_body.is_none(), true); assert_eq!(unwrapped_result.commit_footer.is_none(), true); -} +} \ No newline at end of file