Skip to content

fix(template): correctly serialize JSON for the commit fields #1145

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 3 commits into from
May 15, 2025

Conversation

ognis1205
Copy link
Contributor

@ognis1205 ognis1205 commented May 14, 2025

Description

Fixes inconsistent serde_json::Value string representations when applying regex matchers. Specifically, it ensures that:

  • Value::String does not include surrounding quotes,
  • other scalar types (Number, Bool, Null) are stringified appropriately,
  • Object and Array are treated as unsupported and result in an error.

Closes #1143

Motivation and Context

Previously, serde_json::Value::String("example") was converted using to_string(), resulting in "\"example\"", which broke regex patterns expecting plain strings. This PR fixes that by:

  • Matching and extracting raw strings for Value::String,
  • Applying to_string() to Number, Bool, and Null,
  • Raising a user-friendly error if the field is an Object or Array.

This makes pattern matching robust and predictable when working with JSON input from commit metadata or remote PR context.

How Has This Been Tested?

  • Added unit tests for Commit::parse to verify field-based parsing logic using both author and remote attributes.
  • Verified correct parser behavior using the following commit_parsers configuration in cliff.toml:
commit_parsers = [
 { message = "$^", skip = true },
 { field = "author.name", pattern = "github-actions", skip = true },
 { field = "author.email", pattern = "github-actions@github.com", skip = true },
 { field = "committer.name", pattern = "github-actions", skip = true },
 { field = "committer.email", pattern = "github-actions@github.com", skip = true },
 { field = "remote.pr_title", pattern = "^feat", group = "<!-- 00 -->🚀 Features" },
 { field = "remote.pr_title", pattern = "^fix", group = "<!-- 01 -->🐛 Bug Fixes" },
 { field = "remote.pr_title", pattern = "^refactor", group = "<!-- 02 -->🚜 Refactor" },
 { field = "remote.pr_title", pattern = "^docs", group = "<!-- 03 -->📚 Documentation" },
 { field = "remote.pr_title", pattern = "^perf", group = "<!-- 04 -->⚡ Performance" },
 { field = "remote.pr_title", pattern = "^style", group = "<!-- 05 -->🎨 Styling" },
 { field = "remote.pr_title", pattern = "^test", group = "<!-- 06 -->🧪 Testing" },
 { field = "remote.pr_title", pattern = "^chore\\(release\\)", skip = true },
 { field = "remote.pr_title", pattern = "^chore\\(deps.*\\)", skip = true },
 { field = "remote.pr_title", pattern = "^chore\\(pr\\)", skip = true },
 { field = "remote.pr_title", pattern = "^chore\\(pull\\)", skip = true },
 { field = "remote.pr_title", pattern = "^(chore|ci)", group = "<!-- 07 -->⚙️ Miscellaneous Tasks" },
 { field = "remote.pr_title", pattern = "^revert", group = "<!-- 08 -->◀️ Revert" },
 { field = "remote.pr_title", pattern = ".*", group = "<!-- 09 -->💼 Other" },
]

Screenshots / Logs (if applicable)

N/A

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (no code change)
  • Refactor (refactoring production code)
  • Other

Checklist:

  • My code follows the code style of this project.
  • I have updated the documentation accordingly.
  • I have formatted the code with rustfmt.
  • I checked the lints with clippy.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link

welcome bot commented May 14, 2025

Thanks for opening this pull request! Please check out our contributing guidelines! ⛰️

@codecov-commenter
Copy link

codecov-commenter commented May 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 40.23%. Comparing base (4c3c946) to head (7bc6d77).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1145      +/-   ##
==========================================
+ Coverage   40.04%   40.23%   +0.20%     
==========================================
  Files          21       21              
  Lines        1911     1914       +3     
==========================================
+ Hits          765      770       +5     
+ Misses       1146     1144       -2     
Flag Coverage Δ
unit-tests 40.23% <100.00%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com>
ognis1205 added 2 commits May 15, 2025 08:56
Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com>
Signed-off-by: Shingo OKAWA <shingo.okawa.g.h.c@gmail.com>
@ognis1205 ognis1205 changed the title [WIP] fix: JSON serialization issue in Commit::parse fix: JSON serialization issue in Commit::parse May 15, 2025
@ognis1205 ognis1205 marked this pull request as ready for review May 15, 2025 05:43
@ognis1205 ognis1205 requested a review from orhun as a code owner May 15, 2025 05:43
@orhun orhun changed the title fix: JSON serialization issue in Commit::parse fix(template): correctly serialize JSON for the commit fields May 15, 2025
Copy link
Owner

@orhun orhun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, thanks for your contribution!

@orhun orhun merged commit e981e1d into orhun:main May 15, 2025
83 checks passed
Copy link

welcome bot commented May 15, 2025

Congrats on merging your first pull request! ⛰️

@ognis1205
Copy link
Contributor Author

Thank you for providing this wonderful crate @orhun !

@ognis1205 ognis1205 deleted the fix/issue-143 branch May 16, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caret ^ doesn't seem to match beginning of field remote.pr_title (GitHub) in commit_parsers
3 participants