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(parser): allow whole commit context to be used in commit parsers #758

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl<'a> Changelog<'a> {
config,
additional_context: HashMap::new(),
};
changelog.add_remote_data()?;
orhun marked this conversation as resolved.
Show resolved Hide resolved
changelog.process_commits();
changelog.process_releases();
changelog.add_remote_data()?;
Ok(changelog)
}

Expand Down
82 changes: 66 additions & 16 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ impl Commit<'_> {
protect_breaking: bool,
filter: bool,
) -> Result<Self> {
let lookup_context = serde_json::to_value(&self).map_err(|e| {
AppError::FieldError(format!(
"failed to convert context into value: {}",
e
))
})?;

for parser in parsers {
let mut regex_checks = Vec::new();
if let Some(message_regex) = parser.message.as_ref() {
Expand All @@ -287,25 +294,17 @@ impl Commit<'_> {
if let (Some(field_name), Some(pattern_regex)) =
(parser.field.as_ref(), parser.pattern.as_ref())
{
regex_checks.push((
pattern_regex,
match field_name.as_str() {
"id" => Some(self.id.clone()),
"message" => Some(self.message.clone()),
"body" => body,
"author.name" => self.author.name.clone(),
"author.email" => self.author.email.clone(),
"committer.name" => self.committer.name.clone(),
"committer.email" => self.committer.email.clone(),
_ => None,
match tera::dotted_pointer(&lookup_context, field_name) {
DerTiedemann marked this conversation as resolved.
Show resolved Hide resolved
Some(value) => {
regex_checks.push((pattern_regex, value.to_string()));
}
.ok_or_else(|| {
AppError::FieldError(format!(
None => {
return Err(AppError::FieldError(format!(
"field {} does not have a value",
field_name
))
})?,
));
)));
}
}
}
if parser.sha.clone().map(|v| v.to_lowercase()).as_deref() ==
Some(&self.id)
Expand Down Expand Up @@ -728,4 +727,55 @@ mod test {

Ok(())
}

#[test]
fn field_name_regex() -> Result<()> {
DerTiedemann marked this conversation as resolved.
Show resolved Hide resolved
let commit = Commit {
message: String::from("feat: do something"),
author: Signature {
name: Some("John Doe".to_string()),
email: None,
timestamp: 0x0,
},
..Default::default()
};
let parsed_commit = commit.clone().parse(
&[CommitParser {
sha: None,
message: None,
body: None,
footer: None,
group: Some(String::from("Test group")),
default_scope: None,
scope: None,
skip: None,
field: Some(String::from("author.name")),
pattern: Regex::new("Something else").ok(),
}],
false,
true,
);

assert!(parsed_commit.is_err());

let parsed_commit = commit.parse(
&[CommitParser {
sha: None,
message: None,
body: None,
footer: None,
group: Some(String::from("Test group")),
default_scope: None,
scope: None,
skip: None,
field: Some(String::from("author.name")),
pattern: Regex::new("John Doe").ok(),
}],
false,
false,
)?;

assert_eq!(Some(String::from("Test group")), parsed_commit.group);
Ok(())
}
}
Loading