Skip to content

Commit e981e1d

Browse files
authored
fix(template): correctly serialize JSON for the commit fields (#1145)
* fix: JSON serialization issue in Commit::parse Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com> * test: add unittests for Commit::parse Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com> * test: add unittests for Commit::parse Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com> --------- Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com>
1 parent 4c3c946 commit e981e1d

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

git-cliff-core/src/commit.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,24 @@ impl Commit<'_> {
335335
let value = if field_name == "body" {
336336
body.clone()
337337
} else {
338-
tera::dotted_pointer(&lookup_context, field_name)
339-
.map(|v| v.to_string())
338+
tera::dotted_pointer(&lookup_context, field_name).and_then(|v| {
339+
match &v {
340+
Value::String(s) => Some(s.clone()),
341+
Value::Number(_) | Value::Bool(_) | Value::Null => {
342+
Some(v.to_string())
343+
}
344+
_ => None,
345+
}
346+
})
340347
};
341348
match value {
342349
Some(value) => {
343350
regex_checks.push((pattern_regex, value));
344351
}
345352
None => {
346353
return Err(AppError::FieldError(format!(
347-
"field {field_name} does not have a value",
354+
"field '{field_name}' is missing or has unsupported \
355+
type (expected String, Number, Bool, or Null)",
348356
)));
349357
}
350358
}
@@ -709,7 +717,7 @@ mod test {
709717
}
710718

711719
#[test]
712-
fn parse_commit_field() -> Result<()> {
720+
fn parse_commit_fields() -> Result<()> {
713721
let mut commit = Commit::new(
714722
String::from("8f55e69eba6e6ce811ace32bd84cc82215673cb6"),
715723
String::from("feat: do something"),
@@ -721,7 +729,15 @@ mod test {
721729
timestamp: 0x0,
722730
};
723731

724-
let parsed_commit = commit.parse(
732+
commit.remote = Some(crate::contributor::RemoteContributor {
733+
username: None,
734+
pr_title: Some("feat: do something".to_string()),
735+
pr_number: None,
736+
pr_labels: Vec::new(),
737+
is_first_time: true,
738+
});
739+
740+
let parsed_commit = commit.clone().parse(
725741
&[CommitParser {
726742
sha: None,
727743
message: None,
@@ -739,6 +755,49 @@ mod test {
739755
)?;
740756

741757
assert_eq!(Some(String::from("Test group")), parsed_commit.group);
758+
759+
let parsed_commit = commit.clone().parse(
760+
&[CommitParser {
761+
sha: None,
762+
message: None,
763+
body: None,
764+
footer: None,
765+
group: Some(String::from("Test group")),
766+
default_scope: None,
767+
scope: None,
768+
skip: None,
769+
field: Some(String::from("remote.pr_title")),
770+
pattern: Regex::new("^feat").ok(),
771+
}],
772+
false,
773+
false,
774+
)?;
775+
776+
assert_eq!(Some(String::from("Test group")), parsed_commit.group);
777+
778+
let parse_result = commit.clone().parse(
779+
&[CommitParser {
780+
sha: None,
781+
message: None,
782+
body: None,
783+
footer: None,
784+
group: Some(String::from("Invalid group")),
785+
default_scope: None,
786+
scope: None,
787+
skip: None,
788+
field: Some(String::from("remote.pr_labels")),
789+
pattern: Regex::new(".*").ok(),
790+
}],
791+
false,
792+
false,
793+
);
794+
795+
assert!(
796+
parse_result.is_err(),
797+
"Expected error when using unsupported field `remote.pr_labels`, but \
798+
got Ok"
799+
);
800+
742801
Ok(())
743802
}
744803

0 commit comments

Comments
 (0)