Skip to content

Commit

Permalink
feat(bump): use glob pattern for branch whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Mar 8, 2022
1 parent 974587c commit 654baa9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ conventional_commit_parser = "0.9.4"
pest = "2.1.3"
pest_derive = "2.1.0"
tera = "1.12.1"
globset = "0.4.8"

[dev-dependencies]
assert_cmd = "1.0.3"
Expand Down
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use colored::*;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use conventional_commit_parser::parse_footers;
use git2::{Oid, RebaseOptions};
use globset::Glob;
use itertools::Itertools;
use lazy_static::lazy_static;
use semver::{Prerelease, Version};
Expand Down Expand Up @@ -403,11 +404,15 @@ impl CocoGitto {

if !SETTINGS.branch_whitelist.is_empty() {
if let Some(branch) = self.repository.get_branch_shorthand() {
ensure!(
SETTINGS.branch_whitelist.contains(&branch),
"Version bump not allowed on branch {}",
branch
)
let whitelist = &SETTINGS.branch_whitelist;
let is_match = whitelist.iter().any(|pattern| {
let glob = Glob::new(pattern)
.expect("invalid glob pattern")
.compile_matcher();
glob.is_match(&branch)
});

ensure!(is_match, "No patterns matched in {whitelist:?} for branch '{branch}', bump is not allowed")
}
};

Expand Down
64 changes: 58 additions & 6 deletions tests/lib_tests/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ fn bump_with_whitelisted_branch_ok() -> Result<()> {
run_cmd!(
echo $settings > cog.toml;
git add .;
)
.unwrap();
)?;

git_commit("chore: first commit")?;
git_commit("feat: add a feature commit")?;
Expand All @@ -107,8 +106,62 @@ fn bump_with_whitelisted_branch_fails() -> Result<()> {
run_cmd!(
echo $settings > cog.toml;
git add .;
)
.unwrap();
)?;

git_commit("chore: first commit")?;
git_commit("feat: add a feature commit")?;

let mut cocogitto = CocoGitto::get()?;

// Act
let result = cocogitto.create_version(VersionIncrement::Auto, None, None);

// Assert
assert_that!(result.unwrap_err().to_string()).is_equal_to(
"No patterns matched in [\"main\"] for branch 'master', bump is not allowed".to_string(),
);

Ok(())
}

#[sealed_test]
fn bump_with_whitelisted_branch_pattern_ok() -> Result<()> {
// Arrange
let settings = r#"branch_whitelist = [ "main", "release/**" ]"#;

git_init()?;
run_cmd!(
echo $settings > cog.toml;
git add .;
)?;

git_commit("chore: first commit")?;
git_commit("feat: add a feature commit")?;

run_cmd!(git checkout -b release/1.0.0;)?;

let mut cocogitto = CocoGitto::get()?;

// Act
let result = cocogitto.create_version(VersionIncrement::Auto, None, None);

// Assert
assert_that!(result).is_ok();

Ok(())
}

#[sealed_test]
fn bump_with_whitelisted_branch_pattern_err() -> Result<()> {
// Arrange
let settings = r#"branch_whitelist = [ "release/**" ]"#;

git_init()?;
run_cmd!(
echo $settings > cog.toml;
git add .;
)?;

git_commit("chore: first commit")?;
git_commit("feat: add a feature commit")?;

Expand All @@ -118,8 +171,7 @@ fn bump_with_whitelisted_branch_fails() -> Result<()> {
let result = cocogitto.create_version(VersionIncrement::Auto, None, None);

// Assert
assert_that!(result.unwrap_err().to_string())
.is_equal_to("Version bump not allowed on branch master".to_string());
assert_that!(result).is_err();

Ok(())
}

0 comments on commit 654baa9

Please sign in to comment.