Skip to content

Commit

Permalink
feat: add branch whitelist to cog bump #165
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Jan 27, 2022
1 parent db8cfb5 commit d78071a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ impl CocoGitto {
// Fail if repo contains un-staged or un-committed changes
ensure!(statuses.0.is_empty(), "{}", self.repository.get_statuses()?);

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 current_tag = self.repository.get_latest_tag();
let current_version = match current_tag {
Ok(ref tag) => tag.to_version().unwrap_or_else(|_err| {
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum HookType {
#[serde(deny_unknown_fields)]
pub struct Settings {
#[serde(default)]
pub branch_whitelist: Vec<String>,
pub tag_prefix: Option<String>,
#[serde(default)]
pub pre_bump_hooks: Vec<String>,
Expand Down
78 changes: 78 additions & 0 deletions tests/lib_tests/bump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use anyhow::Result;

use cmd_lib::run_cmd;
use cocogitto::{conventional::version::VersionIncrement, CocoGitto};
use sealed_test::prelude::*;
use speculoos::prelude::*;

use crate::helpers::*;

#[sealed_test]
fn bump_no_whitelisted_branch_ok() -> Result<()> {
// Arrange
git_init().expect("Could not init repository");
git_commit("chore: first commit").expect("Could not create commit");
git_commit("feat: add a feature commit").expect("Could not create commit");

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_ok() -> Result<()> {
// Arrange
let settings = r#"branch_whitelist = [ "master" ]"#;

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

git_commit("chore: first commit").expect("Could not create commit");
git_commit("feat: add a feature commit").expect("Could not create commit");

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_fails() -> Result<()> {
// Arrange
let settings = r#"branch_whitelist = [ "main" ]"#;

git_init()?;
run_cmd!(
echo $settings > cog.toml;
git add .;
)
.unwrap();
git_commit("chore: first commit").expect("Could not create commit");
git_commit("feat: add a feature commit").expect("Could not create 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("Version bump not allowed on branch master".to_string());

Ok(())
}
1 change: 1 addition & 0 deletions tests/lib_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bump;
mod cocogitto;
mod init;
mod log;

0 comments on commit d78071a

Please sign in to comment.