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

feat(issue-6): optional "strict" mode to fail on invalid commits #10

Merged
merged 2 commits into from
Jan 14, 2020
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
98 changes: 95 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ simple-error = "0.2"
fallible-iterator = "0.2.0"
askama = "0.8"
chrono = "0.4.9"
log = "0.4.8"
stderrlog = "0.4.3"

[dependencies.git2]
version = "0.10"
Expand Down
70 changes: 64 additions & 6 deletions src/bumping.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use semver::Version;
use std::fmt::{Display, Formatter};
use git2::Commit;
use std::convert::TryFrom;
use std::error::Error;
use simple_error::SimpleError;
use std::fmt::{Display, Formatter};
use std::str::FromStr;

use git2::Commit;
use semver::Version;
use simple_error::SimpleError;

/// Extension methods for `&str`, useful for handling conventional commits
pub trait FirstLine<'a> {
/// The first line of a conventional commit
Expand Down Expand Up @@ -92,6 +93,7 @@ pub trait Bump {
fn bump(self, bt: &BumpType) -> Self;
}

/// Case-insensitive parse method for `BumpType`'s enum values
impl FromStr for BumpType {
type Err = SimpleError;

Expand All @@ -106,8 +108,42 @@ impl FromStr for BumpType {
}
}

impl From<&str> for BumpType {
fn from(commit_msg: &str) -> Self {
pub const OTHER_TYPES: &[&str] = &[
"build",
"ci",
"chore",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test"
];

pub trait ObserveParseError {
fn on_error(&mut self, commit_msg: &str, first_line: &str);
}

impl ObserveParseError for () {
fn on_error(&mut self, _commit_msg: &str, _first_line: &str) {
// No-op
}
}

impl BumpType {

pub fn parse_commit_msg(commit_msg: &str) -> Self {
BumpType::parse_commit_msg_with_errors(commit_msg, &mut ())
}

/// Parses a conventional commit message into a `BumpType`. If a non-conventional message
/// is encountered, the `error_observer` is called.
///
/// A NO-OP implementation of `ObserveParseError` is provided for `()`, if you want to ignore
/// errors. (Non-conventional messages always generate `BumpType::None`).
pub fn parse_commit_msg_with_errors(commit_msg: &str, error_observer: &mut dyn ObserveParseError) -> Self {
let first_line = commit_msg.first_line();
let conventional_prefix = first_line.prefix();
let breaking = conventional_prefix.contains('!') || commit_msg.contains("\nBREAKING CHANGE");
Expand All @@ -119,11 +155,33 @@ impl From<&str> for BumpType {
} else if conventional_prefix.starts_with("feat") {
BumpType::Minor
} else {
if !OTHER_TYPES.contains(&conventional_prefix.as_str()) {
error_observer.on_error(commit_msg, first_line);
}
BumpType::None
}
}
}

#[cfg(test)]
mod test {
use crate::bumping::BumpType;

#[test]
fn test_parse_commit_messages() {
assert_eq!(BumpType::parse_commit_msg("feat: hello"), BumpType::Minor);
assert_eq!(BumpType::parse_commit_msg("FEAT: hello"), BumpType::Minor);
assert_eq!(BumpType::parse_commit_msg("feat!: hello"), BumpType::Major);

assert_eq!(BumpType::parse_commit_msg("fix: hello"), BumpType::Patch);
assert_eq!(BumpType::parse_commit_msg("Fix: hello"), BumpType::Patch);
assert_eq!(BumpType::parse_commit_msg("fix!: hello"), BumpType::Major);

assert_eq!(BumpType::parse_commit_msg("chore"), BumpType::None);
assert_eq!(BumpType::parse_commit_msg("platypus: foo"), BumpType::None);
}
}

impl Default for BumpType {
fn default() -> Self {
BumpType::None
Expand Down
2 changes: 1 addition & 1 deletion src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ChangeLog<'_> {
let mut result = ChangeLog::<'a>::default();
let _ = commits.for_each(|ref commit| {
let msg = commit.message().unwrap_or_default();
let bump_type = BumpType::from(msg);
let bump_type = BumpType::parse_commit_msg(msg);
match LogEntry::try_from(commit.clone()) {
Ok(entry) => match bump_type {
BumpType::Patch => result.fixes.push(entry),
Expand Down
Loading