Skip to content

Commit

Permalink
fix: normalize footer BREAKING CHANGE and BREAKING-CHANGE to avoi…
Browse files Browse the repository at this point in the history
…d duplicate header in changelog

Refs: #222
  • Loading branch information
hdevalke committed Apr 25, 2024
1 parent 1cb0e55 commit 7b60f34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/cmd/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
Note, NoteGroup, Reference,
},
config::Config,
CommitParser, Footer,
CommitParser, Footer, FooterKey,
},
git::{filter_merge_commits, GitHelper, VersionAndTag},
semver::SemVer,
Expand Down Expand Up @@ -102,10 +102,10 @@ impl<'a> ChangeLogTransformer<'a> {
fn make_notes(&self, footers: &'a [Footer], scope: Option<String>) -> Vec<(String, Note)> {
footers
.iter()
.filter(|footer| footer.key.starts_with("BREAKING"))
.filter(|footer| matches!(footer.key, FooterKey::BreakingChange))
.map(|footer| {
(
footer.key.clone(),
footer.key.to_string(),
Note {
scope: scope.clone(),
text: footer.value.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/conventional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub(crate) mod changelog;
mod commits;
pub(crate) mod config;

pub(crate) use commits::{CommitParser, Footer, ParseError};
pub(crate) use commits::{CommitParser, Footer, FooterKey, ParseError};
pub(crate) use config::Config;
42 changes: 33 additions & 9 deletions src/conventional/commits.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
use std::fmt;
use std::fmt::{self, Display};

use regex::Regex;
use serde::Serialize;
use thiserror::Error;

#[derive(Debug, PartialEq)]
pub(crate) struct Footer {
pub(crate) key: String,
pub(crate) key: FooterKey,
pub(crate) value: String,
}

#[derive(Debug, PartialEq)]
pub(crate) enum FooterKey {
BreakingChange,
String(String),
}

impl From<&str> for FooterKey {
fn from(value: &str) -> Self {
match value {
"BREAKING CHANGE" | "BREAKING-CHANGE" => Self::BreakingChange,
_ => Self::String(value.to_owned()),
}
}
}

impl Display for FooterKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FooterKey::BreakingChange => write!(f, "BREAKING CHANGE"),
FooterKey::String(s) => write!(f, "{}", s),
}
}
}

#[derive(Debug, PartialEq, Serialize)]
pub(crate) struct Reference {
pub(crate) action: Option<String>,
Expand All @@ -34,7 +58,7 @@ impl Commit {
|| self
.footers
.iter()
.any(|f| f.key == "BREAKING CHANGE" || f.key == "BREAKING-CHANGE")
.any(|f| matches!(f.key, FooterKey::BreakingChange))
}
}

Expand Down Expand Up @@ -110,13 +134,13 @@ impl CommitParser {
match (key, ref_key, value) {
(Some(key), None, Some(value)) => {
footers.push(Footer {
key: key.to_owned(),
key: key.into(),
value: value.to_owned(),
});
}
(None, Some(key), Some(value)) => {
footers.push(Footer {
key: key.to_owned(),
key: key.into(),
value: value.to_owned(),
});
}
Expand All @@ -132,7 +156,7 @@ impl CommitParser {
for captures in self.regex_references.captures_iter(line) {
let prefix = &captures[1];
let issue = &captures[2];
let action = footers.last().map(|footer| footer.key.to_owned());
let action = footers.last().map(|footer| footer.key.to_string());
let reference = Reference {
action,
prefix: prefix.into(),
Expand Down Expand Up @@ -352,7 +376,7 @@ mod tests {
description: "allow provided config object to extend other configs".into(),
body: None,
footers: vec![Footer {
key: "BREAKING CHANGE".to_string(),
key: FooterKey::BreakingChange,
value:
"`extends` key in config file is now used for extending other config files"
.to_string()
Expand Down Expand Up @@ -406,11 +430,11 @@ mod tests {
body: Some("see the issue for details\n\non typos fixed.".into()),
footers: vec![
Footer {
key: "Reviewed-by".to_string(),
key: FooterKey::String("Reviewed-by".into()),
value: "Z".to_string()
},
Footer {
key: "Refs".to_string(),
key: "Refs".into(),
value: "133".to_string()
}
],
Expand Down

0 comments on commit 7b60f34

Please sign in to comment.