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: configurable commit validation #648

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

protonjohn
Copy link

@protonjohn protonjohn commented Jan 2, 2023

Allow clients overriding the BaseCommitizen object to have more custom commit message validation beyond just matching a regex schema. This lets clients have custom error messages and more intelligent pattern matching.

Description

This change lets users add certain things like bad word filters or more intelligent task ID listing (for example, make sure one or more IDs appears in the commit message, and in no particular order).

Checklist

  • Add test cases to all the changes you introduce
  • Run ./scripts/format and ./scripts/test locally to ensure this change passes linter check and test
  • Test the changes on the local machine manually
  • Update the documentation for the changes

Expected behavior

Override validate_commit or validate_commits in BaseCommitizen for commit validation functions that are expressible in Python, and not just by a regular expression.

Steps to Test This Pull Request

Run the test_check_custom_validation_succeeds and test_check_custom_validation_fails test cases in tests/commands/test_check_command.py.

Additional context

I noticed during my development that the test_check_command_with_empty_range case was failing with the error:

commitizen.exceptions.GitCommandError: fatal: ambiguous argument 'master..master': unknown revision or path not in the working tree.

instead of generating its expected output. I did my best to make sure that this failure wasn't related to my changes, since it also reproduced when I ran the test suite against master. It could just be an issue with my local environment.

@protonjohn protonjohn force-pushed the jkb/feature/custom-validation branch 2 times, most recently from 674f283 to f624bd3 Compare January 2, 2023 19:17
@codecov
Copy link

codecov bot commented Jan 2, 2023

Codecov Report

Base: 97.92% // Head: 98.01% // Increases project coverage by +0.08% 🎉

Coverage data is based on head (006026a) compared to base (db42a95).
Patch coverage: 91.89% of modified lines in pull request are covered.

❗ Current head 006026a differs from pull request most recent head bbe424a. Consider uploading reports for the commit bbe424a to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #648      +/-   ##
==========================================
+ Coverage   97.92%   98.01%   +0.08%     
==========================================
  Files          35       39       +4     
  Lines        1252     1710     +458     
==========================================
+ Hits         1226     1676     +450     
- Misses         26       34       +8     
Flag Coverage Δ
unittests 98.01% <91.89%> (+0.08%) ⬆️

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

Impacted Files Coverage Δ
commitizen/commands/init.py 87.93% <84.00%> (-3.74%) ⬇️
commitizen/cz/customize/customize.py 95.34% <96.29%> (+0.26%) ⬆️
commitizen/__version__.py 100.00% <100.00%> (ø)
commitizen/commands/changelog.py 98.86% <100.00%> (ø)
commitizen/commands/check.py 100.00% <100.00%> (ø)
commitizen/cz/__init__.py 100.00% <100.00%> (ø)
commitizen/cz/base.py 100.00% <100.00%> (ø)
commitizen/cz/customize/__init__.py 100.00% <100.00%> (ø)
commitizen/exceptions.py 100.00% <100.00%> (ø)
commitizen/changelog.py 99.43% <0.00%> (ø)
... and 3 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@protonjohn protonjohn force-pushed the jkb/feature/custom-validation branch 2 times, most recently from 0f30511 to 006026a Compare January 2, 2023 19:50
@protonjohn
Copy link
Author

I'm not sure why the patch check is failing - it's complaining about a lack of test code coverage in a file that I didn't touch.

commitizen/cz/base.py Show resolved Hide resolved
commitizen/cz/customize/customize.py Outdated Show resolved Hide resolved
@@ -79,3 +83,53 @@ def info(self) -> Optional[str]:
elif info:
return info
return None


class CustomizeCommitValidationCz(CustomizeCommitsCz):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why we need to add this Cz rule. Could you describe a bit more? Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

It's mostly so that I could test it in the unit tests, but also to give others an example to work with if they want to use the feature. I didn't see a better place to put it, if you can suggest one I'm happy to put it somewhere :)

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking of moving it to tests. Not sure whether it works

commitizen/cz/customize/customize.py Show resolved Hide resolved
Allow clients overriding the BaseCommitizen object to have more custom
commit message validation beyond just matching a regex schema. This lets
clients have custom error messages and more intelligent pattern
matching.
@Lee-W
Copy link
Member

Lee-W commented Jan 18, 2023

It seems the latest version didn't pass the CI. @protonjohn could you please take a look? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants