8000 feat: Add configurable changelog omission for custom commit types by the-wondersmith · Pull Request #288 · cocogitto/cocogitto · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Add configurable changelog omission for custom commit types #288

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

Merged
merged 8 commits into from
Jun 23, 2023
6 changes: 5 additions & 1 deletion src/conventional/changelog/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ impl<'a> From<CommitRange<'a>> for Release<'a> {
}

match Commit::from_git_commit(&commit) {
Ok(commit) => commits.push(ChangelogCommit::from(commit)),
Ok(commit) => {
if !commit.should_omit() {
commits.push(ChangelogCommit::from(commit))
}
}
Err(err) => {
let err = err.to_string().red();
warn!("{}", err);
Expand Down
10000
42 changes: 41 additions & 1 deletion src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ pub struct Commit {
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct CommitConfig {
pub changelog_title: String,
#[serde(default)]
pub omit_from_changelog: bool,
}

impl CommitConfig {
pub(crate) fn new(changelog_title: &str) -> Self {
CommitConfig {
changelog_title: changelog_title.to_string(),
omit_from_changelog: false,
}
}
}
Expand Down Expand Up @@ -87,6 +90,13 @@ impl Commit {
}
}

pub(crate) fn should_omit(&self) -> bool {
SETTINGS
.commit_types()
.get(&self.message.commit_type)
.map_or(false, |config| config.omit_from_changelog)
}

pub fn get_log(&self) -> String {
let summary = &self.message.summary;
let message_display = Commit::short_summary_from_str(summary).yellow();
Expand Down Expand Up @@ -244,7 +254,7 @@ pub(crate) fn format_summary(commit: &ConventionalCommit) -> String {

#[cfg(test)]
mod test {
use crate::conventional::commit::{format_summary, verify, Commit};
use crate::conventional::commit::{format_summary, verify, Commit, CommitConfig};

use chrono::NaiveDateTime;
use cmd_lib::run_fun;
Expand Down Expand Up @@ -440,6 +450,36 @@ mod test {
assert_that!(summary).is_equal_to("fix: this is the message".to_string());
}

#[test]
fn should_toggle_changelog_omission() {
// Arrange
let mut config = CommitConfig::new("Omittable Changes");

// Assert
assert!(
!&config.omit_from_changelog,
"expected CommitConfig::omit_from_changelog to be falsy unless explicitly set"
);

// Act
config.omit_from_changelog = true;

// Assert
assert!(
&config.omit_from_changelog,
"CommitConfig::omit_from_changelog should be truthy after calling CommitConfig::omit"
);

// Act
config.omit_from_changelog = false;

// Assert
assert!(
!&config.omit_from_changelog,
"CommitConfig::omit_from_changelog should be falsy after calling CommitConfig::include"
);
10000 }

#[sealed_test]
fn should_map_conventional_commit() {
// Arrange
Expand Down
65 changes: 64 additions & 1 deletion tests/cog_tests/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ fn get_changelog_from_tag_to_tagged_head() -> Result<()> {
}

#[sealed_test]
fn get_changelog_whith_custom_template() -> Result<()> {
fn get_changelog_with_custom_template() -> Result<()> {
// Arrange
let crate_dir = env!("CARGO_MANIFEST_DIR");
let template = PathBuf::from(crate_dir).join("tests/cog_tests/template.md");
Expand Down Expand Up @@ -437,3 +437,66 @@ fn get_changelog_whith_custom_template() -> Result<()> {
);
Ok(())
}

#[sealed_test]
/// Test that the `omit_from_changelog` configuration
/// directive is honored if/when it is specified for
/// a given commit type.
fn ensure_omit_from_changelog_is_honored() -> Result<()> {
// Arrange
git_init()?;

let cog_toml = indoc!(
"[changelog]
remote = \"github.com\"
repository = \"test\"
owner = \"test\"

[commit_types]
wip = { changelog_title = \"Work In Progress\", omit_from_changelog = false }"
);

let _setup = (
run_cmd!(echo $cog_toml > cog.toml;)?,
fs::read_to_string("cog.toml")?,
git_commit("chore: init")?,
git_commit("wip(some-scope): getting there")?,
git_tag("1.0.0")?,
);

let changelog = Command::cargo_bin("cog")?
.arg("changelog")
// Assert
.assert()
.success();

let changelog = changelog.get_output();
let changelog = String::from_utf8_lossy(&changelog.stdout);

assert!(
changelog.as_ref().contains("#### Work In Progress"),
"Expected changelog to contain a \"Work In Progress\" entry but got:\n\n{}",
changelog.as_ref()
);

let cog_toml = cog_toml.replace("omit_from_changelog = false", "omit_from_changelog = true");

run_cmd!(echo $cog_toml > cog.toml;)?;

let changelog = Command::cargo_bin("cog")?
.arg("changelog")
// Assert
.assert()
.success();

let changelog = changelog.get_output();
let changelog = String::from_utf8_lossy(&changelog.stdout);

assert!(
!changelog.as_ref().contains("#### Work In Progress"),
"Expected \"Work In Progress\" entry to be omitted from changelog but got:\n\n{}",
changelog.as_ref()
);

Ok(())
}
0